Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active August 29, 2015 14:01
Show Gist options
  • Save ksasao/fffc7ba8f6bd4116461a to your computer and use it in GitHub Desktop.
Save ksasao/fffc7ba8f6bd4116461a to your computer and use it in GitHub Desktop.
socket.io 経由で TWE-Lite DIP のデータを取得して、MIDI ファイルを再生するサンプル。app.js と index.html を同じフォルダに置いて node app.js で起動して http://localhost:3000 を叩くとブラウザで動作を確認できます。SocketIO4Net.Client を利用した Windows Forms 版クライアントも追加。ブラウザは Internet Explorer を想定 (.mid のURLを指定すると自動で Windows Media Player を起動して演奏するため)。MIDIファイルは http://mpga.jp/data/ofuro.mid からどうぞ。ポケットミク用です。ライセンスは WTFP…
var serialport = require('serialport');
var fs = require('fs');
var app = require('http').createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(fs.readFileSync('index.html'));
}).listen(3000);
var io = require('socket.io').listen(app);
var SerialPort = serialport.SerialPort;
var sp = new SerialPort('COM3', {
baudrate: 115200,
parser: serialport.parsers.readline('\r\n')
}, false);
sp.open(function () {
console.log('open');
sp.on('data', function(data) {
console.log('data received: ' + data);
io.sockets.emit('msg', data);
});
});
<!DOCTYPE html>
<meta charset="UTF-8">
<html><head><title>ポケットミク</title>
<script src="/socket.io/socket.io.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var lastStatus = "0";
$(function() {
var socket = io.connect();
socket.on('msg', function(data) {
var message = [ '沸いていません', 'お風呂が沸きました' ];
var status = data.substr(34,1);
if(status == 1 && lastStatus == 0) {
location.href="http://mpga.jp/data/ofuro.mid";
}
var date = new Date();
var str = date.toUTCString() + " " + status + " " + message[status];
$('div').prepend(str + '<br>');
lastStatus = status;
});
});
</script>
</head><body><div></div></body></html>
using SocketIOClient;
using System;
using System.Windows.Forms;
namespace SocketIOCs
{
public partial class Form1 : Form
{
// nuget で SocketIO4Net.Client を追加
Client sc = new Client(@"http://localhost:3000");
int lastStatus = 0;
public Form1()
{
InitializeComponent();
sc.Connect();
sc.On("msg", (res) =>
{
var data = res.Json.Args[0];
var message = new[] { "沸いていません", "お風呂が沸きました" };
var status = Convert.ToInt32(data.Substring(34, 1));
if (status == 1 && lastStatus == 0)
{
System.Diagnostics.Process.Start("http://mpga.jp/data/ofuro.mid");
}
var date = DateTime.UtcNow;
var str = date.ToString() + " " + status + " " + message[status];
this.textBox1.Invoke(new Action(()=>{
this.textBox1.Text = str + "\r\n" + this.textBox1.Text;
}));
lastStatus = status;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment