2012.12.22
ææ°çã§åä½ããããã¨ã³ããªãä¿®æ£ãã¾ããã
2011.12.01
ææ°çã§åä½ããããã¨ã³ããªãä¿®æ£ãã¾ããã
2011.5.25
ææ°çã§åä½ããããã¨ã³ããªãä¿®æ£ãã¾ããã
ã¯ããã«
node.js + socket.ioã使ã£ããµã¼ãããã·ã¥ã®ãµã³ãã«ããã°ã©ã ãä½ã£ã¦ã¿ã¾ããã
å¿ è¦ãªã½ããã¦ã§ã¢
- node.js v0.4.7
- npm 0.3.18
- socket.io 0.6.17
- express 2.2.2
äºåæºå
node.jsã¨npmãã¤ã³ã¹ãã¼ã«ããããnpmã³ãã³ãã§å¿ è¦ãªã½ããã§ã¢ãä¸æ¬ã¤ã³ã¹ãã¼ã«ãã¾ãã
sudo npm install express ejs socket.io -g
ã¢ããªã±ã¼ã·ã§ã³ã®ã²ãªå½¢ãèªåçæãã
以ä¸ã®ã³ãã³ãã§ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ã®ã²ãªå½¢ãä½ãã¾ãã
express -t ejs SampleApp
ã²ãªå½¢ãã§ãããã£ããããã®ãã¡app.jsã¨viewsãã£ã¬ã¯ããªã®index.ejsãç·¨éãã¾ãã
ãµã¼ããµã¤ãï¼app.jsï¼
require.paths.push('/usr/local/lib/node_modules'); var express = require('express'); var app = express.createServer(); // 3000ãã¼ãã§ããã°ã©ã ãåä½ããã app.listen(3000); // ã½ã±ãããä½ã var socketIO = require('socket.io'); // ã¯ã©ã¤ã¢ã³ãã®æ¥ç¶ãå¾ ã¤(IPã¢ãã¬ã¹ã¨ãã¼ãçªå·ãçµã³ã¤ãã¾ã) var socket = socketIO.listen(app); // ã¯ã©ã¤ã¢ã³ããæ¥ç¶ãã¦ããã¨ãã®å¦ç socket.on('connection', function(client) { console.log(client.sessionId + 'ãæ¥ç¶ãã¾ããã'); // ã¡ãã»ã¼ã¸ãåããã¨ãã®å¦ç client.on('message', function(msg) { console.log(client.sessionId + "'ãã¡ãã»ã¼ã¸ãéä¿¡ãã¾ããã(" + msg + ")"); // ã¤ãªãã£ã¦ããã¯ã©ã¤ã¢ã³ãå ¨å¡ã«éä¿¡ client.send(msg); client.broadcast(msg); }); // ã¯ã©ã¤ã¢ã³ããåæããã¨ãã®å¦ç client.on('disconnect', function(){ console.log(client.sessionId + 'ãåæãã¾ããã'); }); }); // ãªã¯ã¨ã¹ããæ¥ãããindex.ejsã®å 容ãã¯ã©ã¤ã¢ã³ãã«åºåãã app.get('/', function(req, res){ res.render('index.ejs', { layout: false }); });
ã¯ã©ã¤ã¢ã³ããµã¤ãï¼index.ejsï¼
<script type="text/javascript" src="/socket.io/socket.io.js"></script> <script type="text/javascript"> var socket = new io.Socket(); // ãµã¼ãã«æ¥ç¶ãã socket.connect(); // ãµã¼ãã«æ¥ç¶ããã¨ã socket.on('connect', function(msg) { document.getElementById("connectId").innerHTML = "ããªãã®æ¥ç¶ID::" + socket.transport.sessionid; }); // ã¡ãã»ã¼ã¸ãåããã¨ã socket.on('message', function(msg) { document.getElementById("receiveMsg").innerHTML = msg; }); // ã¡ãã»ã¼ã¸ãéã function SendMsg() { var msg = document.getElementById("message").value; socket.send(msg); } // åæãã function DisConnect() { socket.send(socket.transport.sessionid + "ã¯åæãã¾ããã"); socket.disconnect(); } </script> <h1>socket.ioã®ãµã³ãã«ããã°ã©ã </h1> <div id="connectId"></div> <input type="text" id="message" value=""> <input type="button" value="ã¡ãã»ã¼ã¸ãéã" onclick="SendMsg()"> <input type="button" value="åæãã" onclick="DisConnect()"> <div id="receiveMsg"></div>
ãµã³ãã«ããã°ã©ã ã®åããæ¹
以ä¸ã®ã³ãã³ãã§ãµã¼ãããã°ã©ã ãèµ·åãã¾ãã
node app.js
è¤æ°ã®ãã©ã¦ã¶ã§http://localhost:3000/ã«ã¢ã¯ã»ã¹ãã¾ãã
ã³ã¼ã解説
- console.logã¯ãµã¼ãã®ã³ã³ã½ã¼ã«ã«ãã°ãåºåãã¦ãã¾ãã
- client.send(msg);ã¯ã¡ãã»ã¼ã¸ãéä¿¡ãã¦ããã¯ã©ã¤ã¢ã³ãã«ã®ã¿ã¡ãã»ã¼ã¸ãéä¿¡ãã¾ãã
- client.broadcast(msg);ã¯ã½ã±ããã«ã¤ãªãã£ã¦ããã¯ã©ã¤ã¢ã³ããã¹ã¦ï¼ã¡ãã»ã¼ã¸éä¿¡è ã¯é¤ãï¼ã«ã¡ãã»ã¼ã¸ãéä¿¡ãã¾ãã
- ã¯ã©ã¤ã¢ã³ããµã¤ãã§ãsocket.transport.sessionidã§ã³ãã¯ã·ã§ã³çªå·ãåå¾ã§ãã¾ãã
æè¡è©è«ç¤¾
売ãä¸ãã©ã³ãã³ã°: 2480
以ä¸ã®ã¨ã³ããªããããã¦ã©ãã
- node.js + websocketã§ä»ã®ãã·ã³ããæ¥ç¶ã§ããªãå ´åã®å¯¾å¦æ¹æ³
- 2011å¹´ node.jsã«é¢ããè¨äºã®ãã¯ãæ°ã©ã³ãã³ã°TOP50ãä½ã£ã¦ã¿ããã
- node.jsã®ãããã°(ã¹ãããå®è¡)ãã§ããnode-inspectorã®ä½¿ãæ¹
- ï¼åãããã§åºæ¥ãnode.js(0.6) + socket.io(0.8x)ã®ãµã³ãã«ããã°ã©ã
- æ°ãããªã£ãdotcloudã§node.jsã®Hello Worldãåããã¦ã¿ããã