当前位置: 首页 > 面试题库 >

socket.io和node.js将消息发送到特定客户端

宗波涛
2023-03-14
问题内容

向所有客户端发送消息效果很好,但是我想向特定的用户名发送消息。我的server.js文件看起来像。它的作用是在http://localhost:8080运行时,客户端代码将用户添加到对象用户名以及套接字对象中。并立即将单个消息返回给每个连接的客户端。

//var io = require('socket.io').listen(8080);
var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')
var usernames={};  
 app.listen(8080);

// on server started we can load our client.html page
function handler ( req, res ) {
  fs.readFile( __dirname + '/client.html' ,
  function ( err, data ) {
    if ( err ) {
      console.log( err );
      res.writeHead(500);
      return res.end( 'Error loading client.html' );
    }
    res.writeHead( 200 );
    res.end( data );
  });
};
io.set('log level', 1); // reduce logging
io.sockets.on('connection', function (socket) {

 socket.on('adduser', function(username){
        // store the username in the socket session for this client
        socket.username = username;
        // add the client's username to the global list
        usernames[username] = username;
        // send client to room 1
        console.log(username+' has connected to the server');
        // echo to client they've connected
  });

  socket.on('pmessage', function (data) {
        // we tell the client to execute 'updatechat' with 2 parameters
        io.sockets.emit("pvt",socket.username,data+socket.username); // This works
        io.sockets.socket(socket.username).emit("pvt",socket.username,data+socket.username); // THIS DOESNOT
   });

  socket.on('disconnect', function(){
        // remove the username from global usernames list
        delete usernames[socket.username];

        // echo globally that this client has left
       console.log(socket.username + ' has disconnected');
    });
});

发出消息的部分

socket.on('pmessage', function (data) {
        // we tell the client to execute 'updatechat' with 2 parameters
        io.sockets.emit("pvt",socket.username,data+socket.username); // This works and sends message to all clients
        io.sockets.socket(socket.username).emit("pvt",socket.username,data+socket.username); // THIS DOESNOT
   });

问题答案:

试试这个:

socket.on('pmessage', function (data) {
    // we tell the client to execute 'updatechat' with 2 parameters
    io.sockets.emit("pvt",socket.username,data+socket.username);
    io.sockets.socket(socket.id).emit("pvt",socket.username,data+socket.username); 
});

socket.id由socket.io保存,并且包含客户端的唯一ID。您可以使用以下命令进行检查:

console.log(socket.id);


 类似资料:
  • 问题内容: 我正在使用socket.io和node.js,直到现在看起来还不错,但我不知道如何从服务器向特定客户端发送消息,如下所示: 但是无论是还是方法似乎都无法满足我的需求。 我发现一种可能的解决方案是,该方法将不发送消息的SessionId数组作为第二个参数,因此我可以将此时连接了所有SessionId的数组传递给服务器,除了那个我希望发送邮件,但是我觉得必须有一个更好的解决方案。 有任何想

  • 在socket.io中,如果要将消息发送到特定的房间,通常在服务器端使用特定的语法:。 但是客户机(我指的是在浏览器中运行的socket.io相关代码)如何指示消息应该发送到特定的房间? 还是socket.io客户端库也为此目的提供了特定的语法? 编辑:澄清一下,我上面的建议似乎起作用了,我只是不确定是否有更好的解决方案。

  • 问题内容: 我从socket.io + node.js开始,我知道如何在本地发送消息和广播功能:-所有连接的客户端都收到相同的消息。 现在,我想知道如何向特定的客户端发送私人消息,我的意思是一个套接字,用于2个人之间的私人聊天(客户端到客户端流)。谢谢。 问题答案: 当用户连接时,它应使用唯一的用户名(例如电子邮件)向服务器发送消息。 一对用户名和套接字应存储在这样的对象中: 在客户端上,使用以下

  • 我想发送数据到一个特定的套接字ID。 在旧版本中,我们曾经能够做到这一点: 我将如何在Socket.io1.0中执行类似的操作?

  • 问题内容: 我试图通过向服务器发送自定义事件来使Python客户端使用Socket.io 0.7与Node.js服务器通信。 基于我在GitHub上找到的Socket.io参考以及以下WebSocket Python库。 到目前为止,这是我的代码: 节点服务器 Python客户端 节点控制台输出 Python控制台输出 现在看来,尽管服务器以ACK响应,套接字事件仍未触发。因此,该消息已正确接收,

  • 问题内容: 我是Go的新手,并且发现自己作为第一个项目使用套接字。这是一个多余的问题,但是我无法理解如何将Websocket更新发送到Go中的特定客户端(使用Gorilla)。 我要解决的主要问题是-使用websocket和搜索引擎(如ES / Lucene)建立预输入。我在搜索引擎上维护了一堆索引,并在其周围进行了Go包装。当我开始在Go中使用websockets时,几乎发现了所有展示广播机制的