socket.io简单使用说明
连接
connection事件是连接事件
connection等价于connect
io等价于io.sockets
io.sockets.on('connection', function (so) {
// 把io看作电话机房,那么so可以看作是连到电话机房的电话线
});
socket方法
即connection之后的每个电话线
emit用以回答当前的socket,可以理解为回复给当前的电话线
send(data)等价于emit(‘message’,data);
socket.emit('message', "this is a test");
发送给其他所有的用户(除去自己)
socket.broadcast.emit('message', "this is a test");
发送给”某个频道”的其他所有的用户(除去自己)
to来选择频道
to等价于in
socket.broadcast.to('game').emit('message', 'nice gameµ');
发送给所有的用户
io.sockets.emit('message', "this is a test");
发送给”某个频道”的其他所有的用户
io.sockets.to('game').emit('message', 'cool game');
针对某人发送一个信息
io.sockets.socket(socketid).emit('message', 'for your eyes only');