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

时间:2015-11-07 20:45:00

标签: java stomp 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!");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

在主要方法的最后一行添加 Thread.sleep(60000) ,它应该有效

答案 1 :(得分:0)

客户端将在通信完成(发送或接收)后关闭连接,您应该阻止该线程以确保应用程序不关闭。
在main方法结束时添加:

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