ããããNode.jsãããã£ã¦ã¿ãã
ã¤ã³ã¹ãã¼ã«å¤§å¤ãããªã¤ã¡ã¼ã¸ãåæã«æã£ã¦ãããã©ãå
¨ç¶ãããªãã¨ã¯ãªã
$ wget http://nodejs.org/dist/node-v0.2.5.tar.gz $ tar zxvf node-v0.2.5.tar.gz $ cd node-v0.2.5 $ ./configure --prefix=$HOME/local $ make $ make install
ã§æ®éã«è¡ãããmake testã§ãªããã¨ã©ã¼åºã¦ãæ°ããããã©ããã·ãã¤
ãã¨ã¯npmãæ¸ãã¦ããéã
npm
$ curl http://npmjs.org/install.sh | sh
ã§ãkã
試ãã«å¹¾ã¤ãã¤ã³ã¹ãã¼ã«ãã¦ã¿ãã
Express - Node.js web application framework
EJS - JavaScript Templates
Socket.IO
$ npm install express $ npm install ejs $ npm install socket.io
ç°¡åã
ã§ãã¢ããªãæ¸ãããµã¼ãã¼ã®app.jsã
var express = require('express'); var app = express.createServer(); var ejs = require('ejs'); var io = require('socket.io'); var port = 3000; app.configure(function(){ app.use(express.staticProvider(__dirname + '/static')); }); app.set('view engine', 'ejs'); app.set('view options', { layout: false }); app.get('/', function(req, res) { console.log('/'); res.render('index', { locals: { port: port } }); }); app.listen(port); var socket = io.listen(app); socket.on('connection', function(client) { client.on('message', function(msg) { client.send(msg); client.broadcast(msg); }); client.on('disconnect', function() { console.log('disconnect'); }); }); console.log('Server running at http://127.0.0.1:' + port + '/');
"views/index.ejs"ã
<html> <head> <title>hoge</title> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript">google.load("jquery", "1.4.4");</script> <script type="text/javascript" src="/socket.io/socket.io.js"></script> <script type="text/javascript">var port = <%= port %>;</script> <script type="text/javascript" src="/js/client.js"></script> <link href="/css/style.css" rel="stylesheet" type="text/css"> </head> <body> <form action="" id="form"> <input type="text" name="message" id="message" /> <input type="submit" /> </form> <hr /> <dl id="list"></dl> </body> </html>
ã¯ã©ã¤ã¢ã³ãå´ã®jsã"static/js/client.js"ã¨ãã¦
$(function() { var socket = new io.Socket(null, { port: port }); socket.connect(); socket.on('connect', function() { console.log('connect'); }); socket.on('message', function(msg) { var date = new Date(); $('#list').prepend($('<dt>' + date + '</dt><dd>' + msg + '</dd>')); }); socket.on('disconnect', function(){ console.log('disconnect'); }); $('#form').submit(function() { var message = $('#message'); socket.send(message.val()); message.attr('value', ''); return false; }); });
ã¨æ¸ããã
ã§ããã¨ã¯
$ node app.js
ã¨ããã ããroot権éãããªãã¨"flash XML policy file"äºã
ãã¨socket.ioããè¦åãåºããã©ãWebSocket使ã£ã¦ãã°é¢ä¿ãªãããªã
submitããã¨ç¬æã«ä»ã¯ã©ã¤ã¢ã³ãå´ã«ãåæ ããããã¹ããã
ææ³
çµæ§ç°¡åã«åãããããæ¸ãã¦ãæã£ã¦ã以ä¸ã«ã¨ã£ã¤ããããã£ããnode.jsã
- ãã¬ã¼ã ã¯ã¼ã¯"express"
"http.createServer"ã ãã§å¦çãæ¸ãã¦ããã¨é¢åãããªã®ã§ããããããã®WAFãããã¨ã¡ãã£ã¨ãããã®ä½ãã®ã«ä¾¿å©ãªæ°ããããã¤ã¤ã«ã³ã¸ã
- ãã³ãã¬ã¼ãã¨ã³ã¸ã³"ejs"
Expressã§ãµãã¼ããã¦ããã³ãã¬ã¼ãã¨ã³ã¸ã³ã§Jadeã ã¨ãHaml.jsã ã¨ããã£ããã©ãããã¨è¦ãã«ã³ã¸ ã¨ã¦ã使ã£ã¦ã¿ãæ°ã«ã¯ãªããªãã£ãã®ã§ãä¸çªç°¡åãããªejsãé¸æãããããã©ã¼ãã³ã¹ã¨ãç´°ãã使ãæ¹ã¯ã¾ã åãããªããã©ä¸çªé¦´æã¿ãããã¦ãæ軽ã ã¨æãã
- socket.io
ãã©ã¦ã¶ãµãã¼ãã ã¨ãæèãããã¨ãªããµã¼ãã¼å´ãã¯ã©ã¤ã¢ã³ãå´ãåãããã«è¨è¿°ã§ããã®ã¯ç¢ºãã«ä¾¿å©ããªã¢ã«ã¿ã¤ã ç³»ã¯ã¨ããããã³ã¬ä½¿ã£ã¦ããã°è¯ããã¨ããã«ã³ã¸ããªã¼