Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] configuration of Jetty for JSR-356

Hiya Brad.

Sorry for the late reply (was out of town).

Using JSR-356 isn't like the Jetty WebSocket API.
It is heavily based on servlet 3.1 concepts.

Such as ServetContainerInitializer and Annotation scanning.

Here's an example of using JSR-356 from Jetty Embedded.

package jetty.jsr356;

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

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jsr356.server.ServerContainer;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;

@ServerEndpoint(value="/echo")
public class ExampleServer
{
    @OnMessage
    public String onMessage(String msg)
    {
        // echo it back
        return msg;
    }
    
    public static void main(String[] args)
    {
        Server server = new Server(8080);
        
        try
        {
            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/");
            server.setHandler(context);
            
            // Initialize the JSR-356 layer
            ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
            // Manually add the endpoint
            container.addEndpoint(ExampleServer.class);
            
            server.start(); // start server
            server.join(); // wait for server to stop
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

However, if you are using JSR-356 from a Webapp/War on a standalone Jetty installation, you have the following choices to register your endpoints with the server.
  1. Annotate your Endpoint with @ServerEndpoint and rely on jetty annotation scanning to find it at deploy it.
  2. Create a javax.websocket.server.ServerApplicationConfig instance and return the Endpoints from the methods appropriate to your webapp.
  3. Create a javax.servlet.ServletContainerInitializer for your webapp, and in its onStartup() method access the javax.websocket.server.ServerContainer and use its various addEndpoint() methods.
    public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException
    {
        ServerContainer container = (ServerContainer)context.getAttribute("javax.websocket.server.ServerContainer");
        try
        {
            container.addEndpoint(EchoSocket.class);
        }
        catch (DeploymentException e)
        {
            throw new ServletException("Unable to deploy websocket", e);
        }
    }

Find one technique that suits you the best, and try not to mix and match the 3 various techniques.

Hope this helps


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


On Sun, Nov 10, 2013 at 5:08 PM, Brad McEvoy <brad@xxxxxxxxxxxxxxxxxxxx> wrote:
Hi All,

I'm using Jetty 9.1.0.RC0 from maven and would like to use JSR-356 websockets, but i'm a little confused about what i need to do to make this work.

I've found this example which shows programmatic configuration - http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/examples/embedded/src/main/java/org/eclipse/jetty/embedded/WebSocketJsrServer.java?h=jetty-9.1

With these magic lines:
ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext(context);

// Add your websockets to the container
wsContainer.addEndpoint(EchoJsrSocket.class);


But i'm launching from maven using the jetty plugin so not quite sure how that should fit into things.

Do i need a startup listener or something? Or is there some XML to do the config? I'm guessing it would need to go into my pom.xml, rather then web.xml

Thanks,
Brad

_______________________________________________
jetty-dev mailing list
jetty-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-dev



Back to the top