var io = require('socket.io').listen(8080);
io.sockets.on('connection', function(socket) {
// on public
socket.on('join', function (data) {
io.sockets.emit('join', socket.id, data);
});
// on broadcast
socket.on('message', function (data) {
socket.broadcast.emit('message', socket.id, data);
});
// on privat
socket.on('whisper', function (id, data, fn) {
if (id && io.sockets.sockets[id]) {
io.sockets.sockets[id].emit('whisper', socket.id, data);
fn(true);
} else {
fn(false);
}
});
socket.on('disconnect', function () {
socket.broadcast.emit('close', socket.id);
});
});
출처 : http://firejune.com/1700/Socket.IO+%ED%95%99%EC%8A%B5+-+%ED%8D%BC%EB%B8%94%EB%A6%AD%252F%EB%B8%8C%EB%A1%9C%EB%93%9C%EC%BA%90%EC%8A%A4%ED%8A%B8%252F%ED%94%84%EB%9D%BC%EC%9D%B4%EB%B9%97+%EA%B5%AC%EB%B6%84