I'm writing simple Stomp Websocket application with Spring, and clients are both web (JS), and Mobile (ios, android). From JS code client connecting over SockJS, while mobile clients are using plain websocket connection behind SockJS.
The issue is that behaviour in my ChannelInterceptor where I'm checking authentication, is completely different for different type of connections. I can't make it work the same for every client.
Let me briefly give some code behind it and explain by example:
Websocket starter was taken from Spring example here: https://github.com/spring-guides/gs-messaging-stomp-websocket.git
Websocket Config:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(new MyChannelInterceptor());
}
}
And ChannelInterceptor itself:
public class MyChannelInterceptor implements ChannelInterceptor {
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
StompCommand command = accessor.getCommand();
...
}
}
When I'm connecting via SockJS from JS app (http://localhost:8080/gs-guide-websocket - and let Spring SockJS do the rest):
CONNECT command in MyChannelInterceptor, in postSend method - OKDISCONNECT command fires TWICE. - Not OKWhen I'm connecting via Websocket behind SockJS (ws://localhost:8080/gs-guide-websocket/websocket):
CONNECT command in MyChannelInterceptor, in postSend method - CRITICALDISCONNECT command fires correctly, once. - OKBasically, though I can't understand why sockjs tries to disconnect twice, I can live with it. But with interceptor not catching every connect event - I can't live, since I'm going to keep track of user session, and store them from exactly that interceptor.
.withSockJs() in the config - and just connect to socket - same problemSessionConnectEvent and SessionConnectedEvent - same problemNow I'm completely stuck and don't know where else I can go from here... Any help or starting point is appreciated.
Thanks a lot for any attention to my pain =(
After posting an issue to Spring Github and conversating there, I found out that this is not a bug, and basically not an issue, but just my fault:
For those interested, please refer to the corresponding thread: https://github.com/spring-projects/spring-framework/issues/24269
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With