我正在使用 spring-websocket 并且我有这两个拦截器
<websocket:client-inbound-channel>
<websocket:executor core-pool-size="100" max-pool-size="200" keep-alive-seconds="600"/>
<websocket:interceptors>
<ref bean="myInterceptor"/>
</websocket:interceptors>
</websocket:client-inbound-channel>
<websocket:client-outbound-channel>
<websocket:executor core-pool-size="100" max-pool-size="200" keep-alive-seconds="600"/>
<websocket:interceptors>
<ref bean="myOutInterceptor"/>
</websocket:interceptors>
</websocket:client-outbound-channel>
我正在使用 StompHeaderAccessor 将消息包装在 preSend(Message<?> message, MessageChannel channel)
中在两个拦截器中。
我使用以下内容来访问入站拦截器中的 session 属性:
...
StompHeaderAccessor sha = StompHeaderAccessor.wrap(message);
// ignore non-STOMP messages like heartbeat messages
if(sha.getCommand() == null) {
return message;
}
String sessionId = sha.getSessionId();
Map<String, Object> sessionAttributes = sha.getSessionAttributes();
...
问题是,sha.getSessionAttributes();
在入站拦截器中返回数据,但是当我调用 sha.getSessionAttributes();
时在出站拦截器中,它返回 null。
如何从出站拦截器访问 sessionAttributes?
最佳答案
这感觉像是一个解决方法。我就是这样解决的。
我添加了一个类似于包含 Map 的存储库的 bean,键是 session ID,值是 session 属性。
在入站拦截器中,在 SUBSCRIBE 情况下,我将 session ID 与属性一起放入。并将其从 map 中删除,以防取消订阅和断开连接。
在出站拦截器中,例如 MESSAGE,我从该 bean sessionAttributes = theBean.getSessionIdAndAttributes().get(sessionId)
获取了相关的 sessionAttributes,而不是从消息对象 sessionAttributes = sha.getSessionAttributes()
获取它。
关于java - 如何从客户端出站 channel 访问 websocket 客户端入站 channel 拦截器中填充的 STOMP getSessionAttributes()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53388803/