简体   繁体   English

如何更改打开WebSocket的超时?

[英]How do I change the timeout for an opening a WebSocket?

Short and sweet: How do I tell a WebSocket to stop or 'close' after attempting to open after a while? 简短而有趣:尝试过一段时间后,如何告诉WebSocket停止或“关闭”?

I'm currently working on a Kahoot like web app where players can connect with their phones to play. 我目前正在开发类似Kahoot的网络应用程序,玩家可以在其中连接手机。 The game uses completely vanilla JavaScript to run everything in the background and uses WebSockets to communicate with the django-channels game server. 该游戏使用完全原始的JavaScript在后台运行所有内容,并使用WebSockets与django-channels游戏服务器进行通信。 The game plays fine except for in scenarios where a player accidentally locks their phone or switches apps and closes their WebSocket. 除玩家意外锁定手机或切换应用程序并关闭其WebSocket的情况外,游戏都可以正常运行。 I can easily open a new connection but for some reason, in mobile safari, WebSockets take FOREVER to give up connecting. 我可以轻松地打开一个新的连接,但是由于某种原因,在移动浏览器中,WebSocket会永远放弃连接。

For example: 例如:

  1. A user connects on Wifi and opens a WebSocket 用户在Wifi上连接并打开WebSocket
  2. User disconnects from Wifi and drops the WebSocket without calling .close and properly closing the connection 用户断开与Wifi的连接,并丢弃WebSocket而不调用.close并正确关闭连接
  3. User attempts to open new WebSocket without Wifi 用户尝试在没有Wifi的情况下打开新的WebSocket
  4. User then connects to Wifi while previous WebSocket is still trying to connect 然后,用户连接到Wifi,而以前的WebSocket仍在尝试连接

In my testing, the second WebSocket will try to connect for at least a minute before finally giving in and failing. 在我的测试中,第二个WebSocket将尝试连接至少一分钟,直到最终让步并失败。 Only then can I attempt to reconnect again and just exaggerates the whole process of reconnecting the player. 只有这样,我才能尝试重新连接,并夸大了重新连接播放器的整个过程。

So like I asked above: how can I shorten this process? 因此,就像我在上面问过的:如何缩短此过程? Is there a way to make the WebSocket give up sooner or am I doing this the wrong way? 有没有办法让WebSocket早点放弃,还是我做错了呢?

If I need to add any more information, please let me know. 如果我需要添加更多信息,请告诉我。 This is my first time posting. 这是我第一次发贴。

Thanks a lot! 非常感谢!

I do not see a connection timeout mentioned anywhere in the webSocket API specification , nor can I find any mention in any webSocket documentation. 我没有在webSocket API规范的任何地方看到连接超时,也没有在任何webSocket文档中找到任何提及。 So, it appears you might have to implement your own. 因此,看来您可能必须实现自己的。 Here's one way to do that: 这是一种方法:

function connectWebSocket(url, timeout) {
    timeout = timeout || 2000;
    return new Promise(function(resolve, reject) {
        // Create WebSocket connection.
        const socket = new WebSocket(url);

        const timer = setTimeout(function() {
            reject(new Error("webSocket timeout"));
            done();
            socket.close();
        }, timeout);

        function done() {
            // cleanup all state here
            clearTimeout(timer);
            socket.removeEventListener('error', error);
        }

        function error(e) {
            reject(e);
            done();
        }

        socket.addEventListener('open', function() {
            resolve(socket);
            done();
        });
        socket.addEventListener('error', error);
    });
}

// Usage    
connectWebSocket(yourURL, 1000).then(function(socket) {
    socket.send("hello");
    // put other code that uses webSocket here
}).catch(function(err) {
    console.log(err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM