今天遇到socket.io的问题.当我刷新页面时.on("connect",{})永远不会触发.当我从网址加载页面时,所有事件都会按照应有的方式启动.如下所示,我将gameStatus设置为已损坏.在一系列客户端/服务器通信之后,游戏状态应更新为"就绪",并且应在服务器上创建新的播放器.当我重新加载页面(cmd + r)时,没有任何客户端事件触发.
服务器端识别新连接"看到"新的socket.id并发出"发送房间"事件,但是在客户端没有收到该事件.
我试图强制新连接,但我不知道我是否正确使用它.
上周这按照我的预期工作,然后今天甚至没有更新代码,它无法在页面刷新时触发这些事件.如果我从完整的URL加载页面(http://localhost:3000在这种情况下),一切正常
index.html的:
var io = io.connect('/', {'force new connection': true});
Run Code Online (Sandbox Code Playgroud)
client.js
var gameStatus = 'broken';
var localPlayer;
window.onload = function() {
console.log(gameStatus); // "broken"
io.on("connect", onSocketConnected);
io.on("disconnect", onSocketDisconnect);
io.on("new player", onNewPlayer);
io.on("send room", function(data){
console.log("send room fired"); // doesn't fire on refresh
addPlayer(data);
});
}
function onSocketConnected() {
console.log("Connected to socket server "); // doesn't connect on refresh
// assign socket.sessionid as the player name
localPlayer.id = io.socket.sessionid;
io.emit("new player", {id: localPlayer.id});
console.log(localPlayer.id); …Run Code Online (Sandbox Code Playgroud)