Spring, how to broadcast message to connected clients using websockets?

前端 未结 1 1528
情话喂你
情话喂你 2021-02-13 18:46

I am trying to use websockets in my app. I have followed this tutorial: http://spring.io/guides/gs/messaging-stomp-websocket/

It works perfectly.

When one of

1条回答
  •  别那么骄傲
    2021-02-13 19:02

    @SendTo works only in the SimpAnnotationMethodMessageHandler, which is initiated only through the SubProtocolWebSocketHandler, hance when the WebSocketMessage is received from clients.

    To achieve your requirements you should inject to the your @Scheduled service SimpMessagingTemplate brokerMessagingTemplate and use it directly:

    @Autowired
    private SimpMessagingTemplate brokerMessagingTemplate;
    .......
    this.brokerMessagingTemplate.convertAndSend("/topic/greetings", "foo");
    

    0 讨论(0)
提交回复
热议问题