【问题标题】:Websockets - onclose - server/client - internet connection dropWebsockets - onclose - 服务器/客户端 - 互联网连接断开
【发布时间】:2016-05-20 05:06:16
【问题描述】:

我有客户端-服务器应用程序客户端基于Java Desktop应用程序,服务器是暴露端点的Web应用程序并部署了tomcat .

@OnClose 是否处理以下场景。当涉及到@OnClose 方法时,我可以向服务器端点发送一些消息吗?

我需要处理由于

而导致客户端连接关闭的情况

1.) 互联网断开连接

2.) 系统进入休眠模式。

服务器可以打开我的客户端连接。

这是我的客户端代码

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

** 此代码已经开发,我认为他们还没有实现@OnClose,因为他们向服务器发送关闭消息,然后服务器从其列表中删除该会话。

@OnClose 可以在遇到上述此类故障时提供帮助。

【问题讨论】:

    标签: java websocket connection-close


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2022-01-05
      • 2015-05-17
      • 1970-01-01
      相关资源
      最近更新 更多