首页 > 解决方案 > spring boot websocket中的处理程序传输错误

问题描述

我在 spring-boot 1.5.8 中实现 websocket,在我的本地它工作正常,但是当我部署到服务器(在嵌入式服务器中运行)时,来自服务器的消息通知不稳定,有时客户端有时会收到消息。

我查看日志并收到一条消息

2019-03-20 08:04:08.723  INFO 15506 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats    : WebSocketSession[2 current WS(2)-HttpStream(0)-HttpPoll(0), 167 total, 0 closed abnormally (0 connect failure, 0 send limit, 27 transport error)], stompSubProtocol[processed CONNECT(107)-CONNECTED(107)-DISCONNECT(2)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1149], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 306], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 4, completed tasks = 4468]

(0 connect failure, 0 send limit, 27 transport error)

日志消息显示 27 消息错误,我不知道如何解决。

我们可以处理错误消息事件来解决这个问题吗?

更新

我的 websocket 服务器

 @Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/queue");
    registry.enableSimpleBroker("/notify", "/queue", "/user");
    registry.setUserDestinationPrefix("/user");
}

在客户端

function connect() {

    var socket = new SockJS('http://localhost:8080/ws/');
    var stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
      stompClient.subscribe('/user/queue/notify', function(notification) {
          notify(notification.body)
      });
    });

    return;
  } 

  /**
   * Display the notification message.
   */
  function notify(message) {
    $("#notifications-area").append(message + "\n");
    return;
  }

标签: spring-bootspring-websocket

解决方案


推荐阅读