java - Spring, how to broadcast message to connected clients using websockets? -
i trying utilize websockets in app. have followed tutorial: http://spring.io/guides/gs/messaging-stomp-websocket/
it works perfectly.
when 1 of connected clients press button, method called:
@messagemapping("/hello") @sendto("/topic/greetings") public greeting greeting() throws exception { system.out.println("sending message..."); thread.sleep(1000); // simulated delay homecoming new greeting("hello!"); }
and message broadcasted of connected clients.
now want modify server app, broadcast messages periodically (each hour) of connected clients, without interaction clients.
something this(but not working obviously):
@scheduled(fixedrate = 3600000) public void sendmessage(){ seek { @sendto("/topic/greetings") greeting(); } grab (exception e) { e.printstacktrace(); } }
thx advices.
@sendto
works in simpannotationmethodmessagehandler
, initiated through subprotocolwebsockethandler
, hance when websocketmessage
received clients.
to accomplish requirements should inject @scheduled
service simpmessagingtemplate brokermessagingtemplate
, utilize directly:
@autowired private simpmessagingtemplate brokermessagingtemplate; ....... this.brokermessagingtemplate.convertandsend("/topic/greetings", "foo");
java spring stomp
No comments:
Post a Comment