简体   繁体   English

使用客户端ID和websocket和node.js将数据发送到某些客户端

[英]Send data to certain client using client ID with websocket and node.js

I am creating customer service chat application that get data from client website to node.js server then send this data to agent and the agent reply to the client.. 我正在创建客户服务聊天应用程序,该应用程序将从客户端网站获取数据到node.js服务器,然后将此数据发送给代理,然后代理将回复发送给客户端。

Server code: 服务器代码:

var ws = require("nodejs-websocket");
var clients = [];
var server = ws.createServer(function(conn){
    console.log("New Connection");

    //on text function
    conn.on("text", function(str){

        var object = JSON.parse(str);
        conn.sendText("Message send : " + object);
        console.log("User ID: " + object.id);


        clients.push(object.id);

        var unique=clients.filter(function(itm,i,a){
            return i==a.indexOf(itm);
        });
        /*
        conn.on('message', function("test") {
            console.log('message sent to userOne:', message);
            unique[0].send("Message: " + message);
        });
        */
        console.log("Number of connected users : " + unique.length);

        //closing the connection
        conn.on("close", function(){
            console.log("connection closed");

        });
    });



}).listen(process.env.PORT, process.env.IP);

Everything works perfectly and I have each client ID but the I want to reply with a message to that client ID.. 一切正常,我有每个客户端ID,但是我想向该客户端ID回复一条消息。

What I have tried: 我尝试过的

I have tried to reply to the client using conn.send("message", callBackFunction) but it send to all not with a specified user ID. 我尝试使用conn.send("message", callBackFunction)答复客户端conn.send("message", callBackFunction)但它不使用指定的用户ID发送给所有人。

Disclaimer: co-founder of Ably - simply better realtime 免责声明: Ably的共同创始人-更好的实时性

You have two problems there I suspect. 我怀疑那里有两个问题。 Firstly, if you ever need to scale to more than one server you've got problems as you will need to figure out how to pass messages between servers. 首先,如果您需要扩展到多个服务器,则会遇到问题,因为您将需要弄清楚如何在服务器之间传递消息。 Secondly, you have no way of maintaining state between disconnections which will happen as part of normal behaviour for clients . 其次,您无法维护断开连接之间的状态,而断开连接是客户端正常行为的一部分

The industry typically approaches this type of problem using the concept of channels as it scales, it decouples the publisher from the subscriber, and it's quite a simple concept to work with. 行业通常会在规模扩大时使用渠道概念来解决此类问题,它将发布者与订阅者分离开来,这是一个非常简单的概念。 For example, if you had a channel called "client:1" and you published to that channel, and your subscriber was listening on that channel, then they would receive the message. 例如,如果您有一个名为“ client:1”的频道并发布到该频道,而您的订阅者正在该频道上收听,那么他们将收到该消息。 You can find out more about how we have designed our realtime service around channels , I would suggest you do consider that pattern in your system. 您可以找到有关我们如何设计各种渠道的实时服务的更多信息,建议您在系统中考虑该模式。

Matt, co-founder, Ably Matt, Ably联合创始人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM