从过去2天开始,我正面临这个问题。调试后,我可以看到spring websocket将数据发送到队列中,但没有被客户端接收
我没有收到任何错误,但有时我收到了此错误。
线程“ clientInboundChannel-23”中的异常 org.springframework.messaging.MessageDeliveryException:发送失败 消息发送给ExecutorSubscribableChannel [clientOutboundChannel];嵌套的 异常是java.lang.NullPointerException, failedMessage = GenericMessage [有效载荷=字节[0], 标头= {simpMessageType = DISCONNECT_ACK, simpDisconnectMessage = GenericMessage [有效载荷=字节[0], 标头= {simpMessageType = DISCONNECT,stompCommand = DISCONNECT, simpSessionAttributes = {org.springframework.messaging.simp.SimpAttributes.COMPLETED = true}, simpUser=com.gn.beans.User@44880668,simpSessionId = egw2xot3}], simpUser=com.gn.beans.User@44880668,simpSessionId = egw2xot3}] org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:127) 在 org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:104) 在 org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler.handleDisconnect(SimpleBrokerMessageHandler.java:328) 在 org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler.handleMessageInternal(SimpleBrokerMessageHandler.java:282) 在 org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.handleMessage(AbstractBrokerMessageHandler.java:238) 在 org.springframework.messaging.support.ExecutorSubscribableChannel $ SendTask.run(ExecutorSubscribableChannel.java:135) 在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) 在java.lang.Thread.run(未知源)造成原因: java.lang.NullPointerException在 com.gn.interceptors.WebSocketInterceptor.preSend(WebSocketInterceptor.java:24) 在 org.springframework.messaging.support.AbstractMessageChannel $ ChannelInterceptorChain.applyPreSend(AbstractMessageChannel.java:158) 在 org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:113) ...另外8个
我的websocket设置如下所示。
WebSocketConfig.java
package com.gn.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import com.gn.interceptors.WebSocketInterceptor;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/advantageClient","/advantageAll");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").setAllowedOrigins("*");
registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS();
}
@Bean
public WebSocketInterceptor webSocketInterceptor() {
return new WebSocketInterceptor();
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(webSocketInterceptor());
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(8);
registration.setInterceptors(webSocketInterceptor());
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration)
{
registration.setSendTimeLimit(15 * 1000)
.setSendBufferSizeLimit(200 * 1024 * 1024)
.setMessageSizeLimit(200 * 1024 * 1024);
}
}
WebSocketUtils.java将数据发送到客户端
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.simp.SimpMessagingTemplate;
public class WebSocketUtil {
private static final Logger logger = LoggerFactory.getLogger("com.gn.utils");
private static SimpMessagingTemplate template;
public static void setTemplate(SimpMessagingTemplate tmplt) {
template = tmplt;
}
public static void sendToUser(String message,int userId) {
logger.info("Send to user with userID id "+userId+" msg is "+message);
String uId = String.valueOf(userId);
template.convertAndSendToUser(uId,"/advantageClient/connect/", message);
// template.convertAndSendToUser(userId+"","/advantageClient/connect/", message);
}
public static void sendToAll(String message) {
logger.info("Send to all with msg is "+message);
template.convertAndSend("/advantageAll/connect/", message);
}
}
用于连接Websocket的客户端代码
connect() {
const uId = this.homeService.getUserId();
let ws = new SockJS(environment.apiProtocol + '://' + environment.apiHost + ':' + environment.apiPort + UrlConstantsService.urlConstants.advantageService + UrlConstantsService.urlConstants.websocket);
this.ws = Stomp.over(ws);
let that = this;
this.ws.connect({userId: uId}, function(frame) {
that.ws.subscribe('/user/advantageClient/connect/', function(msg) {
that.change.emit(JSON.parse(msg.body));
});
that.ws.subscribe('/advantageAll/connect/', function(msg) {
that.change.emit(JSON.parse(msg.body));
});
}, function(error) {
console.log('error connecting to server', error);
});
}
disconnect() {
if (this.ws !== null) {
this.ws.ws.close();
}
}