Subscribed unsubscribe Subscribe Subscribe

socket.io 0.7 でチャンネルをとりあえず振り分ける

var chat = require('socket.io').listen(80)
    .of('/chat')
    .on('connection', function(socket) {
      socket.on('message', function(msg) {
        socket.get('roomName', function(err, name) {
          console.log("[message]", name);
          chat.in(name).send(msg);
        });
      });

      socket.on('disconnect', function() {
        console.log("[disconnect]", socket.id);
        socket.get('roomName', function(err, name) {
          socket.leave(name);
          socket.broadcast.to(name).send(socket.id + " leave from " + name);
        });
      });                                                                                           
                                                                                                    
      socket.on('join', function(name, fn) {                                                        
        console.log("[join]", socket.id);                                                           
        socket.set('roomName', name);                                                               
        socket.join(name);                                                                          
        socket.broadcast.to(name).send(socket.id + " join to " + name);                             
        fn();                                                                                       
      });                                                                                           
    }); 

こんな感じかなぁ。