使用webSocket将消息发送到特定的连接用户? [英] Sending message to a specific connected users using webSocket?

查看:2065
本文介绍了使用webSocket将消息发送到特定的连接用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码,用于向所有用户广播消息:



使用过的代码: (简单)

  // websocket和http服务器
var webSocketServer = require('websocket' )。服务器;

...
...
var clients = [];

var server = http.createServer(function(request,response){
//对我们来说不重要,我们正在编写WebSocket服务器,而不是HTTP服务器
});
server.listen(webSocketsServerPort,function(){
...
});

var wsServer = new webSocketServer({
// WebSocket服务器绑定到HTTP服务器。
httpServer:server
});


//每次有人
//尝试连接到WebSocket服务器时调用此回调函数
wsServer.on('request',function(request ){
...
var connection = request.accept(null,request.origin);
var index = clients.push(connection) - 1;
...

请注意:


  • 我没有任何用户引用,只有一个连接。 所有用户连接都存储在数组中。



目标:假设NodeJs服务器想要将消息发送到特定客户(约翰)。



以下是问题:




  • NodeJs服务器如何知道John有哪些连接?



    NodeJs服务器甚至不知道John。看到的是连接。




所以,
我相信现在,我不应该仅通过它们的连接来存储用户,而是需要存储对象 - 它将包含 userId 连接对象。



这是我的想法:




  • 当NodeJs服务器接受连接时 - 生成一个唯一的字符串并将其发送到客户端浏览器。将用户连接和唯一字符串存储在对象中。 eg {UserID:6,value:{connectionObject}}

  • <在客户端,当这个消息到达时 - 将其存储在隐藏字段或cookie中。 (用于将来对NodeJs服务器的请求)


    $ hr
    $ b

    当服务器想要发送消息给John:




    • 在字典中查找john的UserID并通过相应的连接发送消息。 b $ b

      请注意,此处没有任何asp.net服务器代码(在消息机制中)。只有NodeJs。

      问题:

      怎么走? (或者我错过了什么?)

      解决方案

      这不仅是正确的路要走,而且是唯一的途径。基本上每个连接都需要一个唯一的ID。否则,您将无法识别它们,就像这样简单。



      现在,您将如何表现它是另一回事。使用 id connection 属性创建一个对象是一个很好的方法来做到这一点(我肯定会去做的)。您还可以将 id 直接附加到连接对象。



      还要记住,如果您想要用户之间进行通信,那么您也必须发送目标用户的ID,即当用户A想要向用户B发送消息时,显然A必须知道B的ID。


      I wrote a code for broadcasting a message to all users :

      The Used code : (brief)

      // websocket and http servers
      var webSocketServer = require('websocket').server;
      
      ...
      ...
      var clients = [ ];
      
      var server = http.createServer(function(request, response) {
          // Not important for us. We're writing WebSocket server, not HTTP server
      });
      server.listen(webSocketsServerPort, function() {
        ...
      });
      
      var wsServer = new webSocketServer({
          // WebSocket server is tied to a HTTP server. 
          httpServer: server
      });
      
      
          // This callback function is called every time someone
          // tries to connect to the WebSocket server
          wsServer.on('request', function(request) {
          ...
          var connection = request.accept(null, request.origin); 
          var index = clients.push(connection) - 1;
          ...
      

      Please notice :

      • I don't have any user reference but only a connection .
      • All users connection are stored in an array.

      Goal : let's say that the NodeJs server wants to send a message to a specific client (John).

      And here is the question :

      • How would the NodeJs server know which connection does John have ?

        The NodeJs server doesn't even know John. all it sees is the connections.

      So , I believe that now , I shouldn't store users only by their connection ,Instead , I need to store an object -- which will contain the userId and the connection object.

      And here is my idea :


      • When the page finish loading (Dom ready) - establish a connection to the NodeJs server.

      • When the NodeJs server accept a connection - generate a unique string and send it to the client browser. Store the user connection and the unique string in an object. e.g. {UserID:"6" , value :{connectionObject}}

      • At client side , when this message arrives - store it in a hidden field or cookie. (for future requests to the NodeJs server )


      When the server wants to send a message to John :

      • Find john's UserID in the dictionary and send a message by the corresponding connection.

      please notice there is no asp.net server code invloced here (in the message mechanism). only NodeJs .

      Question :

      Is this the right way to go ? (or have I missed something ?)

      解决方案

      This is not only the right way to go, but the only way. Basically each connection needs a unique ID. Otherwise you won't be able to identify them, it's as simple as that.

      Now how you will represent it it's a different thing. Making an object with id and connection properties is a good way to do that ( I would definitely go for it ). You could also attach the id directly to connection object.

      Also remember that if you want communication between users, then you have to send target user's ID as well, i.e. when user A wants to send a message to user B, then obviously A has to know the ID of B.

      这篇关于使用webSocket将消息发送到特定的连接用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆