无法使用 Spring Websocket 将客户端连接到服务器

Can't connect client to server using Spring Websocket

我在 java 中编写了一个 Blackjack 服务器,它启动良好,但我在编写单独的 java 客户端连接到我的服务器时遇到了问题。我遵循了 Spring Websocket 文档和示例,看起来我的客户端连接没有任何错误,我希望它能达到 afterConnected() 方法,但它没有达到这一点。客户端运行并完成且没有错误。知道我做错了什么吗?

WebSocketConfig.java 服务器端:

package com.lactaoen.blackjack.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic", "/queue");
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/connect").withSockJS();
    }
}

客户端:

package com.lactaoen.blackjack.client;

import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.simp.stomp.*;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.RestTemplateXhrTransport;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;

import java.util.ArrayList;
import java.util.List;

public class BlackjackClient {

    public static void main(String[] args) {

        List<Transport> transports = new ArrayList<>();
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        transports.add(new RestTemplateXhrTransport());

        SockJsClient sockJsClient = new SockJsClient(transports);

        String stompUrl = "ws://localhost:8080/connect";
        WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
        stompClient.setMessageConverter(new StringMessageConverter());

        stompClient.connect(stompUrl, new BlackjackSessionHandler());
    }

    private static class BlackjackSessionHandler extends StompSessionHandlerAdapter {
        @Override
        public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
            // I'd expect it to print this out, but it doesn't get here
            System.out.println("Hello, world!");
        }
    }
}

客户端的主要方法在连接之前完成。

在你的 main 方法的最后一行放一个 Thread.sleep(60000),它应该可以工作

客户端会在通信完成(发送或接收)后关闭连接,您应该阻塞线程以确保应用程序不会关闭。
在主要方法的末尾添加:

//block the thread
CountDownLatch latch = new CountDownLatch(1);
try {
    latch.await();
} catch (InterruptedException e) {
    e.printStackTrace();
}