使用webSocket将消息发送到特定的连接用户? [英] Sending message to a specific connected users using 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 当服务器想要发送消息给John: 请注意,此处没有任何asp.net服务器代码(在消息机制中)。只有NodeJs。 问题: 怎么走? (或者我错过了什么?) 这不仅是正确的路要走,而且是唯一的途径。基本上每个连接都需要一个唯一的ID。否则,您将无法识别它们,就像这样简单。 现在,您将如何表现它是另一回事。使用 还要记住,如果您想要用户之间进行通信,那么您也必须发送目标用户的ID,即当用户A想要向用户B发送消息时,显然A必须知道B的ID。 I wrote a code for broadcasting a message to all users : The Used code : (brief) Please notice : 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 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. 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 : 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 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屋! {UserID:6,value:{connectionObject}} $>
$ hr
$ b
id
和 connection
属性创建一个对象是一个很好的方法来做到这一点(我肯定会去做的)。您还可以将 id
直接附加到连接对象。
// 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;
...
array
.
userId
and the connection
object.
{UserID:"6" , value :{connectionObject}}
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.