我是socket.io的新手,遇到了一些看起来很奇怪的事情。我实际上不知道和之间的区别socket.emit,io.emit但是在任何地方都找不到解释。
socket.emit
io.emit
io.on('connection', function(socket){ io.emit('connected') // <<<< HERE >> socket.emit('connected'); socket.on('disconnect', function(){ io.emit('disconnect') }); socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); server.listen(3000);
这是我的但是服务器的东西时,我改变io到socket该消息时,用户是谁连接的连接将仅被显示。io.emit将消息发送给所有用户。
io
socket
也许应该是这样,或者只是一些可怕的骇客?让我知道您是否需要客户端HTML。
这是一个补充文档,以供参考。
socket.emit('message', "this is a test"); //sending to sender-client only socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel) socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid io.emit('message', "this is a test"); //sending to all clients, include sender io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender socket.emit(); //send to all connected clients socket.broadcast.emit(); //send to all connected clients except the one that sent the message socket.on(); //event listener, can be called on client to execute on server io.sockets.socket(); //for emiting to specific clients io.sockets.emit(); //send to all connected clients (same as socket.emit) io.sockets.on() ; //initial connection from a client.
希望这可以帮助!。