gpt4 book ai didi

javascript - Nodejs,客户端并不总是收到服务器消息

转载 作者:行者123 更新时间:2023-11-30 05:43:04 26 4
gpt4 key购买 nike

我正在设置一个非常基本的 Node 应用程序,计划是任何人都可以访问该站点并按一个字母,这将增加一个计数器。每个人都可以看到随着全世界的人们都在增加它,计数器在上升。它工作得很好,除了我一开始设置它的方式然后 Node 服务器每秒联系每个客户端 20 次,这是非常浪费的。

因此,为了避免我添加了一个条件,即计数器必须增加才能让 Node 服务器发送消息。但是,当我这样做时,消息并没有真正发送给所有客户,它们发送给客户的方式有些困惑,显然是随机顺序。

这是我的第一个 Node 应用程序,我仍在掌握它的窍门,任何人都可以用它指出正确的方向吗?问题可能出在 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});
});

最佳答案

问题是你不存储你的连接,当你使用 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

});

关于javascript - Nodejs,客户端并不总是收到服务器消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19662894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com