users@glassfish.java.net

[gf-users] Websockets: got 404 on server, but works on localhost (glassfish 4.1 latest)

From: Mladen Adamović <mladen.adamovic_at_gmail.com>
Date: Thu, 12 Feb 2015 12:29:46 +0100

I have difficulties getting Websockets to work for me.

What to check apart that http-listener-1 has Websockets enabled (it is
indeed enabled) ?

this is simple example which works at localhost and doesn't work in
production.
Ordinary servlets do work in production (running on port 8080 and I have
iptables port forwarding to port 80).


This is my testing endpoint:
@ServerEndpoint(value = "/web/test")
public class TestWSEndpoint {

  @OnOpen
   public void open(Session session,
                    EndpointConfig c) throws IOException {
   }

   @OnClose
public void close(Session session,
                  CloseReason reason) {
    Logger.getLogger(this.getClass().getName()).log(Level.INFO, "close {0}
cause {1}", new Object[] { reason.toString(), reason.getReasonPhrase() } );
}

   @OnMessage
   public void onMessage(String message) {
     Logger.getLogger(this.getClass().getName()).log(Level.INFO, "server
onMessage {0}", new Object[] { message });
   }


I can access it from localhost, but trying to open it from
ws://www.heroesofcadwick.com/web/test
it doesn't work.
(I'm using have a simple HTML page at:
http://www.heroesofcadwick.com/test_ws.jsp
) to access websockets.

That page has this content:

<%_at_page import="heroes.Configuration"%>
<%_at_page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>

        <script>
function onError(evt) {
    console.log('<span style="color: red;">ERROR:</span> ' + evt.data);
}

function onOpen() {
    console.log("Connected to " + wsUri);
}

function onClose() {
    console.log("Closed connection to " + wsUri);
}

function sendText(json) {
    console.log("sending text: " + json);
    websocket.send(json);
}

<%
  if (!Configuration.isRunningLocally()) {
    %>
var wsUri = "ws://www.heroesofcadwick.com/web/test";
    <%
  } else {
    %>
var wsUri = "ws://localhost:8080/Heroes/web/test";
    <%
  }
    %>

var websocket = new WebSocket(wsUri);
websocket.onerror = function(evt) { onError(evt) };
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
</script>

    </script>
</html>