处理浏览器重载socket.io

处理浏览器重载socket.io,第1张

处理浏览器重载socket.io

有一个

socket.on('disconnect', function () { ... });
。所以你可以做

socket.on('disconnect', function () {    setTimeout(function () {         //do something    }, 10000);});

编辑1:

我明白了 因此,也许您应该执行以下 *** 作:

客户:
//right after connectionsocket.emit('register', localstorage.getItem('gameUniqueId'));//somewhere, when game startsvar randomlyGeneratedUID = Math.random().toString(36).substring(3,16) + +new Date;localStorage.setItem('gameUniqueId', randomlyGeneratedUID);
服务器:
io.sockets.on('connection', function (socket) {    var player = null;    socket.on('register', function (data) {        if (data !== null) { //there was something in localstorage if (game.Players.existsUID(data)) {     player = game.Players.getByUID(data);     player.disconnected = false; } else {     //timed out, create new player }        } else { //localStorage is not set, create new player        }    });    socket.on('disconnect', function () {        player.disconnected = true;        setTimeout(function () { if (player.disconnected) player.delete();        }, 10000);    });});


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5476846.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存