发出到特定的套接字(socket.io v2)

时间:2017-06-17 20:50:00

标签: javascript node.js sockets socket.io

我正在尝试向特定插座发射并尝试几乎所有东西但没有运气。

两个连接

client1 - side clientide

var socket = io.connect('/room2');
socket.on('msg', function(msg) {
 console.log(msg);
 }

client2 - side ejs

var socket = io.connect('/room1');
socket.emit('message', 'This is my message');

client2 - 服务器端

socket.on('messsage', function(msg) {
   var clinet1 = io.to(socket.id of client 1).emit('msg', "send message');
    //Not working

   var client1 = io.of('/room1').to('client1').emit('msg', 'send message');
    //Not working

   io.sockets.connected[socket_id].emit('msg', 'send message');
   // not working

}

1 个答案:

答案 0 :(得分:0)

尝试将'message'更改为任何其他名称。事件名称'message'已保留,仅由socket.send

发送