Jetty WebSocket - 需要 onWebSocketClose 会话
Jetty WebSocket - onWebSocketClose session wanted
我正在努力如何在收到 onWebSocketClose 事件时找出会话。
官方文档:https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/websocket/api/WebSocketAdapter.html#onWebSocketClose(int,java.lang.String)
它说有两个参数:onWebSocketClose(int statusCode, java.lang.String reason)
另一个网站说,有 3 个参数:
https://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-annotations.html
@OnWebSocketClose
An optional method level annotation.
Flags one method in the class as receiving the On Close event.
Method signature must be public, not abstract, and return void.
The method parameters:
Session (optional)
int closeCode (required)
String closeReason (required)
目前在 WebSocketOpen 上,我将会话添加到这样的集合中:
private static Set<Session> connections = Collections.synchronizedSet(new HashSet<Session>());
private static final Logger logger = LogManager.getLogger(WebSocketEvent.class);
@Override
public void onWebSocketConnect(Session session)
{
super.onWebSocketConnect(session);
connections.add(session);
logger.debug("Socket Connected: " + session);
}
在 WebSocketClose 上,我想像这样再次删除此会话:
@Override
public void onWebSocketClose(Session session, int statusCode, String reason)
{
super.onWebSocketClose(session,statusCode,reason);
logger.debug("Connection closed. IP: " + session.getRemoteAddress().getAddress().toString()+ "; rc: "+statusCode+ "; reason: "+ reason);
connections.remove(session);
logger.debug("Session with following IP removed from list: " + session.getRemoteAddress().getAddress().toString());
}
但似乎官方文档中描述的可选会话参数不可用,因此将其标记为错误。
知道如何解决吗?提前致谢!
好的,我找到了。在此对象中的 WebSocketClose 事件上,有一个方法 getSession() 可用。
@Override
public void onWebSocketClose(int statusCode, String reason)
{
connections.remove(this.getSession());
super.onWebSocketClose(statusCode,reason);
logger.debug("Connection closed. rc: "+statusCode+ "; reason: "+ reason);
}
我正在努力如何在收到 onWebSocketClose 事件时找出会话。 官方文档:https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/websocket/api/WebSocketAdapter.html#onWebSocketClose(int,java.lang.String)
它说有两个参数:onWebSocketClose(int statusCode, java.lang.String reason)
另一个网站说,有 3 个参数: https://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-annotations.html
@OnWebSocketClose
An optional method level annotation.
Flags one method in the class as receiving the On Close event.
Method signature must be public, not abstract, and return void.
The method parameters:
Session (optional)
int closeCode (required)
String closeReason (required)
目前在 WebSocketOpen 上,我将会话添加到这样的集合中:
private static Set<Session> connections = Collections.synchronizedSet(new HashSet<Session>());
private static final Logger logger = LogManager.getLogger(WebSocketEvent.class);
@Override
public void onWebSocketConnect(Session session)
{
super.onWebSocketConnect(session);
connections.add(session);
logger.debug("Socket Connected: " + session);
}
在 WebSocketClose 上,我想像这样再次删除此会话:
@Override
public void onWebSocketClose(Session session, int statusCode, String reason)
{
super.onWebSocketClose(session,statusCode,reason);
logger.debug("Connection closed. IP: " + session.getRemoteAddress().getAddress().toString()+ "; rc: "+statusCode+ "; reason: "+ reason);
connections.remove(session);
logger.debug("Session with following IP removed from list: " + session.getRemoteAddress().getAddress().toString());
}
但似乎官方文档中描述的可选会话参数不可用,因此将其标记为错误。
知道如何解决吗?提前致谢!
好的,我找到了。在此对象中的 WebSocketClose 事件上,有一个方法 getSession() 可用。
@Override
public void onWebSocketClose(int statusCode, String reason)
{
connections.remove(this.getSession());
super.onWebSocketClose(statusCode,reason);
logger.debug("Connection closed. rc: "+statusCode+ "; reason: "+ reason);
}