龙空技术网

springboot(十六)Stomp配置获取WebsocketSession

聪明的晚风zqw 157

前言:

此刻我们对“js中获取session”大体比较珍视,小伙伴们都需要剖析一些“js中获取session”的相关文章。那么小编同时在网摘上汇集了一些有关“js中获取session””的相关知识,希望大家能喜欢,同学们一起来学习一下吧!

在使用springboot的websocket功能stomp时,不能直接获取websocketsession,下面提供一个方法。

stomp配置可参考之前文章:配置websocket

添加WebSocketHandler

public class CustomSubProtocolWebSocketHandler extends SubProtocolWebSocketHandler {    private static final Logger LOGGER = LoggerFactory.getLogger(CustomSubProtocolWebSocketHandler.class);//    @Autowired    private SessionHandler sessionHandler;    public CustomSubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel) {        super(clientInboundChannel, clientOutboundChannel);    }    public CustomSubProtocolWebSocketHandler(MessageChannel clientInboundChannel,                                             SubscribableChannel clientOutboundChannel, SessionHandler sessionHandler) {        super(clientInboundChannel, clientOutboundChannel);        this.sessionHandler = sessionHandler;    }    @Override    public void afterConnectionEstablished(WebSocketSession session) throws Exception {        LOGGER.info("New websocket connection was established");        sessionHandler.register(session);        super.afterConnectionEstablished(session);    }}

其中afterConnectionEstablished,负责能够在建立websocket连接时获取session,这里的sessionHandler就是对这个session的处理了。

将CustomSubProtocolWebSocketHandler配置一下就可以了

@EnableWebSocketMessageBrokerpublic class WebSocketStompConfig /*extends AbstractWebSocketMessageBrokerConfigurer*/        extends WebSocketMessageBrokerConfigurationSupport implements WebSocketMessageBrokerConfigurer {    @Autowired    private AbstractAuthService authService;    @Autowired    private SessionHandler sessionHandler;    /**     * 注册stomp的端点     */    @Override    public void registerStompEndpoints(StompEndpointRegistry registry) {        // 允许使用socketJs方式访问,访问点为webSocketServer,允许跨域        // 在网页上我们就可以通过这个链接        //         // 来和服务器的WebSocket连接        registry.addEndpoint("/webSocketServer")//                .addInterceptors(new SessionAuthHandshakeInterceptor())                .setAllowedOrigins("*")                .withSockJS();    }    /**     * 配置信息代理     */    @Override    public void configureMessageBroker(MessageBrokerRegistry registry) {        // 订阅Broker名称        registry.enableSimpleBroker("/queue", "/topic");        // 全局使用的消息前缀(客户端订阅路径上会体现出来)        registry.setApplicationDestinationPrefixes("/app");        // 点对点使用的订阅前缀(客户端订阅路径上会体现出来),不设置的话,默认也是/user/        registry.setUserDestinationPrefix("/user/");    }    /**     * 配置客户端入站通道拦截器     */    @Override    public void configureClientInboundChannel(ChannelRegistration registration) {        registration.interceptors(createUserInterceptor());    }    @Override    public void configureClientOutboundChannel(ChannelRegistration registration) {        super.configureClientOutboundChannel(registration);    }    @Override    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {        super.addArgumentResolvers(argumentResolvers);    }    @Override    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {        super.addReturnValueHandlers(returnValueHandlers);    }    @Override    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {        return super.configureMessageConverters(messageConverters);    }    /*将客户端渠道拦截器加入spring ioc容器*/    @Bean    public UserInterceptor createUserInterceptor() {        UserInterceptor userInterceptor = new UserInterceptor();        userInterceptor.setAuthService(authService);        return userInterceptor;    }    @Override    public void configureWebSocketTransport(WebSocketTransportRegistration registration) {        registration.setMessageSizeLimit(500 * 1024 * 1024);        registration.setSendBufferSizeLimit(1024 * 1024 * 1024);        registration.setSendTimeLimit(200000);    }    @Override    @Bean    public WebSocketHandler subProtocolWebSocketHandler() {        return new CustomSubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel(), sessionHandler);    }}

标签: #js中获取session