- STOMP is a subprotocol operating on top of the lower-level WebSocket.
- generate a new project with the required dependency (Websocket).
- implementation 'org.webjars:webjars-locator-core'
- implementation 'org.webjars:sockjs-client:1.0.2'
- implementation 'org.webjars:stomp-websocket:2.3.3'
- implementation 'org.webjars:bootstrap:3.3.7'
- implementation 'org.webjars:jquery:3.1.1-1'
- model the message that carries the name, create a plain old Java object with a name property and a corresponding getName()
- model the greeting representation, add another plain old Java object with a content property and a corresponding getContent() method
The @MessageMappingannotation ensures that, if a message is sent to the /hello destination, thegreeting()method is called.- After the one-second delay, the
greeting()method creates a Greeting object and returns it. - The return value is broadcast to all subscribers of
/topic/greetings, as specified in the@SendToannotation.
-
Create a Java class named
WebSocketConfig-
@Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/gs-guide-websocket").withSockJS(); } }
-
-
@Configurationto indicate that it is a Spring configuration class. -
@EnableWebSocketMessageBrokerenables WebSocket message handling, backed by a message broker. -
enableSimpleBroker() to:
- enable a simple memory-based message broker to carry the greeting messages back to the client on destinations prefixed with
/topic - designates the /app prefix for messages that are bound for methods annotated with
@MessageMapping
- enable a simple memory-based message broker to carry the greeting messages back to the client on destinations prefixed with
-
The registerStompEndpoints() method registers the /gs-guide-websocket endpoint,
- enabling SockJS fallback options so that alternate transports can be used if WebSocket is not available.
- The SockJS client will attempt to connect to /gs-guide-websocket and use the best available transport (websocket, xhr-streaming, xhr-polling, and so on).
- This HTML file imports the SockJS and STOMP javascript libraries that will be used to communicate with our server through STOMP over websocket.