Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在套接字IO中向特定客户端发送消息_Node.js_Sockets - Fatal编程技术网

Node.js 在套接字IO中向特定客户端发送消息

Node.js 在套接字IO中向特定客户端发送消息,node.js,sockets,Node.js,Sockets,我正在使用Socket IO v1.4.5,并尝试了下面3种不同的方法,但没有任何结果 client.emit('test', 'hahahaha'); io.sockets.socket(id).emit('test',''hahaha); io.sockets.connected[id].emit('test','hahaha'); 这是我的服务器端 var socket = require( 'socket.io' ); var express = require( 'express'

我正在使用Socket IO v1.4.5,并尝试了下面3种不同的方法,但没有任何结果

client.emit('test', 'hahahaha');
io.sockets.socket(id).emit('test',''hahaha); 
io.sockets.connected[id].emit('test','hahaha');
这是我的服务器端

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var dateFormat = require('date-format');
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
    user[client.id]=client;

//when we receive message 
    client.on('message', function( data ) {
        console.log( 'Message received from' + data.name + ":" + data.message +' avatar' +data.avatar );
        client.emit('test', 'hahahaha');
});

任何帮助都会很好。感谢您的帮助。要向特定客户发送消息,您需要这样做:

socket.broadcast.to(socketid).emit('message', 'for your eyes only');
下面是一张关于插座的小备忘单:

 // sending to sender-client only
 socket.emit('message', "this is a test");

 // sending to all clients, include sender
 io.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 in 'game' room(channel), include sender
 io.in('game').emit('message', 'cool game');

 // sending to sender client, only if they are in 'game' room(channel)
 socket.to('game').emit('message', 'enjoy the game');

 // sending to all clients in namespace 'myNamespace', include sender
 io.of('myNamespace').emit('message', 'gg');

 // sending to individual socketid
 socket.broadcast.to(socketid).emit('message', 'for your eyes only');
归功于


与直接发送到套接字相比,最简单的方法是创建一个供两个用户使用的空间,并在其中自由发送消息

socket.join('some-unique-room-name'); // Do this for both users you want to chat with each other
socket.broadcast.to('the-unique-room-name').emit('message', 'blah'); // Send a message to the chat room.

否则,您将需要跟踪每个单独的客户端套接字连接,并且当您想要聊天时,您必须查找该套接字连接,并使用我上面提到的函数专门向该套接字发送。房间可能更简单。

Socket.io版本2.0.3+

向特定套接字发送消息

    let namespace = null;
    let ns = _io.of(namespace || "/");
    let socket = ns.connected[socketId] // assuming you have  id of the socket
    if (socket) {
        console.log("Socket Connected, sent through socket");
        socket.emit("chatMessage", data);
    } else {
        console.log("Socket not connected, sending through push notification");
    }
就这么做吧

io.socket.in('room').emit("send message to everyone", data);

嗨,我只需要把这一行添加到服务器站点?还有什么事情看起来不错吗?@HoángPhúcVũ我不是100%确定你想在代码中实现什么,你说你想发送到一个特定的套接字连接,但我看不到有人试图寻找一个套接字id并发送给他们。我想要的是在两个用户之间发送私人消息。所以你对这个案例有什么建议。谢谢help@Datsik嗨,你能帮我谈谈这个话题吗?请注意:如果使用
socket.broadcast.to(id)
的套接字是自己的(因此,
socket.id==id
),那么这将不起作用。相反,请使用
io.sockets.to(id).emit('event-name-message','this only for your')
。问题清楚地表明,特定的客户端。