如何在Java Spring WebSocketStompClient中获取会话ID?
我有WebSocketStompClient和StompSessionHandlerAdapter,这些实例可以很好地连接到我服务器上的websocket。 WebSocketStompClient使用SockJsClient 但我不知道如何获取websocket连接的会话ID。在客户端使用stomp会话处理程序的代码
private class ProducerStompSessionHandler extends StompSessionHandlerAdapter {
...
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
...
}
stomp会话包含会话ID,这与服务器上的会话ID不同。 所以从这个ID:
DEBUG ... Processing SockJS open frame in WebSocketClientSockJsSession[id='d6aaeacf90b84278b358528e7d96454a...
DEBUG ... DefaultStompSession - Connection established in session id=42e95c88-cbc9-642d-2ff9-e5c98fb85754
我需要来自WebSocketClientSockJsSession的第一个会话ID。 但我没有在WebSocketStompClient或SockJsClient中找到任何方法来检索会话ID ......
答案 0 :(得分:7)
要获取会话ID,您需要定义自己的拦截器,如下所示,并将会话ID设置为自定义属性。
public class HttpHandshakeInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map attributes) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession();
attributes.put("sessionId", session.getId());
}
return true;
}
现在,您可以在控制器类中获得相同的会话ID。
@MessageMapping("/message")
public void processMessageFromClient(@Payload String message, SimpMessageHeaderAccessor headerAccessor) throws Exception {
String sessionId = headerAccessor.getSessionAttributes().get("sessionId").toString();
}
答案 1 :(得分:5)
您可以使用@Header
批注来访问sessionId:
@MessageMapping("/login")
public void login(@Header("simpSessionId") String sessionId) {
System.out.println(sessionId);
}
它对我来说很好,没有任何自定义拦截器
答案 2 :(得分:1)
有一种方法可以通过反射API提取sockjs sessionId:
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
// we need another sessionId!
System.out.println("New session established : " + session.getSessionId());
DefaultStompSession defaultStompSession =
(DefaultStompSession) session;
Field fieldConnection = ReflectionUtils.findField(DefaultStompSession.class, "connection");
fieldConnection.setAccessible(true);
String sockjsSessionId = "";
try {
TcpConnection<byte[]> connection = (TcpConnection<byte[]>) fieldConnection.get(defaultStompSession);
try {
Class adapter = Class.forName("org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter");
Field fieldSession = ReflectionUtils.findField(adapter, "session");
fieldSession.setAccessible(true);
WebSocketClientSockJsSession sockjsSession = (WebSocketClientSockJsSession) fieldSession.get(connection);
sockjsSessionId = sockjsSession.getId(); // gotcha!
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (StringUtils.isBlank(sockjsSessionId)) {
throw new IllegalStateException("couldn't extract sock.js session id");
}
String subscribeLink = "/topic/auth-user" + sockjsSessionId;
session.subscribe(subscribeLink, this);
System.out.println("Subscribed to " + subscribeLink);
String sendLink = "/app/user";
session.send(sendLink, getSampleMessage());
System.out.println("Message sent to websocket server");
}
可以在这里看到:tutorial