2013-10-29 109 views
2

我正在設置一個非常基本的節點應用程序,計劃是任何人都可以在網站上按下一個字母,然後遞增計數器。隨着世界各地的人們都在增加它,每個人都可以看到這種情況正在上升。 它的功能非常出色,除了我在開始時設置它的方式之後,節點服務器每秒接觸每個客戶端20次,這非常浪費。Nodejs,客戶端並不總是收到服務器消息

所以爲了避免這種情況,我添加了一個條件,即計數器必須增加以便節點服務器發送消息。但是當我這樣做的時候,這些信息從來沒有真正地讓所有的客戶知道,他們如何去客戶端是一些混亂,顯然是隨機的順序。

這是我的第一個節點應用程序,我仍然得到它的竅門,任何人都可以用這個指向正確的方向嗎?問題可能在於increment_time函數。

服務器部分:

var http=require('http'); 
var io=require('socket.io'); 
var fs=require('fs'); 

var sockFile=fs.readFileSync('text'); 
server=http.createServer(); 

//2. get request, via port, http://localhost:8000 
server.on('request', function(req, res){ 
    console.log('request received'); 
    res.writeHead(200, {'content-type':'text/html'}); 
    res.end(sockFile); 
    }); 

//have server and socket listen to port 8000. 
server.listen(8000); 
var socket=io.listen(server); 

//the lower the number the less chatty the debug becomes. 
socket.set('log level', 1); 

//initiate the counter. 
counter=0; 
counterp=0; 


//3. on client connect. 
//http://<address>/apps/press_k 
socket.on('connection', function(socket){ 
    console.log("Client connected."); 

    //increment counter on clients command. 
    socket.on('s_increment_counter',function(data){ 
    counter++; 
    console.log('New counter:'+counter); 
    }); 

//send the counter to all clients if it has increased. 
increment_time_continuously(); 
function increment_time(){ 
    //this is the condition that is to limit how often node sends to the clients, 
    //but setting it on means the counter will be delivered in some erratic way 
    //to the clients. 
    //if(counter===counterp) {return;} 

    //send current counter to all clients. 
    console.log("passed the equals, now broadcast."); 
    //this part is clearly only sending to one of them. 
    socket.emit('c_display_counter', counter); 
    //client.broadcast.emit('c_display_counter', counter) 
    //client.send('Welcome client'); 

    counterp=counter; 
    } 
function increment_time_continuously(){setInterval(increment_time,50);} 
}); 

相關的客戶端部分:

//client_to_server: 
//-tell server to increment counter. 
function s_increment_counter(){ 
    socket.emit('s_increment_counter',{counter:0}); 
    } 

//server_to_client: 
//-when server tells to display counter, display counter. 
socket.on('c_display_counter',function(counter){ 
    //counter=data["counter"]; 
    k_counter_text.attr({"text":counter}); 
    }); 

回答

1

的問題是,你不保存您的連接,當你與socket.emit廣播只能進入你的最後一個連接,你需要存儲已經連接到套接字的每個連接(在一個數組或其他東西中),然後遍歷所有連接並廣播你想要的消息。

var http=require('http'); 
var io=require('socket.io'); 
var fs=require('fs'); 

var sockFile=fs.readFileSync('text'); 
server=http.createServer(); 

//2. get request, via port, http://localhost:8000 
server.on('request', function(req, res){ 
    console.log('request received'); 
    res.writeHead(200, {'content-type':'text/html'}); 
    res.end(sockFile); 
    }); 

//have server and socket listen to port 8000. 
server.listen(8000); 
var socket=io.listen(server); 

//the lower the number the less chatty the debug becomes. 
socket.set('log level', 1); 

//initiate the counter. 
counter=0; 
counterp=0; 
connections = {}; 




function broadcastToConnections(){ 
    for(c in connections){ 
    var con = connections[c]; 
     con.emit('c_display_counter', counter); 
    } 
} 

function connectionClose(socket){ 
    delete connections[socket.id]; 
} 


//3. on client connect. 
//http://<address>/apps/press_k 
socket.on('connection', function(socket){ 
    console.log("Client connected."); 

    //increment counter on clients command. 
    socket.on('s_increment_counter',function(data){ 
     counter++; 
     console.log('New counter:'+counter); 

     broadcastToConnections(); 
    }); 

    connections[socket.id] = socket;//here we store our connection in connections obj 

    socket.on('close', connectionClose);//we listen to close event to remove our connection from connection obj 

}); 
相關問題