Java 如何从客户端出站通道访问websocket客户端入站通道拦截器中填充的STOMP getSessionAttributes()?
我使用的是SpringWebSocket,我有两个拦截器Java 如何从客户端出站通道访问websocket客户端入站通道拦截器中填充的STOMP getSessionAttributes()?,java,spring-websocket,Java,Spring Websocket,我使用的是SpringWebSocket,我有两个拦截器 <websocket:client-inbound-channel> <websocket:executor core-pool-size="100" max-pool-size="200" keep-alive-seconds="600"/> <websocket:interceptors> <ref bean="myInterceptor"/> &
<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)
中包装消息
我正在使用以下命令访问入站拦截器中会话的属性:
...
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();
...
。。。
StompHeaderAccessor sha=StompHeaderAccessor.wrap(消息);
//忽略非跺脚消息,如心跳消息
if(sha.getCommand()==null){
返回消息;
}
字符串sessionId=sha.getSessionId();
Map sessionAttributes=sha.getSessionAttributes();
...
问题是,sha.getSessionAttributes()入站拦截器中的code>返回数据,但当我调用sha.getSessionAttributes()时代码>在出站拦截器中返回null
如何从出站拦截器访问SessionAttribute?这感觉像是一个解决办法。我就是这样解决的
我添加了一个bean,其作用类似于包含映射的存储库,键是会话ID,值是会话属性
在入站拦截器case SUBSCRIBE中,我将会话id与属性放在一起。并将其从地图中删除,以防取消订阅和断开连接
在出站拦截器case MESSAGE中,我从该bean获取了相关的sessionAttributessessionAttributes=theBean.getsessionandattributes().get(sessionId)
,而不是从MESSAGE对象sessionAttributes=sha.getSessionAttributes()
获取它