Spring boot builds Stomp client

1. Dependence

There is only one dependency, and the version follows the spring boot version.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

2. Client configuration

The configuration includes three parts, one is the basic websocket client configuration, and the other is the Stomp client configuration and session handling.

First look at the basic websocket client configuration:

    @Bean
    public WebSocketClient webSocketClient() {
    
    
        List<Transport> transports = new ArrayList<>();
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        transports.add(new RestTemplateXhrTransport());
        return new SockJsClient(transports);
    }

Then the Stomp client configuration:

  @Bean
    public WebSocketStompClient webSocketStompClient(WebSocketClient webSocketClient, StompSessionHandler stompSessionHandler) {
    
    
        WebSocketStompClient webSocketStompClient = new WebSocketStompClient(webSocketClient);
        webSocketStompClient.setMessageConverter(new StringMessageConverter());
        webSocketStompClient.connect("http://localhost:6060/pda-message-websocket", stompSessionHandler);
        return webSocketStompClient;
    }

Session handling configuration:

    @Bean
    public StompSessionHandler stompSessionHandler() {
    
    
        return new ClientStompSessionHandler();
    }

3. Session handler ClientStompSessionHandler

This class inherits StompSessionHandlerAdapter, including the following abstract methods:

    public StompSessionHandlerAdapter() {
    
    
    }

    public Type getPayloadType(StompHeaders headers) {
    
    
        return String.class;
    }

    public void handleFrame(StompHeaders headers, @Nullable Object payload) {
    
    
    }

    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
    
    
    }

    public void handleException(StompSession session, @Nullable StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
    
    
    }

    public void handleTransportError(StompSession session, Throwable exception) {
    
    
    }

In our ClientStompSessionHandler class, the main implementations are: afterConnected, handleFrame, handleException, handleTransportError.

First look at the afterConnected method, which is called after the client connection is completed:

    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
    
    
         //这里需要自己写逻辑,这里只是简单的演示
        logger.info("客户端已连接: headers {}", connectedHeaders);
        session.subscribe("/topic/greeting", this);
        String message = "hello";
        logger.info("客户端发送:{}", message);
        session.send("/app/greeting", message);
    }

Processing of frames:

  @Override
    public void handleFrame(StompHeaders headers, Object payload) {
    
    
        //这里需要自己写逻辑,这里只是简单的演示
        logger.info("客户端收到消息:{}",payload);
    }

Client exception handling:

    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
    
    
      //这里需要自己写逻辑,这里只是简单的演示
        logger.error("客户端错误: 异常 {}, command {}, payload {}, headers {}", exception.getMessage(), command, payload, headers);
    }

Transport exception handling:

   @Override
    public void handleTransportError(StompSession session, Throwable exception) {
    
    
      //这里需要自己写逻辑,这里只是简单的演示
        logger.error("客户端传输错误:错误 {}", exception.getMessage());
    }

4. Start

   public static void main(String[] args) {
    
    
        new SpringApplicationBuilder(ClientWebSocketSockJsStompApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);
    }

The result is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_35241329/article/details/131894317