使用Java EE websocket在ServerEndpoint中未调用OnClose

时间:2018-06-26 08:37:49

标签: java websocket java-websocket

我有一个简单的Websocket端点,使用ServerEndpoint库,但是即使 OnOpen OnMessage 被调用。我也尝试过将@ServerEndpoint("/websocket/cont") public class ContinousWebsocketExample { private static final Logger LOGGER = LoggerFactory.getLogger(ContinousWebsocketExample.class); private int counter = 0; @OnOpen public void onOpen(Session client, EndpointConfig config) { LOGGER.debug("---------------------------Opening websocket connection-----------------------------------"); while (true) { client.getAsyncRemote().sendText("Counter is: " + counter); counter ++; LOGGER.debug(String.valueOf(client.isOpen())); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } @OnClose public void onClose(Session client, CloseReason reason) { LOGGER.debug("---------------------------Closing websocket connection-----------------------------------"); } } 继承为子类,但没有任何运气。

这是我的代码

var socket = new WebSocket('ws://localhost:8080/examples/websocket/cont')
 socket.close();

以及相关的Javascript:

client.isOpen()

即使前端关闭插槽,程序仍继续打印计数器。但是,build:在前端关闭连接后返回 false

0 个答案:

没有答案