Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket handshake. From ws to wss

I am developing an app for some time and recently I attached to the app an SSL Certificate (it is not self-signed; it is signed by Comodo, so the problem can't occur from here). I implemented long ago a WebSocket endpoint on my Java Glassfish server and I'm using it with javascript. I have been using the WebSocket successfully via http until now, when I moved to https.

Let's have a look at the code snippets I use:

Server Endpoint:

@ServerEndpoint(value = "/ws/chat", 
    encoders = ChatMessageEncoder.class, 
    decoders = ChatMessageDecoder.class)
public class ChatEndpoint {

@OnOpen
public void open(final Session session) {
    // stuff happenin'
}

@OnMessage
public void onMessage(final Session session) {
    // stuff happenin'    }

@OnError
public void onError(Throwable t) {
    t.printStackTrace();
    System.out.println("error");
}

@OnClose
public void onClose() {
}
}

Client Connection:

var wsocketPrivate;
function connectToChatserver() {
    var serviceLocation = 'wss://<ip>:8080/ws/chat';
    $rootScope.wsocketPrivate = new WebSocket(serviceLocation);
    $rootScope.wsocketPrivate.onmessage = onMessageReceived;
};
function onMessageReceived(evt) {
    console.log(evt)
};
connectToChatserver();

Not having activated ssl certificate and using var serviceLocation = 'ws://<ip>:8080/ws/chat'; (ws instead of wss) works perfectly fine. When I moved to https, it asked for wss (the browser blocked my ws handshake because it wasn't secure) and moving to wss, the following error occurs:

WebSocket connection to 'wss://<ip>:8080/ws/chat' failed: Error in connection establishment: net::ERR_TIMED_OUT

What am I doing wrong? Can you suggest some tests to find out more information?

Thank you, Mihai

like image 792
ashcrok Avatar asked Nov 09 '22 20:11

ashcrok


1 Answers

I finally found where the problem was. I was using an nginx front server as a proxy. The default configuration of that server was blocking my wss for some weird reason.

like image 180
ashcrok Avatar answered Nov 15 '22 06:11

ashcrok