Haseeb Wali
Haseeb Wali

Reputation: 1171

Client Automatically disconnects after some time in websocket

My desktop App client is connecting to web server through webSockets. I am using javaWebSocket api for the client side, and I am using Apache tomcat 7's webSocketServlet. I do't know what I am doing wrong but client is connecting perfectly with webserver and sending chat messages but after spending some time idle, the server automatically disconnects the client. I want the client to be connected until the client intentionally don't want to disconnect. Here are my sample codes of web Socket servlet and client. First is the client code

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

and now this is the servlet code.

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

Upvotes: 5

Views: 11549

Answers (2)

Haseeb Wali
Haseeb Wali

Reputation: 1171

Finally i managed to solve it. Problem was with tomcat's configuration file. You have to go to the following directory ApacheTomcat->Config->server.xml, There is attribute with the name of connection time out default value is 20000Ms(20 Seconds) change it to -1 for infinite connection time.

Upvotes: 4

unludo
unludo

Reputation: 5010

You may have to implement a keep-alive mechanism.

In general proxies destroy websocket connections after some time.

You may have a look in this : Atmosphere timeout issue with tomcat 7.0.39

Upvotes: 3

Related Questions