5

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);
 }
Gopakumar N G
1,8633 gold badges24 silver badges41 bronze badges
asked May 23, 2013 at 9:21

2 Answers 2

4

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.

answered May 23, 2013 at 12:29
Sign up to request clarification or add additional context in comments.

1 Comment

what is the ideal value for timeout that you used ?
3

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

answered May 23, 2013 at 12:29

1 Comment

Proxies from client side/server side or both? if proxies destroys them then how to manage this ? keep alive meaning have to periodically send a ping message to server ???

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.