37

I hate asking such a vague question, but I'm having a hard time finding a simple example. Here's what I have so far:

public class JettyWebSocketServlet extends WebSocketServlet{
    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.register(MyEchoSocket.class);
    }
}

@WebSocket
public class MyEchoSocket {
    @OnWebSocketMessage
    public void onText(WebSocketConnection conn, String message) {
        System.out.println("text: " + message);
        try {
            conn.write(null, new FutureCallback(), "got: " + message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The embedded Jetty examples I can find always show something like the following, to start a Server instance running, but I don't know how to instantiate my WebSocketServlet.

        Server server = new Server(8080);
        server.start();
        server.join();

How do I create an embedded server that can handle WebSocket connection requests?

Alex Pritchard
  • 4,260
  • 5
  • 33
  • 48

2 Answers2

41

Update: Dec 2, 2013

For an up to date example of the Embedded Jetty with WebSocket see:

https://github.com/jetty-project/embedded-jetty-websocket-examples

Original Answer

There's an example found in the test cases.

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/ExampleEchoServer.java

Short Answer:

Server server = new Server(8080);
WebSocketHandler wsHandler = new WebSocketHandler()
    {
        @Override
        public void configure(WebSocketServletFactory factory)
        {
            factory.register(MyEchoSocket.class);
        }
    };
server.addHandler(wsHandler);
server.start();
server.join();

This will create a simple server that handles 1 context, the root context.

http://localhost:8080/

If you want to bind the WebSocketHandler to another context, wrap it in a ContextHandler.

Server server = new Server(8080);
WebSocketHandler wsHandler = new WebSocketHandler()
    {
        @Override
        public void configure(WebSocketServletFactory factory)
        {
            factory.register(MyEchoSocket.class);
        }
    };
ContextHandler context = new ContextHandler();
context.setContextPath("/echo");
context.setHandler(wsHandler);
server.addHandler(context);
server.start();
server.join();

This will bind your websocket to

http://localhost:8080/echo/
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • 4
    +1 for the update which includes an example with JSR-356 – betomontejo Mar 21 '14 at 15:51
  • I'm trying to write a test with junit. Starting an embedded jetty server with the websocket servlet seems to work. However trying to connect to it with ```container.connectToServer(...)``` throws java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@70cebf7 – Dominic Jul 13 '14 at 19:36
  • @DominicBartl file a new question – Joakim Erdfelt Jul 29 '14 at 01:23
  • @JoakimErdfelt I did http://stackoverflow.com/questions/24726816/junit-test-with-javax-websocket-on-embedded-jetty-throws-rejectedexecutionexcept – Dominic Jul 29 '14 at 15:21
  • it would be nice to include a code snippet to show how to implement the `MyEchoSocket` class. –  Apr 05 '21 at 18:33
7

Here is servlet approach:

1) In this case, don't need to use this code (it is container responsibility):

Server server = new Server(8080);
                server.start();
                server.join();

2) Create web.xml descriptor for servlet:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>WebSocket application</display-name>
    <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>com.example.websocket.JettyWebSocketServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Now, when you write JS:

var ws = new WebSocket("ws://localhost:8080/hello");
ws.send("text");

message is handled with onText method of your MyEchoSocket

Here is tutorial for more understanding servlets, tutorial

Good luck.

Artyom Chernetsov
  • 1,394
  • 4
  • 17
  • 34