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

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

问题描述

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

//websocket 和 http 服务器var webSocketServer = require('websocket').server;......var 客户 = [ ];var server = http.createServer(function(request, response) {//对我们来说不重要.我们正在编写 WebSocket 服务器,而不是 HTTP 服务器});server.listen(webSocketsServerPort,函数(){...});var wsServer = new webSocketServer({//WebSocket 服务器绑定到 HTTP 服务器.httpServer:服务器});//每次有人时都会调用这个回调函数//尝试连接到 WebSocket 服务器wsServer.on('请求',函数(请求){...var connection = request.accept(null, request.origin);var index = clients.push(connection) - 1;...

请注意:

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

目标:假设 Node.js 服务器想要向特定客户端 (John) 发送消息.NodeJs 服务器如何知道 John 有哪个连接?Node.js 服务器甚至不认识 John.它所看到的只是连接.

所以,我相信现在,我不应该只通过他们的连接来存储用户,相反,我需要存储一个对象,该对象将包含 userIdconnection对象.

想法:

  • 当页面完成加载(DOM 就绪)时 - 建立与 Node.js 服务器的连接.

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

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


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

  • 在字典中找到john的UserID,通过对应的连接发送消息.

  • 请注意这里(在消息机制中)没有invloced asp.net 服务器代码.只有 NodeJs .*

问题:

这是正确的方式吗?

解决方案

这不仅是正确的方法,而且是唯一的方法.基本上每个连接都需要一个唯一的 ID.否则你将无法识别它们,就这么简单.

现在你将如何表示它是另一回事.使用 idconnection 属性创建一个对象是一个很好的方法(我肯定会去做).您也可以将 id 直接附加到连接对象.

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

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

// 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 Node.js server wants to send a message to a specific client (John). How would the NodeJs server know which connection John has? The Node.js 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.

Idea:

  • When the page finishes loading (DOM ready) - establish a connection to the Node.js server.

  • When the Node.js 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?

解决方案

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天全站免登陆