4

デスクトップ アプリ クライアントが を介して Web サーバーに接続していますwebSocketsjavaWebSocketクライアント側に api を使用しており、Apache tomcat 7 を使用していますwebSocketServlet。何が間違っているのかわかりませんが、クライアントはWebサーバーと完全に接続してチャットメッセージを送信していますが、しばらくアイドル状態になった後、サーバーはクライアントを自動的に切断します. クライアントが意図的に切断したくないまで、クライアントを接続したい。SocketWebサーブレットとクライアントのサンプル コードを次に示します。最初はクライアントコードです

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();
    }

これがサーブレット コードです。

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);
     }
4

2 に答える 2

3

最後に私はそれを解決することができました。問題は tomcat の構成ファイルにありました。次のディレクトリ ApacheTomcat->Config->server.xml に移動する必要があります。接続タイムアウトの名前の属性があります。デフォルト値は 20000Ms(20 秒) で、無制限の接続時間の場合は -1 に変更します。

于 2013-05-23T12:29:16.337 に答える
1

キープアライブ メカニズムを実装する必要がある場合があります。

一般に、プロキシはしばらくすると Websocket 接続を破棄します。

あなたはこれを見ているかもしれません:Tomcat 7.0.39での大気タイムアウトの問題

于 2013-05-23T12:29:11.650 に答える