Search code examples
javaspringwebsocketglassfishsockjs

Java Glassfish Spring Sockjs failed: Error during WebSocket handshake: Unexpected response code: 500


Hello everyone i have these error

  WebSocket connection to 'ws://localhost:8080/vix/hello/598/rula3cfq/websocket' failed: Error during WebSocket handshake: Unexpected response code: 500

my glassfish version is 4 , spring 4.2.4.RELEASE

This is my Controller

@Controller
public class Test
{

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception
{
    Thread.sleep(3000);
    return new Greeting("hello" + message.getName());
}

this is websocket config

 @Configuration
 @EnableWebSocketMessageBroker
 public class WebsocketConfig extends        
              AbstractWebSocketMessageBrokerConfigurer
{
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}

public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/hello")
            .setAllowedOrigins("*").withSockJS();
}

}

this security config

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/webjars/**", "/images/**", "/oauth/uncache_approvals", "/oauth/cache_approvals","/topic/**","/app/**",
            "/hello/**", "/calcApp/**");
}

this is front where i using stomp and sockjs

    var stompClient = null;

    function setConnected(connected) {
        document.getElementById('connect').disabled = connected;
        document.getElementById('disconnect').disabled = !connected;
        document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
        document.getElementById('response').innerHTML = '';
    }

    function connect() {
        var socket = new SockJS('http://localhost:8080/vix/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/vix/topic/greetings', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }

    function disconnect() {
        if (stompClient != null) {
            stompClient.disconnect();
        }
        setConnected(false);
        console.log("Disconnected");
    }

    function sendName() {
        var name = document.getElementById('name').value;
        stompClient.send("/vix/app/hello", {}, JSON.stringify({ 'name': name }));
    }

    function showGreeting(message) {
        var response = document.getElementById('response');
        var p = document.createElement('p');
        p.style.wordWrap = 'break-word';
        p.appendChild(document.createTextNode(message));
        response.appendChild(p);
    }

any idea how fix this ? i'm new in spring websocket


Solution

  • In your connect method you can just leave

    Don't use the same endpoint "hello" and MessageMapping, will be confuse

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
            .setAllowedOrigins("*").withSockJS();
    }
    
    var socket = new SockJS('/hello');
    

    Here also remove the contextPath vix

    stompClient.subscribe('/topic/greetings', function(greeting){
        showGreeting(JSON.parse(greeting.body).content);
    });
    

    Here again the same

     stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
    

    Basically, all problem was adding the context because the context was added by default when you add the first "/"