websocket自动关闭连接 [英] websocket closing connection automatically

查看:988
本文介绍了websocket自动关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java构建一个基于jetty的嵌入式websocket服务器的应用程序。客户端是谷歌浏览器中默认的websocket实现。只有在连接关闭一段时间后,服务器和客户端之间没有传输时,所有事情都可以正常工作。
我不确定谁在关闭连接:jetty服务器或chrome浏览器。



我想这个解决方案是每x秒,但我打开更好的解决方案。



所以......我的问题是:


  1. 这是websocket协议所要求的,在这种情况下,chrome浏览器正在关闭我的连接?

  2. 这是更多的码头相关,并与websocket协议有关或多或少?在这种情况下,我该如何在码头中禁用此功能?
  3. 是否还有其他问题? >

    感谢

    更新:即使我发送1封邮件/秒仍然关闭连接

    解决方案

    在回答你的第三个问题时:你的客户希望能够处理暂时的网络问题,例如假设用户在休眠的会议之间关闭笔记本电脑,或者网络暂时关闭。



    解决方案是听< onclose 事件,当它们发生时,设置一个客户端超时重新打开连接,比如说第二个:

      function setupWebSocket(){
    this.ws = new WebSocket('wss:// host:port / path');
    this.ws.onerror = ...;
    this.ws.onopen = ...;
    this.ws.onmessage = ...;
    this.ws.onclose = function(){
    setTimeout(setupWebSocket,1000);
    };
    }


    I'm building an application in java that has an embedded websocket server based on jetty. The client is the default websocket implementation in google chrome. Everything is working ok, only if there is no transfer between server and client after a certain time the connection is closed. I'm not sure who's closing the connection: the jetty server or the chrome browser.

    The solution to this I think is to send a message every x seconds, but I'm opened to better solutions.

    SO... my questions are:

    1. Is this something that the websocket protocol requires and in this case the chrome browser is closing my connection?

    2. Is this something that is more jetty related and has more or less to do with the websocket protocol? In this case how do I disable this in jetty?

    3. Is there another problem??

    Thanks

    UPDATE: even if I send 1 message/second still the connection is closed

    解决方案

    In answer to your third question: your client wants to be able to cope with temporary network problems anyway, e.g. let's say the user closes their laptop between meetings which hibernates it, or the network simply goes down temporarily.

    The solution is to listen to onclose events on the web socket client and when they occur, set a client side timeout to re-open the connection, say in a second:

    function setupWebSocket(){
        this.ws = new WebSocket('wss://host:port/path');
        this.ws.onerror = ...;
        this.ws.onopen = ...;
        this.ws.onmessage = ...;
        this.ws.onclose = function(){
            setTimeout(setupWebSocket, 1000);
        };
    }
    

    这篇关于websocket自动关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆