在Socket IO中向特定的客户端发送消息

时间:2022-03-22 07:37:48

I'm using Socket IO v1.4.5 and have tried 3 different ways below but dont have any result.

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

client.emit('test', 'hahahaha');
io.sockets.socket(id).emit('test',''hahaha); 
io.sockets.connected[id].emit('test','hahaha');

Here is my server side

这是我的服务器端

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');
});

Any help would be great.Thanks for help.Kind Regard

任何帮助都是好的。谢谢你的帮助。那种认为

3 个解决方案

#1


82  

To send a message to a specific client you need to do it like so:

要向特定客户发送消息,您需要这样做:

socket.broadcast.to(socketid).emit('message', 'for your eyes only');

Here is a nice little cheat sheet for sockets:

这里有一个很好的套接字的小备忘单:

 // 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');

Credit to https://*.com/a/10099325

信贷到https://*.com/a/10099325


The easiest way rather than sending directly to the socket, would be creating a room for the 2 users to use and just send messages freely in there.

最简单的方法不是直接发送到套接字,而是为两个用户创建一个空间,并在那里*地发送消息。

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.

Otherwise, you're going to need to keep track of each individual clients socket connection, and when you want to chat you'll have to look up that sockets connection and emit specifically to that one using the function I said above. Rooms are probably easier.

否则,您将需要跟踪每个客户端套接字连接,当您想要聊天时,您将不得不查找套接字连接,并使用我上面提到的函数专门向该连接发出。房间很可能更容易。

#2


0  

Socket.io Version 2.0.3+

套接字。io版本2.0.3 +

Sending a message to a specific socket

将消息发送到特定的套接字。

    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");
    }

#3


0  

Simply do this

只是这样做

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

#1


82  

To send a message to a specific client you need to do it like so:

要向特定客户发送消息,您需要这样做:

socket.broadcast.to(socketid).emit('message', 'for your eyes only');

Here is a nice little cheat sheet for sockets:

这里有一个很好的套接字的小备忘单:

 // 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');

Credit to https://*.com/a/10099325

信贷到https://*.com/a/10099325


The easiest way rather than sending directly to the socket, would be creating a room for the 2 users to use and just send messages freely in there.

最简单的方法不是直接发送到套接字,而是为两个用户创建一个空间,并在那里*地发送消息。

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.

Otherwise, you're going to need to keep track of each individual clients socket connection, and when you want to chat you'll have to look up that sockets connection and emit specifically to that one using the function I said above. Rooms are probably easier.

否则,您将需要跟踪每个客户端套接字连接,当您想要聊天时,您将不得不查找套接字连接,并使用我上面提到的函数专门向该连接发出。房间很可能更容易。

#2


0  

Socket.io Version 2.0.3+

套接字。io版本2.0.3 +

Sending a message to a specific socket

将消息发送到特定的套接字。

    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");
    }

#3


0  

Simply do this

只是这样做

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