node.jsã®Socket IO-nodeã§WebSocket
ååã¯ãpywebsocketã§ã®WebSocketãç´¹ä»ãã¾ããããHTML5 hack-a-thonã§ã¯çµå±node.jsã®å®è£
ã§ãã Socket IOã¨Socket IO-nodeã使ãã¾ããã
ãã¡ããåå¿é²ã
node.js
node.jsã¯ãããããµã¼ããµã¤ãJavaScriptã®ä¸ã¤ã§ãæè¿ã«ããã«æ³¨ç®ãéãã¦ã(?)æè¡ã§ãã
主ãªç¹å¾´ã¯
node.jsã®æ¦è¦èªä½ã¯ããã¡ãã®PREZIã使ã£ããã¬ã¼ã³ãç°¡æ½ã§åããããããã¨æãã¾ãã
Introduction to node.js by Toshihiro Shimizu on Prezi
ä¸åº¦ä»æ¥ãnode.jsã®48æéèä¹
Hack-a-thonãéå¬ããã¦ãã¾ããã
ãã®åããNode.js Knockoutã@mesoãããåå ããã¦ã¾ãã
Node.js Knockout | Nov 9-11th, 2013
node.jsã«ã¤ãã¦ã¯ãã®ãã¡ããå°ãçºããè¨äºãæ¸ããããªã¨æã£ã¦ãã¾ãã
æã£ã以ä¸ã«æ軽ã«è²ã
åºæ¥ãã®ã§ãé¢ç½ãã§ãã
node.websocket.js
node.jsã®WebSocketå®è£
ã¨ãã¦ã¯ãã¡ãã£ã¨åã¾ã§ã¯ä»¥ä¸ã®ããã¸ã§ã¯ããè¯ãç´¹ä»ããã¦ãã¾ããã
ä»æã®Web+DB Press ã®ãHTML5ÃCSS3ãã§ãã¡ãã£ã¨ç´¹ä»ããã¦ãã¾ãã
guille/node.websocket.js · GitHub
ã¨ããããã½ã¼ã¹ãåå¾ããrunserver.js ãèµ·åããã¨ãã以ä¸ã®ãããªã¨ã©ã¼ãåºã¾ããã
this.conn = new process.tcp.Connection(); ^ TypeError: Cannot read property 'Connection' of undefined
ã©ãããã®å®è£
ã¯ãnode.jsã®ver 0.1.9以åã§ãªãã¨åããªãããã§ãèªåããã«ããã¦ããver 0.2.0ã§ã¯ãµã¼ããèµ·åãããã¨ãåºæ¥ãªãããã§ãã
(tcp.Connection()ã§ã¯ãªãã®APIãå¤ãã£ã¦ãã¿ããã§ãããä»ã¯net.Stream() ã使ãã°ããã¿ããã§ãã)
ããã¸ã§ã¯ãã®ã³ãããèªä½ãã2010/04/30ã§æ¢ã¾ã£ã¦ãããã¨ãè¦ã¦ããnode.jsã®ãã¼ã¸ã§ã³ã¢ããã«è¿½å¾ãã¦ããªãããã§ãã
ã§ãREADE.mdã«ã¯ä»¥ä¸ã®è¨è¿°ã
Update: For a more complete and cross-browser/cross-platform solution, I recommend you check out Socket.IO and Socket.IO-node, which I'm actively maintaining. ä»ã¯ãSocket.IOã¨Socket.IO-nodeãã¡ã¤ã³ã§ã¡ã³ããã³ã¹ãã¦ããã ãã£ã¡ã®æ¹ãããå®ç§ãªã¯ãã¹ãã©ã¦ã¶ï¼ã¯ãã¹ãã©ãããã©ã¼ã ãå®ç¾ãã¦ããã®ã§ããã£ã¡ããå§ããããï¼
ã¨ã®ãã¨ã§ãã
ã¨ãããã¨ã§ãSocket.IOã使ããã¨ã«ãã¾ãã
Socket.IO 㨠Socket.IO-node
ãã¡ãããnode.websocket.jsã¨åãä½è
ã®å
¬éãã¦ããå®è£
ã§ãã
ãµã¼ãå´ã®å®è£
ã¨ã¯ã©ã¤ã¢ã³ãå´ã®ã©ã¤ãã©ãªã®ä¸¡æ¹ãæä¾ããã¦ãã¾ãã
- LearnBoost/socket.io-node · GitHub : ãµã¼ãå´ã®WebSocketå®è£
- LearnBoost/socket.io · GitHub : ã¯ã©ã¤ã¢ã³ãã®ã©ã¤ãã©ãª
Socket.IO-node
åºæ¬çã«ã¯WebSocketã®å®è£
ã¨ããããããªã¢ã«ã¿ã¤ã éä¿¡ç¨ã®ãµã¼ãå®è£
ã¨ããä½ç½®ã¥ãã®ããã§ãã
ã¤ã¾ããWebSocketãç¡ãã¯ã©ã¤ã¢ã³ãã«ã¯ã代æ¿ã®æ段ãæä¾ããå®è£
ã«ãªã£ã¦ããã¨ãããã¨ã§ã以ä¸ããµãã¼ããã¦ãã¾ãã
* WebSocket (with Flash policy support) * XHR Polling * XHR Multipart Streaming * Forever Iframe
node.jsã¯ver0.1.102以ä¸ãå¿
è¦ã¨ã®ãã¨ã§ãã
ã¾ããã¯ã©ã¤ã¢ã³ãå´ã¯Socket.IOãå°ç¨ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã¨ãã¦ä½¿ãã¾ãã
(æ®éã®WebSocketãªãã¸ã§ã¯ããä½æãã¦è©¦ãã¾ããããéä¿¡ã¯åºæ¥ã¾ããã§ããã)
Socket.IO
Socket.IO-nodeå°ç¨ã®ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã§ãã
ããã使ã£ã¦å®è£
ããã¨ãWebSocketã®ç¡ããã©ã¦ã¶ã§ããªã¢ã«ã¿ã¤ã éä¿¡ãå®ç¾ãããã¨ãåºæ¥ã¾ãã
READMEã«ããã¨ãµãã¼ããã¦ããã®ã¯ã
Supports * WebSocket * Adobe Flash Socket * ActiveX HTMLFile (IE) * XHR with multipart encoding * XHR with long-polling * JSONP polling (for cross-domain) Tested on * Safari 4 * Google Chrome 5 * Internet Explorer 6 * Internet Explorer 7 * Internet Explorer 8 * iPhone Safari * iPad Safari * Firefox 3 * Firefox 4 (Minefield)
ç´ æ´ãããã§ããã
Draftã®éãã ãã§ãªããWebSocketã®å®è£
æç¡ãã«ãã¼ãã¦ãããã®ã§ãWebSocketã®ç¡ããã©ã¦ã¶ã§ãåãã³ã¼ãã§åãããã¨ãåºæ¥ãã¨ããç¹ã§ãã
å¤åç¾æç¹ã§ããã¾ã§å®ç¨çãªã©ã¤ãã©ãªã¯ä¸ã
ç¡ãã¨æãã¾ãã
使ãæ¹
注æãå¿
è¦ãªã®ã¯ãäºã¤ãä¾åé¢ä¿ã¨ããããäºã¤ãã¢ã§ä½¿ããã¨ãåæã«ãã¦ããç¹ã§ãã
ãªã®ã§ãgithubã®ã½ã¼ã¹ãçæ¹ã¥ã¤ã§ã¯ãªãã両æ¹ãã£ãºãã«è½ã¨ãã¦æ¥ã¦é
åãããã¨ãåºæ¥ãæ§ã«ãªã£ã¦ãã¾ãã
Socket.IO-nodeã®æ¹ã«æ¸ããã¦ã¾ããã以ä¸ã®æ§ã«åå¾ãã¾ãã
git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io-node --recursive cd socket.io-node/example/ sudo node server.js
ããã§æ¢ã«ãµã³ãã«ã®chatã¢ããªãåããã¾ãã
ãããåãããªããè²ã
試ãã¦ã¿ãã¨ãããååã®è¨äºã®æ§ã«ã
ãµã¼ãï¼ãæ¥ãç©ããã®ã¾ã¾è¿ãã
ã¯ã©ã¤ã¢ã³ãï¼ããµã¼ãã«ãã ãã¼ã¿ãæãã¦ããã åãåãã
ã¨ãã£ãç°¡åãªå®è£
ã§ããã°ã大ã¾ãã«ãããªæãã§å®è£
ãã¾ãã
//ãµã¼ãå´ã®å®è£ var http = require('http'), io = require('./path/to/socket.io'), server = http.createServer(function(req, res){ // ãµã¼ãã®è¨å® res.writeHeader(200, {'Content-Type': 'text/html'}); res.writeBody('<h1>Hello world</h1>'); res.finish(); }); server.listen(8080); //ãªã¹ãã³ã°ãããã¼ã var io = io.listen(server); //socketã®åå¾ io.on('connection', function(client){ //ã¦ã¼ã¶ãæ¥ç¶ãã¦æ¥ããå®è¡ããã //æ¥ç¶æã®åæåå¦çãæ¸ã client.on('message',function(message){ //ã¯ã©ã¤ã¢ã³ããã¡ãã»ã¼ã¸ãéã£ã¦æ¥ããå®è¡ãããã //messageãéã£ã¦æ¥ããã¼ã¿ client.send(message); //éã£ã¦æ¥ãæ¬äººã ãã«éãã client.broadcast(message); //éã£ã¦æ¥ã人以å¤å ¨å¡ã«éãã }); client.on('disconnect', function(){ //ã¯ã©ã¤ã¢ã³ããåæãããå®è¡ãããã client.broadcast(client.sessionId + ' disconnected' }); //ä»å ¨å¡ã«åæãã人ã®sessionIdãéãã }); });
//ã¯ã©ã¤ã¢ã³ãå´ã®å®è£ //socket.io.jsãã¡ã¤ã«ãèªãã§ãã //ã³ãã¯ã·ã§ã³ã®ç¢ºç« io.setPath('/path/to/socket.io/'); socket = new io.Socket('localhost',{port:8080}); socket.connect(); //ãã¼ã¿ã®éä¿¡ socket.send('data'); //ãã¼ã¿ã®åä¿¡ socket.on('message', function(data){ //åä¿¡ãããå®è¡ããã //dataãåãåã£ããã¼ã¿ console.log(data); });
éä¿¡é¨åã®å®è£
èªä½ã¯é常ã«ã·ã³ãã«ã§ãããåºæ¬çã«ã¯ããã ãã§ãã
ãã¨ã¯ããã©ã使ããã¨ããã¢ã¤ãã¢ã ãã§ãã
ãµã³ãã«ã®ä¸ã«ããChatçãããããªãããè²ã 試ãã¦ã¿ãã¨è¯ãã¨æãã¾ãã
ãã¨ãä»åã®node.js KOã§ãããªã¢ã«ã¿ã¤ã ç³»ã®ã¢ããªãå¤ãæãããã§ãã
ããããSocket.IOã使ã£ãå®è£
ãå¤ãã¯ããªã®ã§ããããã¯å¤ãã«åèã«ãªãã¨æãã¾ãã
以ä¸ã¯ãHack-a-thonã®ä½åã¨åå è
ã®ä¸è¦§ã
ã¡ã¢
node.jsã¯èªåãã¾ã å§ããã°ããã§ãããä»å使ã£ã¦ã¿ã¦æã£ãã¨ãããã¡ã¢ãã¦ããã¾ãã
- node.jsã¯éçºè ãããã©ã¼ãã³ã¹ãããªãæ°ã«ãã¦ããã ããã£ã¦ãåéããµã¯ãµã¯åãã¦ãæ°ãããã
- ã¤ãã³ãé§åIOã®å©ç¹ãçããã¦ãããããã½ã±ããéä¿¡ãç¨ãããªã¢ã«ã¿ã¤ã ã¢ããªã±ã¼ã·ã§ã³ã¨node.jsã¯ç¸æ§ãè¯ãã¨è¨ãããã
- Linux/Unixã§ããã°å°å ¥ããæ·å± ã¯ãã»ã©é«ããªãã
- ãµã¼ãï¼ã¯ã©ã¤ã¢ã³ãå ±ã«åãè¨èªã§æ¸ããã®ã¯çµæ§æãã
- APIã®å®è£ ãé²ã¿ããã£ã¬ã¼ã ã¯ã¼ã¯ãããã¤ãåºã¦ããã
node.jsè²ã
楽ãã¿ã§ããã