Google グループ
socket.io本体の方はちょっとよく分からない。以前書いた問題の修正はまだ取り込まれていないようなので注意
socket.io-clientは0.7.5になって単体でNodeからrequireして使うことができるようになった。
var io = require('socket.io').listen(3000); io.sockets.on('connection', function (socket) { console.log('connection'); setInterval(function () { socket.emit('hoge', { fuga: 'piyo' }); }, 1000); });
とか適当なsocket.ioサーバを立ち上げておいて、
var io = require('socket.io-client'); var socket = io.connect('http://localhost:3000'); socket.on('connect', function () { console.log('connect'); }); socket.on('hoge', function (data) { console.log(new Date() + ':' + JSON.stringify(data)); });
こんなクライアント側のNodeプログラムを動かして繋ぐことができるようになってる(0.7.4以前はたぶんrequireすることすら出来なかった)。
$ node client.js connect Tue Aug 09 2011 00:56:03 GMT+0900 (JST):{"fuga":"piyo"} Tue Aug 09 2011 00:56:04 GMT+0900 (JST):{"fuga":"piyo"} Tue Aug 09 2011 00:56:05 GMT+0900 (JST):{"fuga":"piyo"} Tue Aug 09 2011 00:56:06 GMT+0900 (JST):{"fuga":"piyo"} Tue Aug 09 2011 00:56:07 GMT+0900 (JST):{"fuga":"piyo"} ...
サーバ側ではこんなログが出てる。
$ node server.js info - socket.io started debug - client authorized info - handshake authorized 269741578473335295 debug - setting request GET /socket.io/1/websocket/269741578473335295 debug - set heartbeat interval for client 269741578473335295 debug - client authorized for debug - websocket writing 1:: connection debug - websocket received data packet 5:1+::{"name":"newListener","args":["connect"]} debug - websocket received data packet 5:2+::{"name":"newListener","args":["hoge"]} debug - websocket writing 5:::{"name":"hoge","args":[{"fuga":"piyo"}]} debug - websocket writing 5:::{"name":"hoge","args":[{"fuga":"piyo"}]} debug - websocket writing 5:::{"name":"hoge","args":[{"fuga":"piyo"}]} debug - websocket writing 5:::{"name":"hoge","args":[{"fuga":"piyo"}]} ...
xhr-pollingで繋ぐこともできて、
var io = require('socket.io-client'); io.transports = ['xhr-polling']; var socket = io.connect('http://localhost:3000'); socket.on('connect', function () { console.log('connect'); }); socket.on('hoge', function (data) { console.log(new Date() + ':' + JSON.stringify(data)); });
とやればwebsocketではなくxhr-pollingでサーバに接続するようだ。
$ node server.js info - socket.io started debug - client authorized info - handshake authorized 145573163291229429 debug - setting request GET /socket.io/1/xhr-polling/145573163291229429 debug - setting poll timeout debug - client authorized for debug - clearing poll timeout debug - xhr-polling writing 1:: debug - set close timeout for client 145573163291229429 connection debug - setting request GET /socket.io/1/xhr-polling/145573163291229429 debug - setting poll timeout debug - discarding transport debug - cleared close timeout for client 145573163291229429 debug - xhr-polling received data packet �47�5:1+::{"name":"newListener","args":["connect"]}�44�5:2+::{"name":"newListener","args":["hoge"]} debug - xhr-polling received data packet �47�5:1+::{"name":"newListener","args":["connect"]}�44�5:2+::{"name":"newListener","args":["hoge"]} debug - clearing poll timeout debug - xhr-polling writing 5:::{"name":"hoge","args":[{"fuga":"piyo"}]} debug - set close timeout for client 145573163291229429 debug - setting request GET /socket.io/1/xhr-polling/145573163291229429 debug - setting poll timeout debug - discarding transport debug - cleared close timeout for client 145573163291229429 debug - clearing poll timeout debug - xhr-polling writing 5:::{"name":"hoge","args":[{"fuga":"piyo"}]} debug - set close timeout for client 145573163291229429 debug - setting request GET /socket.io/1/xhr-polling/145573163291229429 debug - setting poll timeout debug - discarding transport debug - cleared close timeout for client 145573163291229429 debug - clearing poll timeout
特にソース読み込んだわけではないし詳しく検証したわけでもないけど、結構便利に使えそうな気がする。
ローカルのNodeプログラムで外部Socket.IOサーバにWebSocketで接続する - すぎゃーんメモでやろうとしていたことが、socket.io-clientをrequireするだけで使えるようになる、のは嬉しい。