javascript Socket.IO 处理断开事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17287330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket.IO handling disconnect event
提问by Raggaer
Cant handle this disconnect event, dont know why socket its not send to the client / client doesnt response!
无法处理这个断开连接事件,不知道为什么套接字不发送到客户端/客户端没有响应!
Server
服务器
io.sockets.on('connection', function (socket) {
socket.on('NewPlayer', function(data1) {
online = online + 1;
console.log('Online players : ' + online);
console.log('New player connected : ' + data1);
Players[data1] = data1;
console.log(Players);
});
socket.on('DelPlayer', function(data) {
delete Players[data];
console.log(Players);
console.log('Adios' + data);
});
socket.on('disconnect', function () {
socket.emit('disconnected');
online = online - 1;
});
});
Client
客户
var socket = io.connect('http://localhost');
socket.on('connect', function () {
person_name = prompt("Welcome. Please enter your name");
socket.emit('NewPlayer', person_name);
socket.on('disconnected', function() {
socket.emit('DelPlayer', person_name);
});
});
As you can see when a client disconnects the Array object[person_name] should be deleted, but its not
如您所见,当客户端断开连接时,应该删除 Array object[person_name],但它不是
回答by code-jaff
Ok, instead of identifying players by name track with sockets through which they have connected. You can have a implementation like
好的,而不是通过名称跟踪带有连接的套接字来识别玩家。你可以有一个像
Server
服务器
var allClients = [];
io.sockets.on('connection', function(socket) {
allClients.push(socket);
socket.on('disconnect', function() {
console.log('Got disconnect!');
var i = allClients.indexOf(socket);
allClients.splice(i, 1);
});
});
Hope this will help you to think in another way
希望这能帮助你换一种方式思考
回答by T J
For those like @sha1 wondering why the OP's code doesn't work -
对于像@sha1 这样想知道为什么 OP 的代码不起作用的人 -
OP's logic for deleting player at server side is in the handler for DelPlayer
event,
and the code that emits this event (DelPlayer
) is in inside disconnected
event callback of client.
OP 在服务器端删除播放器的逻辑在DelPlayer
事件的处理程序中,而发出此事件 ( DelPlayer
)的代码在disconnected
客户端的内部事件回调中。
The server side code that emits this disconnected
event is inside the disconnect
event callback which is fired when the socket loses connection. Since the socket already lost connection, disconnected
event doesn't reach the client.
发出此disconnected
事件的服务器端代码位于disconnect
事件回调中,当套接字失去连接时会触发该回调。由于套接字已经失去连接,disconnected
事件不会到达客户端。
Accepted solution executes the logic on disconnect
event at server side, which is fired when the socket disconnects, hence works.
接受的解决方案disconnect
在服务器端的事件上执行逻辑,当套接字断开连接时会触发该逻辑,因此可以工作。
回答by Alexander Mills
Create a Map or a Set, and using "on connection" event set to it each connected socket, in reverse "once disconnect" event delete that socket from the Map we created earlier
创建一个 Map 或一个 Set,并使用“on connection”事件设置给每个连接的套接字,相反的“一旦断开”事件从我们之前创建的 Map 中删除该套接字
import * as Server from 'socket.io';
const io = Server();
io.listen(3000);
const connections = new Set();
io.on('connection', function (s) {
connections.add(s);
s.once('disconnect', function () {
connections.delete(s);
});
});
回答by www-data
You can also, if you like use socket id to manage your player list like this.
你也可以,如果你喜欢使用套接字 id 来管理你的玩家列表。
io.on('connection', function(socket){
socket.on('disconnect', function() {
console.log("disconnect")
for(var i = 0; i < onlineplayers.length; i++ ){
if(onlineplayers[i].socket === socket.id){
console.log(onlineplayers[i].code + " just disconnected")
onlineplayers.splice(i, 1)
}
}
io.emit('players', onlineplayers)
})
socket.on('lobby_join', function(player) {
if(player.available === false) return
var exists = false
for(var i = 0; i < onlineplayers.length; i++ ){
if(onlineplayers[i].code === player.code){
exists = true
}
}
if(exists === false){
onlineplayers.push({
code: player.code,
socket:socket.id
})
}
io.emit('players', onlineplayers)
})
socket.on('lobby_leave', function(player) {
var exists = false
for(var i = 0; i < onlineplayers.length; i++ ){
if(onlineplayers[i].code === player.code){
onlineplayers.splice(i, 1)
}
}
io.emit('players', onlineplayers)
})
})