Spring + SockJS + Stomp不接收心跳以外的消息

时间:2018-03-20 11:01:46

标签: bower stomp spring-websocket sockjs

我在服务器端和bower客户端使用Spring版本4.3.6.RELEASE" stomp-websocket":" 2.0"和" sockjs-client":" 1.1.4"

以下是服务器端的所有相关代码 -

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer
{
  @Autowired
  private SimpMessagingTemplate brokerMessagingTemplate;

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry)
  {
    // for local testing, setting allowed origins to * - Should be removed later
    registry.addEndpoint("/websoc").setAllowedOrigins("*").withSockJS().setSupressCors(true);
  }

  @Override
  public void configureMessageBroker(MessageBrokerRegistry registry)
  {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableSimpleBroker("/topic", "/queue");
  }

  @Override
  public void configureClientOutboundChannel(ChannelRegistration registration)
  {
    registration.taskExecutor()
    .corePoolSize(10)
    .maxPoolSize(20);
  }

  @Override
  public void configureWebSocketTransport(WebSocketTransportRegistration registration)
  {
    registration.setSendTimeLimit( 15 * 1000)
    .setSendBufferSizeLimit( 512 * 1024)
    .setMessageSizeLimit( 128 * 1024);
  }

  @PostConstruct
  public void fetchNotifications()
  {

    NotificationsController.setMesgingTmpt(brokerMessagingTemplate);
  }
}


@Controller
public class NotificationsController
{
  private static SimpMessagingTemplate brokerMessagingTemplate;

  public static void setMesgingTmpt(SimpMessagingTemplate tmp)
  {
    brokerMessagingTemplate = tmp;
  }

  // @SendTo("/topic/userNotifications")
  public void testWSMessage(JUserNotification un)
  {
    LOGGER.debug("testWSMessage called");

    un.setAccountId("1");
    un.setUserId("1");

    try
    {
      LOGGER.debug("sending notif: {}", JSONUtils.stringify(un));
      brokerMessagingTemplate.convertAndSend("/topic/userNotifications", un);

    } catch (final Exception e)
    {
      LOGGER.debug("exception while sending msg to client: {}", e);
    }
   // return un;
  }
}

@Component
public class CustomApplicationListener implements ApplicationListener<ApplicationEvent>
{
    @Override
  public void onApplicationEvent(ApplicationEvent event)
  {
    if (event instanceof SessionSubscribeEvent)
    {
      LOGGER.debug("subscribe event received. Some params - ");
      final SessionSubscribeEvent se = (SessionSubscribeEvent) (event);
      final StompHeaderAccessor headers = StompHeaderAccessor.wrap(se.getMessage());
      LOGGER.debug("sessionId: {}", headers.getSessionId());
      LOGGER.debug("sessionAttributes: {}", headers.getSessionAttributes());
      LOGGER.debug("ack: {}", headers.getAck());
      LOGGER.debug("command: {}", headers.getCommand());
      LOGGER.debug("destination: {}", headers.getDestination());
      LOGGER.debug("subscriptionId: {}", headers.getSubscriptionId());
      LOGGER.debug("user: {}", headers.getUser());


      // test code
      final NotificationsController notifs = new NotificationsController();
      final JUserNotification usrNotif = new JUserNotification();
      notifs.testWSMessage(usrNotif);
    }
  }
}

在客户端上,这是代码 -

    this.set("webSockURL", this.get("serverInfo").getWebsocketEndPoint());
    this.set("sock", new SockJS(this.get("webSockURL")));
    this.set("stompClient", Client.over(this.get("sock")));

    var stompcli = this.get("stompClient");

    var subscribeCallBack = function(message) {
    console.log("inside subscribeCallBack");
      this._processNotifications(message)
    };

    var connectCallBack = function() {
      console.log("inside connectCallBack");
      stompcli.subscribe("topic/userNotifications", subscribeCallBack);
    };

    stompcli.connect({}, {}, connectCallBack);

Websocket连接已建立,我收到心跳消息,但我没有收到我发送的通知。我甚至尝试过使用@SendTo注释,但它不起作用。

当我使用webjars stomp和sockjs尝试相同的服务器代码并在本地运行时,我收到消息。这让我觉得客户端库有问题,但我不确定。

服务器在nginx后面的docker容器上运行作为反向代理。我已经将ELB配置为允许websockets并启用了proxy_protocol。

由于连接本身已成功建立,我不会怀疑它的基础设施部分。 任何想法将不胜感激。

0 个答案:

没有答案