Nodejs - websocket-node module: How to make multi-client socket-server works?

五迷三道 提交于 2019-12-06 12:38:44

问题


I created a socket server using websocket module with this configuration taken from this example (with some changes):

    var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(5050, function() {
    console.log((new Date()) + ' Server is listening on port 5050');
});

wsServer = new WebSocketServer({
    httpServer: server,
    // You should not use autoAcceptConnections for production 
    // applications, as it defeats all standard cross-origin protection 
    // facilities built into the protocol and the browser.  You should 
    // *always* verify the connection's origin and decide whether or not 
    // to accept it. 
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  // put logic here to detect whether the specified origin is allowed. 
  return true;
}

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      // Make sure we only accept requests from an allowed origin 
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }

    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        }
        else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
            connection.sendBytes(message.binaryData);
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

I create my own client in html :

<html>
    <head>
        <script src='./js/jquery1-11-3-min.js'></script>
        <script>
            $(document).ready(function (){
                buildwebsocket();
            });
            var ws;
            function buildwebsocket(){
                ws = new WebSocket("ws://192.168.0.96:5050",'echo-protocol');
                ws.onopen = function(evt) { onOpen(evt) };
                ws.onclose = function(evt) { onClose(evt) };
                ws.onmessage = function(evt) { onMessage(evt) };
                ws.onerror = function(evt) { onError(evt) };
            }
            function onOpen(ev){
                //alert("konek men! mantap! :D");
                $("#recmsg").append("connected!<br>");
            }
            function onClose(ev){
                $("#recmsg").append("connection closed!<br>");
            }
            function onMessage(ev){
                //alert("ada pesan datang!");
                $("#recmsg").append(ev.data+"<br>");
            }
            function onError(ev){
                $("#recmsg").append("connecting error!<br>");
            }
            function doSend(){
                //writeToScreen("SENT: " + message); 
                var message = $("#pesan").val();
                ws.send(message);
            } function doClose(){
                ws.close();
            }
            //function writeToScreen(message){
                //var pre = document.createElement("p");
                //pre.style.wordWrap = "break-word";
                //pre.innerHTML = message;
                //output.appendChild(pre);
            //}
            //window.addEventListener("load", init, false);
        </script>
    </head>
    <body>
        <button onclick='doClose()'>Close</button>
        <textarea id='pesan'></textarea><br>
        <button onclick='doSend()'>Kirim!</button>
        <br>
        received message
        <div id='recmsg'>
        </div>  
    </body>
</html>

The connection between client (first client) and the server was successfully established. I try to send messages from first client, then the server receives the message without any promblem, and then the message sent back to the first client, and the first client receives it. I can say the connection and the socket works well.
I try to establish another connection (second client), so I open the second client in another device. The connection is good. But, when I send messages from the first or the second client, the first client doesn't get the response but the second client gets it.
And if open the third client and then send a message, the first and the second client don't get the response. Only the last connected client receives the response from server, and there's no client receives any error messages.
Is it the cons of the module? or the server configuration must be changed/added?
Can I establish multi-client-supported-socket-server using this module?


回答1:


You're not storing the connections on the server side. You're just setting them up on the server to communicate directly back and forth to the server. If you want messages going to the server to be sent back out to everyone, you need to set up the on.('message', ...) function for each connection on the server to have that behavior. To do this, you'll need to store the connections as they are created. Try this:

var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(5050, function() {
    console.log((new Date()) + ' Server is listening on port 5050');
});

wsServer = new WebSocketServer({
    httpServer: server,
    // You should not use autoAcceptConnections for production 
    // applications, as it defeats all standard cross-origin protection 
    // facilities built into the protocol and the browser.  You should 
    // *always* verify the connection's origin and decide whether or not 
    // to accept it. 
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  // put logic here to detect whether the specified origin is allowed. 
  return true;
}

//create an array to hold your connections
var connections = [];

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      // Make sure we only accept requests from an allowed origin 
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }
    var connection = request.accept('echo-protocol', request.origin);

    //store the new connection in your array of connections
    connections.push(connection);

    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);

            //send the received message to all of the 
            //connections in the connection array
            for(var i = 0; i < connections.length; i++) {
                connections[i].sendUTF(message.utf8Data);
            }
        }
        else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
            connection.sendBytes(message.binaryData);
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});


来源:https://stackoverflow.com/questions/32960956/nodejs-websocket-node-module-how-to-make-multi-client-socket-server-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!