socket.io使用随笔

这段时间一直在做一个手机APP,正好使用到了socket.io。这里记录一下服务器端发送信息的几种不同方式:

socket
.emit('message',"this is a test");//发送给对应的客户端

io.sockets.emit('message',"this is a test");//发送给所有客户端
socket
.broadcast.emit('message',"this is a test");//发送给除了发送者之外的所有客户端
socket.broadcast.to('game').emit('message','nice game');//发送给房间game中除了发送者的所有客户端
io.sockets.in('game').emit('message','cool game');//发送给房间game中的所有客户端,包括发送者

io.sockets.socket(socketid).emit('message','for your eyes only');//发送给指定socketId的客户端


知识从这里获取 http://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender-socket-io
posted @ 2013-07-18 11:18  ajccom  阅读(1119)  评论(0编辑  收藏  举报