如何使用 ConcurrentWebSocketSessionDecorator 通过 websocket 发送消息
How can I using ConcurrentWebSocketSessionDecorator to send my message with websocket
我有一个并发问题,我的 WebSocket 被使用了 spring-消息,
当我的聊天室有很多人时它会很慢。
所以我试着找出方法,我发现 WebSocketSession 使用 sendMessage 的一些问题,它在 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;
}
}
}
所以如果他们在一秒钟内有 200 个聊天,那会很慢,
找到WebSocketSession调用ConcurrentWebSocketSessionDecorator的实现
但是我无法将 WebSocketServerSockJsSession 转换为 ConcurrentWebSocketSessionDecorator,我找不到如何设置我的 WebSocketSession。
我无法更改 sockJS。
那么如果我使用了sockJS又想使用ConcurrentWebSocketSessionDecorator方法怎么办呢?
是实现 ConcurrentWebSocketSessionDecorator sendMessage 的另一种方法,或者我可以做一些 属性 设置并使我的 WebSocketSession 转向 ConcurrentWebSocketSessionDecorator?
这是我的配置设置
@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();
}
顾名思义,ConcurrentWebSocketSessionDecorator
是一个 装饰器。
这个装饰器覆盖了一些函数,比如WebSocketSession
的sendMessage
,而WebSocketServerSockJSession
通过它的webSocketSession
来执行它的动作,所以把webSocketSession
改成ConcurrentWebSocketSessionDecorator
可以解决问题。
ConcurrentWebSocketSessionDecorator currentWebSocketSessionDecorator = new ConcurrentWebSocketSessionDecorator (session, 1000, 1024);
wsSockJsSession.initializeDelegateSession((WebSocketSession)concurrentWebSocketSessionDecorator);
我有一个并发问题,我的 WebSocket 被使用了 spring-消息, 当我的聊天室有很多人时它会很慢。
所以我试着找出方法,我发现 WebSocketSession 使用 sendMessage 的一些问题,它在 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;
}
}
}
所以如果他们在一秒钟内有 200 个聊天,那会很慢,
找到WebSocketSession调用ConcurrentWebSocketSessionDecorator的实现
但是我无法将 WebSocketServerSockJsSession 转换为 ConcurrentWebSocketSessionDecorator,我找不到如何设置我的 WebSocketSession。
我无法更改 sockJS。
那么如果我使用了sockJS又想使用ConcurrentWebSocketSessionDecorator方法怎么办呢?
是实现 ConcurrentWebSocketSessionDecorator sendMessage 的另一种方法,或者我可以做一些 属性 设置并使我的 WebSocketSession 转向 ConcurrentWebSocketSessionDecorator?
这是我的配置设置
@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();
}
顾名思义,ConcurrentWebSocketSessionDecorator
是一个 装饰器。
这个装饰器覆盖了一些函数,比如WebSocketSession
的sendMessage
,而WebSocketServerSockJSession
通过它的webSocketSession
来执行它的动作,所以把webSocketSession
改成ConcurrentWebSocketSessionDecorator
可以解决问题。
ConcurrentWebSocketSessionDecorator currentWebSocketSessionDecorator = new ConcurrentWebSocketSessionDecorator (session, 1000, 1024);
wsSockJsSession.initializeDelegateSession((WebSocketSession)concurrentWebSocketSessionDecorator);