自动装配连接Websocket客户端

时间:2019-10-16 04:02:26

标签: spring-boot websocket stomp spring-websocket sockjs

我有2个基于Spring Boot的Websocket项目,我正在从一个项目发布到另一个项目。项目A的主题为testA,我使用下面的代码从项目中发布了该主题

我使用下面的代码成功完成了工作,但是我的问题是我想自动连接此连接,而不是每次都管理连接n断开连接,我该怎么做?

public ListenableFuture<StompSession> connect(String url) {
 try {
        log.trace("Enter ManagerWebSocket.connect() : url " + url);

        Transport webSocketTransport = new WebSocketTransport(new StandardWebSocketClient());
        List<Transport> transports = Collections.singletonList(webSocketTransport);

        SockJsClient sockJsClient = new SockJsClient(transports);
        sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

        WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);

        log.trace("Exit ManagerWebSocket.connect() : url " + url);

        return stompClient.connect(url, headers, new CustomStompSessionHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

ListenableFuture<StompSession> managerWebSocketSession = new 
ManagerWebSocket().connect(url);

Global.stompSession = managerWebSocketSession.get();
Global.stompSession.send(....);
Global.stompSession.disconnect();

0 个答案:

没有答案