- Promote the official website xb.exrick.cn
- Online Demo xboot. Exrick. Cn
- Open source Github address github.com/Exrick/x-bo…
- Develop document www.kancloud.cn/exrick/xboo…
- Get the full versionxpay.exrick.cn/pay?xboot
Stomp is a simple (streaming) text-directed message protocol that provides an interoperable link format. Allows STOMP clients to interact with any STOMP message Broker. STOMP is widely used in multiple languages and platforms due to its simple design and ease of client development.
- Add the dependent
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
Copy the code
- The configuration class
/ * * *@author Exrickx
*/
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {
/** * Register stomp endpoint *@param registry
*/
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// To allow socketJs access, use http://IP:PORT/ws to connect to the server websocket
registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}
/** * Configure the information broker *@param registry
*/
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
// Subscribe to the Broker name user point-to-point topic
registry.enableSimpleBroker("/user"."/topic");
// The message prefix used by the global (client)
registry.setApplicationDestinationPrefixes("/app");
// No default /user is required for the peer-to-peer prefix
registry.setUserDestinationPrefix("/user"); }}Copy the code
- Since you’re only doing broadcast and peer-to-peer messaging, you’re only using subscriptions for publishing
@Autowired
private SimpMessagingTemplate messagingTemplate;
/ / radio
messagingTemplate.convertAndSend("/topic/subscribe"."You have received a new system message");
// Implement point-to-point with user ID
messagingTemplate.convertAndSendToUser(id,"/queue/subscribe"."You have received a new message.");
Copy the code