Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Minimal Jsr 356 server setup

Here ya go ...

Note, you will need to use Jetty 9.1 (which is currently under development in the jetty-9.1 branch)
Expect milestone or RC releases before the week is out.

package example;

import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.jsr356.server.ServerContainer;
import org.eclipse.jetty.websocket.jsr356.server.WebSocketConfiguration;

 * Example of setting up a javax.websocket server with Jetty embedded
public class WebSocketJsrServer
     * A server socket endpoint
    @ServerEndpoint(value = "/echo")
    public static class EchoJsrSocket
        public void onMessage(Session session, String message)

    public static void main(String[] args) throws Exception
        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

        // Add a servlet to your context.
        // It is required that you provide at least 1 servlet.
        // Recommended that this servlet merely provide a
        // "This is a websocket only server" style response to GET requests
        context.addServlet(new ServletHolder(new DumpServlet()),"/*");

        // Enable javax.websocket configuration for the context
        ServerContainer wsContainer = WebSocketConfiguration.configureContext(context);

        // Add your websockets to the container

        context.dumpStdErr(); // show the context details

Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts

On Wed, Sep 11, 2013 at 4:21 AM, Kasper Nielsen <kasperni@xxxxxxxxx> wrote:


I'm looking for some simple sample code that sets up a simple server and deploys a Jsr 356 annotated endpoint.

On the client side I would do something like this:

WebSocketContainer container = ContainerProvider.getWebSocketContainer();

        Jsr356AnnotatedClient c = new Jsr356AnnotatedClient();

        Session session = container.connectToServer(c, new URI("ws://localhost:1234"));


jetty-users mailing list

Back to the top