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.

4

1 回答 1

0

我为此找到了解决方案/解决方法。

一旦您的互联网连接中断,套接字通道将保持不变,并且客户端无法再发送消息。

我做了什么,每当请求到达我的服务器端点时,我都会询问客户端“是否已连接”,然后最多等待 3 秒以获取响应,如果没有这样做,则表示套接字未连接。

于 2016-05-25T05:05:31.257 回答