I am using a library on my Spring Boot application and this library is exposing a WebSocket endpoint for the clients to communicate. In my use case, I want to enrich the data this library is sending on the WebSocket. I'm thinking of adding a WebSocket interceptor to capture the data and manipulate it, but I am not sure whether it's possible to add an interceptor to a WebSocket endpoint that is exposed by a third party library.
1 Replies
With a HandshakeInterceptor
you won't be able to get CONNECT / DISCONNECT frames. You have to implement a ChannelInterceptor (or extend ChannelInterceptorAdapter) and add it to the clientInboundChannel
. The preSend
method allows you to add your logic before the message is processed:
public class FilterChannelInterceptor extends ChannelInterceptorAdapter {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor headerAccessor= StompHeaderAccessor.wrap(message);
if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand()) {
// Your logic
}
return message;
}
}