有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用SpringWebSocket将客户端连接到服务器

我已经用java编写了一个Blackjack服务器,它可以正常启动,但是我在编写一个单独的java客户端来连接到我的服务器时遇到了问题。我遵循了SpringWebSocket文档和示例,它看起来像是我的客户机连接没有任何错误,我希望它能够命中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!");
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

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

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

  2. # 2 楼答案

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

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