Search code examples
javastompspring-websocket

Can't connect client to server using Spring Websocket


I've written a Blackjack server in java which starts up fine, but I'm having trouble writing a separate java client to connect to my server. I've followed the Spring Websocket documentation and examples and it looks like my client connects without any errors and I'd expect it to hit the afterConnected() method, but it doesn't get to that point. The client runs and finishes with no errors. Any idea what I'm doing wrong?

WebSocketConfig.java on the server side:

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();
    }
}

Client side:

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!");
        }
    }
}

Solution

  • the main method of the client side is finishing before to connect.

    put a Thread.sleep(60000) in the last line of your main method, it's should work