服务器信息传输
// send to current request socket client
socket.emit(‘message’, “this is a test”);
// sending to all clients except sender
socket.broadcast.emit(‘message’, “this is a test”);
// sending to all clients in ‘game’ room(channel) except sender
socket.broadcast.to(‘game’).emit(‘message’, ‘nice game’);
// sending to all clients, include sender
io.sockets.emit(‘message’, “this is a test”);
// sending to all clients in ‘game’ room(channel), include sender
io.sockets.in(‘game’).emit(‘message’, ‘cool game’);
// sending to individual socketid
io.sockets.socket(socketid).emit(‘message’, ‘for your eyes only’);
上述集中方式为socket.io常用的数据传输方式,
io.sockets.on(‘connection’, function (socket) {
});
回调函数的socket参数为一个 client 与服务器的连接标示,不同的 client 会有不同的连接标示。
Read More →