0

I have client-server application, client is based on Java Desktop Application, and server is web application with endpoint exposed and deployed tomcat.

Does @OnClose handles below scenarios. Can i send some message to server end point when it comes to @OnClose method.

I need to handle scenario where client connection closes due to

1.) Internet drop connection

2.) System goes in Hibernate mode.

Server can have my clients connections open.

Here is my client code

@ClientEndpoint
public class ChatroomClientEndPoint {
  Session session = null;

   public ChatroomClientEndPoint() {
        URI uri;

        try {

            uri = new URI("endpoint url");

            ContainerProvider.getWebSocketContainer().connectToServer(this, uri);
            System.err.println("Connection found");
        } catch (Exception ex) {
            System.err.println("No connection found-->" + ex);
        }

    }


    @OnOpen
    public void processOpen(Session session) throws MalformedURLException, IOException {
        session.setMaxIdleTimeout(60000 * 60 * 24);     //websocket session timeout =1 day
        this.session = session;

    }


    @OnMessage
    public void processMessage(String message) throws JSONException {
    // on receiving messages from server, sends desktop notifications
    }

    public void disConnect() throws IOException {
        sendMessage("close");
    }
 }

** This code is already developed, and i think they have not implemented @OnClose because they send close message to the server, server then removes that session from its list.

Can @OnClose help in case of above failures like these.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116

1 Answers1

0

I found a solution/workaround for this.

Once your internet connection goes down, socket channel remain intact, and client can no more send messages.

What i did, whenever request come to my server endpoint , i ask client "if it is connected", then max i wait for 3 seconds to get the response, failure to do so, it means socket is not connected.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116