【问题标题】:Client Automatically disconnects after some time in websocket客户端在 websocket 中一段时间​​后自动断开连接
【发布时间】:2023-03-25 06:50:01
【问题描述】:

我的桌面应用程序客户端正在通过webSockets 连接到网络服务器。我在客户端使用javaWebSocket api,我使用的是Apache tomcat 7 的webSocketServlet。我不知道我做错了什么,但客户端与网络服务器完美连接并发送聊天消息,但在闲置一段时间后,服务器会自动断开客户端。我希望客户端连接,直到客户端故意不想断开连接。这是我的 web Socket servlet 和客户端的示例代码。首先是客户端代码

Draft[] drafts = { new Draft_10(), new Draft_17(), new Draft_76(), new Draft_75() };
cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) {

                @Override
                public void onMessage( String message ) {
                    ta.append( "got: " + message + "\n" );
                    ta.setCaretPosition( ta.getDocument().getLength() );
                }

                @Override
                public void onOpen( ServerHandshake handshake ) {
                    ta.append( "You are connected to ChatServer: " + getURI() + "\n" );
                    ta.setCaretPosition( ta.getDocument().getLength() );
                }

                @Override
                public void onClose( int code, String reason, boolean wasClean ) {
                    ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
                    System.out.println("the reason for disconnection is ........ "+wasClean);
                                            ta.setCaretPosition( ta.getDocument().getLength() );
                    connect.setEnabled( true );
                    uriField.setEditable( true );
                    draft.setEditable( true );
                    close.setEnabled( false );
                }

                @Override
                public void onError( Exception ex ) {
                    ta.append( "Exception occured ...\n" + ex + "\n" );
                    ta.setCaretPosition( ta.getDocument().getLength() );
                    ex.printStackTrace();
                    connect.setEnabled( true );
                    uriField.setEditable( true );
                    draft.setEditable( true );
                    close.setEnabled( false );
                }
            };

            close.setEnabled( true );
            connect.setEnabled( false );
            uriField.setEditable( false );
            draft.setEditable( false );
            cc.connect();
        } catch ( URISyntaxException ex ) {
            ta.append( uriField.getText() + " is not a valid WebSocket URI\n" );
        }
    } else if( e.getSource() == close ) {
        cc.close();
    }

现在这是 servlet 代码。

private static ArrayList<MyStreamBound> mmiList = new ArrayList<MyStreamBound>();

@Override
protected StreamInbound createWebSocketInbound(String protocol) {
    return new MyStreamBound();
}



private class MyStreamBound extends StreamInbound{
    WsOutbound myoutbound;

     @Override
     public void onOpen(WsOutbound outbound){
         try {
             System.out.println("Open Client.");
             this.myoutbound = outbound;
             mmiList.add(this);
             outbound.writeTextMessage(CharBuffer.wrap("Hello!"));

         } catch (IOException e) {
         e.printStackTrace();
         }
     }


    @Override
    protected void onBinaryData(InputStream arg0) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onTextData(Reader recievedData) throws IOException {
        BufferedReader in = new BufferedReader(recievedData);
        String line = null;
        StringBuilder rslt = new StringBuilder();
        while ((line = in.readLine()) != null) {
            rslt.append(line);
        }
        System.out.println(rslt.toString()); 

        for(MyStreamBound mmib: mmiList){
             CharBuffer buffer = CharBuffer.wrap(rslt.toString());
             mmib.myoutbound.writeTextMessage(buffer);
             mmib.myoutbound.flush();

         }

    }

     @Override
     protected void onClose(int status){
         System.out.println("Close Client."+status);
         mmiList.remove(this);
     }

【问题讨论】:

    标签: java jsp tomcat servlets websocket


    【解决方案1】:

    最后我设法解决了。问题出在tomcat的配置文件上。必须到以下目录ApacheTomcat->Config->server.xml,有一个属性名称为connection time out 默认值为20000Ms(20 Seconds) 改成-1表示无限连接时间。

    【讨论】:

    • 您使用的理想超时值是多少?
    【解决方案2】:

    您可能必须实现一个保持活动的机制。

    通常代理会在一段时间后破坏 websocket 连接。

    你可以看看这个:Atmosphere timeout issue with tomcat 7.0.39

    【讨论】:

    • 来自客户端/服务器端或两者的代理?如果代理破坏了它们,那么如何管理呢?保持活动状态意味着必须定期向服务器发送 ping 消息???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2015-05-30
    • 2022-01-03
    相关资源
    最近更新 更多