Iri*_*ina 6 java session spring stomp spring-websocket
如何在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) {
...
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我需要来自WebSocketClientSockJsSession的第一个会话ID.但我没有在WebSocketStompClient或SockJsClient中找到任何方法来检索会话ID等内容...
小智 10
要获取会话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;
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以在控制器类中获得相同的会话ID.
@MessageMapping("/message")
public void processMessageFromClient(@Payload String message, SimpMessageHeaderAccessor headerAccessor) throws Exception {
String sessionId = headerAccessor.getSessionAttributes().get("sessionId").toString();
}
Run Code Online (Sandbox Code Playgroud)
您可以使用@Header注释来访问sessionId:
@MessageMapping("/login")
public void login(@Header("simpSessionId") String sessionId) {
System.out.println(sessionId);
}
Run Code Online (Sandbox Code Playgroud)
没有任何自定义拦截器,它对我来说很好
小智 5
有一种方法可以通过Reflection 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");
}
Run Code Online (Sandbox Code Playgroud)
可以在这里看到:教程