简体   繁体   English

客户端等待websocket的最佳方式是什么?

[英]The best way for a client to wait for a websocket?

I'd like to connect to a server, then do some stuff as soon as the connection is open. 我想连接到服务器,然后在连接打开后立即执行一些操作。 But if the connection stalls, I want to trap for that and not do the stuff, and perhaps cancel the waiting connection. 但是,如果连接停止,我想陷阱,而不是做的东西,也许取消等待连接。

function doStuff () {
    var connection = new WebSocket('wss://someURL');

    //do some stuff here as soon as socket open but trap for stall
}

I was looking for some feature such as 我正在寻找一些功能,如

connection.addEventListener('timeout',...);

because upon configuring my WS server to not respond (simulate a too slow server), Chrome's network inspector perpetually shows the connection as "Pending". 因为在配置我的WS服务器不响应(模拟太慢的服务器)时,Chrome的网络检查员会永久地将连接显示为“待定”。 For lack of that feature, my first pass is: 由于缺乏该功能,我的第一个传递是:

function doStuff () {
    var connection = new WebSocket('wss://someURL');
    connection.addEventListener('open', onOpen, false);

    var socketTimer = setTimeout(onNotResponding, 10000);

    function onOpen () {
        clearTimeout(socketTimer);
        //do my stuff here.
    }

    function onNotResponding () { 
        //the server is not responding, how do I "cancel" the connection request here? 
    }
}

(This is an answer to the question: the best way to wait for a websocket ... to become available, which I thought was the intention of this post. Anyway, I'm leaving my answer here, perhaps other readers will find it useful). (这是一个问题的答案:等待websocket变得可用的最佳方式,我认为这是本帖的目的。无论如何,我在这里留下我的答案,或许其他读者会发现它有用)。

Here is how I solved it: 这是我解决它的方式:

var socket;

var socketOnMessage = function(msg) {
    console.log("received " + msg.data);
};

var socketOnOpen = function(msg) {
    console.log("websocket opened");
};

var socketOnClose = function(msg) {
    console.log('websocket disconnected - waiting for connection');
    websocketWaiter();
};

function websocketWaiter(){
    setTimeout(function(){
        socket = new WebSocket(websocketUrl);
        socket.onopen = socketOnOpen;
        socket.onclose = socketOnClose;
        socket.onmessage = socketOnMessage;
    }, 1000);
};

websocketWaiter();

In the onClose event handler, you call the websocketWaiter again. 在onClose事件处理程序中,再次调用websocketWaiter。

In websocketWaiter, you must re-initialize the event handlers, because you created a new object. 在websocketWaiter中,您必须重新初始化事件处理程序,因为您创建了一个新对象。

A quick google search showed this. 快速谷歌搜索显示了这一点。
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#close%28%29 https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#close%28%29

connection.close();

Just to note that the pending connection you see in Chrome Developer Tools is the initial http connection which is then left open to allow for the web socket to have bidirectional communication. 请注意,您在Chrome开发者工具中看到的待处理连接是初始的http连接,然后保持打开以允许Web套接字进行双向通信。 You will not see the real websocket traffic. 你不会看到真正的websocket流量。

For that you can install the free WireShark network traffic sniffer looking at this solution here: 为此,您可以在此处安装免费的WireShark网络流量嗅探器,查看此解决方案:

How to debug websockets with wireshark 如何使用wireshark调试websockets

You can also filter to show only WebSocket packets by using a display filter: (websocket) 您还可以使用显示过滤器过滤以仅显示WebSocket数据包:(websocket)

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

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