ashcrok
ashcrok

Reputation: 244

Websocket using Glassfish + Nginx + Javascript

I'm trying to make a chat app using Websockets in JavaEE using Glassfish server and Javascript AngularJS for client side. I am using Nginx to proxy the connections.

Nginx config:

location /ws/ {
        proxy_pass http://10.10.127.78:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

In Glassfish admin page, I enabled the web socket mode and I ensured that the port is set to 8080.

The server side path is set to : @ServerEndpoint(value = "/ws/chat/{room}").

OK, now the issue is that, I tried to connect to many addresses and each one gives some problems:

  1. Tried to connect as localhost: var serviceLocation = 'ws://localhost:8080/ws/chat/all/'; here the connection results in a WebSocket connection to 'ws://localhost:8080/ws/chat/all/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

  2. Tried to connect to server ip: var serviceLocation = 'ws://10.10.127.78:8080/ws/chat/'; it results in connection timeout WebSocket connection to 'ws://10.10.127.78:8080/ws/chat/all/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

  3. Tried to connect to the domain: var serviceLocation = 'ws://test.domain.com:8080/ws/chat/'; it results in a 404 WebSocket connection to 'ws://test.domain.com:8080/ws/chat/all/' failed: Error during WebSocket handshake: Unexpected response code: 404

Can't get this to work. What can I try next ? Or what am I doing wrong ? If you need more info, leave a comment. Thank you, and hope someone can help.

I'm hosting all of this environments on Jelastic.

UPDATE: Javascript code

var wsocket;
var serviceLocation = 'ws://localhost:8080/ws/chat/';
//var serviceLocation = 'ws://10.10.127.78:8080/ws/chat/';
var nickName = $rootScope.loggedUser.user.firstName + " " + 
        $rootScope.loggedUser.user.lastName;
var room = 'all';
var $chatWindow = $('#response');


function onMessageReceived(evt) {
    var msg = JSON.parse(evt.data);
    var $appendedLine = $('<tr class="hidden-xs"><td>'+msg.received+' | '+msg.sender+'</td><td>'+msg.message+'</td></tr>\n\
                        <tr class="visible-xs"><td>'+msg.sender+': '+msg.message+'</td></tr>');
    $chatWindow.append($appendedLine);
}

function sendMessage() {
    var msg = '{"message":"' + $scope.message + '", "sender":"'
                    + nickName + '", "received":""}';
    wsocket.send(msg);
    $scope.message = "";
}

function connectToChatserver() {
    console.log(serviceLocation + room);
    wsocket = new WebSocket(serviceLocation + room);
    console.log("> ok");
    wsocket.onmessage = onMessageReceived;
    console.log("> connected to Web Socket server");
}


$scope.submitMessage = function() {
    sendMessage();
};


connectToChatserver();

UPDATE: local test

I tried to mirror the server to the local machine and test it locally without Nginx and it worked. Did I miss anything on Nginx, and it might be the cause of the issue ? Verified the proxy pass ip and port and they check out.

Upvotes: 2

Views: 821

Answers (1)

ashcrok
ashcrok

Reputation: 244

Ok, I found out what was wrong and I want to share. The problem was the deployment to a glassfish 3.1.4 server with jdk 7 from a build made on a glassfish 4.1 with jdk 8 server. Ofc the issue occurred because of the jdk version, but because of the server version difference, the server didn't throw any version exception issues so I didn't see where the problem was coming from (forgot I was building from a jdk 8 server).

Upvotes: 1

Related Questions