node.jsのsocket.ioをテスト

はじめにnpmでsocket.ioをインストールするが
カレントディレクトリにnode_modulesというディレクトリを
つくって、そこにインストールされるようなので、
node.jsの開発用ディレクトリに移動してインストールする。

$ mkdir node_dev
$ cd noed_dev
$ npm install scoket.io


http://socket.io/
やら
http://spechal.com/2011/03/19/super-simple-node-js-chatroom/
やらを参考に
インストール後、サーバー側のテスト用コードを以下のように書き、server.jsというファイ名で保存します。

var http = require('http'),
io = require('/home/nabe/node_dev/node_modules/socket.io'),
fs = require('fs');

// normal server code
server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type':'text/html'});
  // read index.html and send it to the client
  var output = fs.readFileSync('./index.html', 'utf8');
  res.end(output);
});
server.listen(8080);

//socket.io
var socket = io.listen(server);

socket.on('connection', function(client){
  // broadcast the connection
  // client.broadcast({message: client.sessionId 
  // + ' is now available'});

  // when the server gets a message, during a connection,
  // broadcast the message
  client.on('message', function(msg){
    console.log('recieve message:' + msg);

    // 自分にメッセージを送る
    //client.send({messag: msg});

    // 自分以外の全員にメッセージを送る
    client.broadcast({message: msg});
    // 全員に送る場合は
    //socket.broadcast({message: msg});
  });

  // when the server gets a disconnect, 
  // during a connection, broadcast the disconnection
  client.on('disconnect', function(){
        client.broadcast({ message: client.sessionId + ' is no longer available' });
  });
});


次にindex.htmlを以下のように書きます。

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>test</title>
  <script src="/socket.io/socket.io.js"></script>
  <script>
    // make a connection to the server
    var socket = new io.Socket('192.168.15.132', {port:8080, connectTimeout:3000});
    socket.connect();

    //connect
    socket.on('connect', function(client) {
        var div1 = document.getElementById('div1');
        div1.innerHTML = "connected";
    });

    socket.on('message', function(msg) {
        alert("recieved message:" + msg.message);
    });

  
    function sendMsg() {
        socket.send("hello world!");
    }
  </script>
</head>
<body>
<h1>socket.io test</h1>
<div id="div1">
socket.io test
</div>

<form>
  <input type="button" value="button"  onclick="sendMsg();"/>
</form>
</body>
</html>

このindex.htmlはserver.jsに読みこまれてクライアントに送り出されます。
でここで、サーバーを起動

$ node server.js

接続のメッセージはうまく受信できましたが、なぜかボタンを押して、sendMsg()でサーバーにメッセージを送って、それをそのまま全員に返してもらう
clicen.broadcastが動作しないいいいいい。
と思ったら、client.broadcastは

https://github.com/LearnBoost/Socket.IO-node/blob/0.6.3/README.md
のREADMEにあるように

Methods:

send(message)

Sends a message to the client.

broadcast(message)

Sends a message to all other clients. Equivalent to Listener::broadcast(message, client.sessionId).

all other clienetsらしいので、送った自分以外の全員にブロードキャストするらしいのでした。
つまり、自分も含めてブロードキャストするには


// 自分にメッセージを送る
client.send({messag: msg});
// 自分以外の全員にメッセージを送る
client.broadcast({message: msg});

の2つを書く
または

// 全員に送る場合は
socket.broadcast({message: msg});

と書く必要があるということです。