users@glassfish.java.net

[gf-users] Re: 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 17:24:47 +0100

Hi Pavel,

that project doesn't have web.xml.

I've created a smallest project to reproduce the issue and deployed it to
server test.heroesofcadwick.com - the issue is same and still existing.

The project has two sourcefiles and I'm attaching whole Netbeans project
with sources.

If you deploy it to test.oracle.com (for example), you can run from your
browser:
http://test.oracle.com/?url=ws://test.oracle.com/web/test
(that one shows Websocket connection fails at my test.heroesofcadwick.com)
but
http://localhost:8080/Websockets/?url=ws://localhost:8080/Websockets/web/test
is failing

Attached Netbeans project source containing two small source files:

*index.jsp*
<%_at_page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Websockets Test</title>
    </head>
    <body>
        <h1>Websockets Test</h1>
    </body>

        <script>

function print(str) {
    console.log(str);
    var para = document.createElement("P");
    var t = document.createTextNode(str);
    para.appendChild(t);
    document.body.appendChild(para);
}

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

function onOpen() {
    print("Connected to " + wsUri);
}

function onClose() {
    print("Closed connection to " + wsUri);
}

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

<%
String url = request.getParameter("url");
if (url == null) {
    url = "ws://test.heroesofcadwick.com/web/test";
}
%>
var wsUri = "<%=url%>";
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>

    </body>
</html>


*TestWSEndPoint.java*
package websocketsTest;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.CloseReason;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

/**
 *
 * @author mladen
 */
@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 String onMessage(String message, @PathParam("battle_id") String
battle_id, @PathParam("secret") String secret, Session session) {
//
// }

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



}




On Thu, Feb 12, 2015 at 1:10 PM, Pavel Bucek <pavel.bucek_at_oracle.com> wrote:

> Hi Mladen,
>
> can you share web.xml or ideally whole project?
>
> Thanks,
> Pavel
>
> On 12/02/15 12:29, Mladen Adamović wrote:
>
>> 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
>> <http://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;
>> style="font-family:monospace"> <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
>> <http://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>
>>
>>