3

I am trying to create javaee application with websockets however can't get the program to work properly. Using Tomcat 7, Java EE 7 application with websockets.

Here is my java code:

import javax.websocket.CloseReason;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/hello")
public class EchoEndpoint {
    RemoteEndpoint.Async endpoint;
    @OnOpen
    public void open(Session session, EndpointConfig conf) { 
        //Connection opened.
        System.out.println("EchoEndpoint on open");
        endpoint = session.getAsyncRemote(); 
    }

    @OnMessage
    public void onMessage(Session session, String msg) {
        //Message received.
        System.out.println("EchoEndpoint on message");
    }

    @OnError
    public void error(Session session, Throwable error) { 
        //Connection error.
        System.out.println("EchoEndpoint on error");
    }

    @OnClose
    public void close(Session session, CloseReason reason) { 
        //Connection closed.
        System.out.println("EchoEndpoint on close");
    }

    public void send(String string) {
        System.out.println("Send called to EchoEndpoint");
        endpoint.sendText(string);
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app
        version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

</web-app>

index.html:

<!DOCTYPE html>  
    <meta charset="utf-8" />  
    <title>WebSocket Test</title>  
    <script language="javascript" type="text/javascript">  
        var wsUri = "ws://localhost:8080/myapp/hello"; 
        var output;  
        function init() { 
            output = document.getElementById("output"); 
            testWebSocket(); 
        }  

        function testWebSocket() { 
            websocket = new WebSocket(wsUri);
            websocket.onopen = function(evt) { onOpen(evt) };
            websocket.onclose = function(evt) { onClose(evt) }; 
            websocket.onmessage = function(evt) { onMessage(evt) }; 
            websocket.onerror = function(evt) { onError(evt) }; 
        }  

        function onOpen(evt) { 
            writeToScreen("CONNECTED"); 
            doSend("WebSocket rocks"); 
        }  

        function onClose(evt) { 
            writeToScreen("DISCONNECTED"); 
        }  

        function onMessage(evt) { 
            writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); 
            //websocket.close(); 
        }  

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

        function doSend(message) { 
            writeToScreen("SENT: " + message);  
            websocket.send(message); 
        }  

        function writeToScreen(message) { 
            var pre = document.createElement("p"); 
            pre.style.wordWrap = "break-word"; 
            pre.innerHTML = message; 
            output.appendChild(pre); 
        }  

        window.addEventListener("load", init, false);  
    </script>  

    <h2>WebSocket Test</h2>  
    <div id="output"></div>  
</html>

On client side when trying to execute the :

websocket = new WebSocket(wsUri);

I get:

Websocket connection to ws://localhost:8080/myapp/hello failed: Unexpected responce code: 404

Did I miss something in web.xml or somewhere else?

maximus
  • 4,201
  • 15
  • 64
  • 117
  • The URL in the `@ServerEndpoint` is relative to the servlet's root, but I'm not seeing anywhere that you're placing the servlet under `myapp`. Does `ws://localhost:8080/hello` work? – chrylis -cautiouslyoptimistic- Aug 23 '13 at 13:14
  • @chrylis No it doesn't I have already tried it – maximus Aug 23 '13 at 13:16
  • I'm not familiar with WebSockets, but I think the issue is that you don't have a `WebSocketServlet` that's the "owner" for the endpoint and that your endpoint class isn't getting attached anywhere. – chrylis -cautiouslyoptimistic- Aug 23 '13 at 13:21
  • @chrylis I was reading this document where nothing about WebSocketServlet was said: http://docs.oracle.com/javaee/7/tutorial/doc/websocket.htm – maximus Aug 23 '13 at 14:04
  • 1
    @chrylis the reason was that the java ee 7 and tomcat 7 are not compatible. Tomcat 8 should be used instead.. – maximus Aug 27 '13 at 12:45
  • Just to bring this up to date. Tomcat 7.0.47 *does* support websockets 1.0 (released 24th Oct 2013), backported from 8.0 – user2989739 Nov 13 '13 at 22:07

1 Answers1

7

WebSocket is implemented in Tomcat 7 by extending WebSocketServlet. But WebSocket 1.0 API introduced in tomcat 8. Your code is not compatible with Tomcat 7. Please make use of tomcat 8 and try the same.

Update...

Now, Tomcat7 supports websocket as @samael said

Susai
  • 565
  • 4
  • 12
  • 2
    WebSocket 1.0 can now be used in Tomcat 7. This is for version 7.0.47 and above only when using Java 7. The old way of extending `WebSocketServlet` is deprecated. – samael Dec 06 '13 at 11:54
  • It would be awesome to understand precisely which versions support which solution. – ash Jul 31 '14 at 22:48