交换多个客户端之间的websocket连接

时间:2014-07-24 13:23:30

标签: javascript node.js sockets monitor clients

我正在尝试监控两台计算机,并将其信息显示在网站上。在服务器中,我使用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()

                }
            }

    })
}

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,保持打开插座我控制它如下:

$(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
         //############################################
    }
});