【问题标题】:Swap websocket connections between multiple clients在多个客户端之间交换 websocket 连接
【发布时间】:2014-09-16 02:00:36
【问题描述】:

我正在尝试监控两台计算机,并将它们的信息显示到网站。在服务器中,我使用 javascript websocket,两台计算机将信息发送到服务器 IP。我想在同一页面监控两台计算机的状态和收到的10多条消息,交换信息以显示。

这是我的实际代码,但这样可以同时连接两个套接字。我想用第一个 IP 打开一个套接字,接收 10 条消息,关闭连接并打开第二个 IP。收到 10 条 IP2 的消息后,关闭连接,打开第一个 IP 的套接字并重复该过程。

使用 node.js 更好吗?我从未使用过它,我不知道它的潜力。

有人可以帮助我吗?

对不起,我的英语不好。

var IPs =  ['ws://localhost:9000','ws://localhost:8000']; 

while(1){
    IPs.forEach(function(IPactual){
            var socket = new WebSocket(IPactual);
            console.log(socket.readyState);
            socket.onopen = function() {
                console.log('%c Connected to ' + socket.url, "color:orange ");
            }

            socket.onclose = function() {
                console.log('%c Disconnected from ' + socket.url, "color: red");
            }

            socket.onerror = function(e) {
                console.log('%c Ooops... ' + e, "color: red");
            }

            var messages= 0;
            socket.onmessage = function(e) {
                messages++;
                console.log(messages);

                // WRITE TO HTML THE INFORAMTION RECIVED

                if (messages==10){
                    socket.onclose = function () {}; 
                    socket.close()

                }
            }

    })
}

【问题讨论】:

  • 为什么要关闭/打开连接?为什么不让它们打开呢?
  • 那么,我怎样才能显示我想要的socket消息呢?
  • 任何你想要的方式。它可能就像将消息计数除以 10 并使用 mod 2 来确定数字是偶数还是奇数一样简单。
  • 我不明白如何打印套接字或其他套接字的信息,但是无论如何,如果我有 30 台计算机怎么办?我想要一些可以让我交换 IP 列表的东西。
  • 你能解释一下你想要做什么吗?您的应用程序的目标是什么?

标签: javascript node.js sockets monitor clients


【解决方案1】:

感谢您的帮助,保持打开我这样控制它的套接字:

$(document).ready(function () {

    var n_messages_to_show = 10;
        var messages_control = 0;

        var socket = new WebSocket('ws://localhost:9000');

        socket.onopen = function() {
            console.log('%c Connected to ' + socket.url, "color:orange ");
        }

        socket.onclose = function() {
            console.log('%c Disconnected from ' + socket.url, "color: red");
        }

        socket.onerror = function(e) {
            console.log('%c Ooops... ' + e, "color: red");
        }

        var connections = 0;
        socket.onmessage = function(e) {
            if (messages_control<n_messages_to_show){
                console.log('%c Sending TAD1 monitor information ','background:lightgreen ; color: black');
                messages_control++;
                post_to_web(e);
            }
        }


        var socket2 = new WebSocket('ws://localhost:8000');

        socket2.onopen = function() {
            console.log('%c Connected to ' + socket2.url, "color:orange ");
        }

        socket2.onclose = function() {
            console.log('%c Disconnected from ' + socket2.url, "color: red");
        }

        socket2.onerror = function(e) {
            console.log('%c Ooops... ' + e, "color: red");
        }

        var connections = 0;
        socket2.onmessage = function(e) {
            if (messages_control>n_messages_to_show){
                console.log('%c Sending TAD2 monitor information ','background:lightgreen ; color: black');
                messages_control++;
                post_to_web(e);
            }
            if(messages_control==n_messages_to_show*2){
                messages_control=0;
            }
        }


    post_to_web = function(e){
         //############################################
         //              POST TO WEB
         //############################################
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2015-05-22
    • 2021-06-19
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多