I have a concurrent issue, my WebSocket was used spring-message, when my chatroom has a lot of people it will very slow.
So I try to find how, and I found some problem where the WebSocketSession using sendMessage, it has a synchronized in class websocketServerSockJsSession
@Override
public void sendMessageInternal(String message) throws SockJsTransportFailureException {
// Open frame not sent yet?
// If in the session initialization thread, then cache, otherwise wait.
if (!this.openFrameSent) {
synchronized (this.initSessionLock) {
if (!this.openFrameSent) {
this.initSessionCache.add(message);
return;
}
}
}
so if they have 200 chat in one second it will be very slow,
I found the implement of WebSocketSession call ConcurrentWebSocketSessionDecorator.
But I can't cast WebSocketServerSockJsSession to ConcurrentWebSocketSessionDecorator, I can't find how to set my WebSocketSession.
I can't change the sockJS.
So how can I do if I use the sockJS and I want to use ConcurrentWebSocketSessionDecorator method?
is another way to implement the ConcurrentWebSocketSessionDecorator sendMessage or I can do some property setting and make my WebSocketSession turn to ConcurrentWebSocketSessionDecorator?
this my config setting
@Configuration
@EnableWebMvc
@EnableWebSocket
public class SpringWebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler(), "/websocket/send").addInterceptors(new HandshakeInterceptor()).setAllowedOrigins("*");
registry.addHandler(webSocketHandler(), "/websocket/sockjs").addInterceptors(new HandshakeInterceptor()).setAllowedOrigins("*").withSockJS();
}
As its name suggests, the ConcurrentWebSocketSessionDecorator
is a Decorator.
This decorator overrides some functions such as sendMessage
of WebSocketSession
, and WebSocketServerSockJSession
performs its actions through its webSocketSession
, so changing webSocketSession
to ConcurrentWebSocketSessionDecorator
can solve the problem.
ConcurrentWebSocketSessionDecorator currentWebSocketSessionDecorator = new ConcurrentWebSocketSessionDecorator (session, 1000, 1024);
wsSockJsSession.initializeDelegateSession((WebSocketSession)concurrentWebSocketSessionDecorator);