Node.JS和Socket.io - 广播返回发件人

时间:2012-11-05 13:12:25

标签: node.js socket.io broadcast

我正在构建一个具有多个客户端和node.js服务器的实时Web应用程序,以处理来自客户端的状态和广播事件更改。我已经实现了Socket.io作为客户端和服务器之间的传输机制。当客户端执行特定操作时,会将此事件发送到服务器,该服务器将广播给网络上的其他客户端。

但是,有时当服务器收到一系列事件时,服务器不会广播它,而是将其发送回发送客户端 - 或者将其发送到所有连接的套接字,包括发送客户。

这是服务器实现的片段,它基本上侦听客户端事件,将它们收集在每2秒广播一次的数组中。

如果您愿意,我可以在客户端实现中包含相关的代码段。

// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io'), users = require('./users');

// Commands to client
var USER_LOGIN = 0,
    USER_LIST = 1,
    USER_CONNECTED = 2,
    USER_DISCONNECTED = 3,
    COMMAND = 4,
    COMMAND_SETNICKNAME = 0,
    COMMAND_POSITION = 1,
    COMMAND_MOUSEDOWN = 2,

var array = [];
var activeUsers = {};


// Start the server at port 8080
var server = http.createServer(function(req, res){ 
    res.writeHead(200,{ 'Content-Type': 'text/html' }); 
});
server.listen(8080);

// Create a Socket.IO instance, passing it our server
var io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(client){ 
    console.log("started");


    // Create periodical which ends a JSON message to the client every 2 seconds
    var interval = setInterval(function() {
        if(array.length >0) {
            console.log("Sending to clients " + array);
        //client.send(JSON.stringify(array));
        client.broadcast.send(JSON.stringify(array));
        array = [];
    }
},2000);

// Success!  Now listen to messages to be received
client.on('message',function(event){ 
        console.log("--Reading event " + event);
        var eventArray = JSON.parse(event);
    for(var i = 0; i < eventArray.length; i++) {
        var dataArray = eventArray[i];
        var userId = parseInt(dataArray[0]);
        var position = 1;

        console.log("current element in array " + dataArray);

        switch (parseInt(dataArray[position++])) {
            case USER_LOGIN: 
                // Log in user with USERID. 
                var username = dataArray[position++];
                console.log("Attempting to log in with user " + username);

                // If valid, send USER_LOGIN broadcast to clients
                if(login(username)) {
                    array.push([0, USER_LOGIN, login(username)]);
                }
                break;

1 个答案:

答案 0 :(得分:1)

io.sockets.on('connection', function(client){ 
    console.log("started");


    // Create periodical which ends a JSON message to the client every 2 seconds
    var interval = setInterval(function() {
        if(array.length >0) {
            console.log("Sending to clients " + array);
        //client.send(JSON.stringify(array));
        client.broadcast.send(JSON.stringify(array));
        array = [];
    }
},2000);

你正在为每个连接创建一个调度程序,这将是一个烂摊子。因为您正在on('connection',内创建间隔 而且我没有看到在某个时间间隔内调用客户端的任何意义,因为您有一种方法来推动某些客户端更改事件的更改。这是socket.io的优点。您可以直接从客户端更改事件中广播另一个侦听器中的更改,而不是使用间隔。

client.on('onSomechangeInClient',function(event){ 
  //update array here..
 client.broadcast.send(JSON.stringify(array));
});
相关问题