MongoDB
 sql >> база данни >  >> NoSQL >> MongoDB

Nodejs - модул websocket-node:Как да накарам мултиклиентския сокет-сървър да работи?

Не съхранявате връзките от страната на сървъра. Вие просто ги настройвате на сървъра, за да комуникират директно напред и назад към сървъра. Ако искате съобщенията, отиващи към сървъра, да се изпращат обратно до всички, трябва да настроите .on('message', ...) функция за всяка връзка на сървъра, за да има това поведение. За да направите това, ще трябва да съхранявате връзките, когато са създадени. Опитайте това:

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.');
    });
});



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Как Mongolab REST API удостоверява

  2. mongodb c# изберете специфично поле точка нотация

  3. Не може да се удостовери в mongo, удостоверяването е неуспешно

  4. Външни ключове в монго?

  5. Създаване на първи потребител в MongoDB 3.2.