Search in sources :

Example 16 with WebSocketCreator

use of org.eclipse.jetty.websocket.servlet.WebSocketCreator in project joynr by bmwcarit.

the class WebSocketJettyServer method start.

@Override
public void start() {
    if (server != null && server.isRunning()) {
        return;
    }
    server = new Server();
    ServerConnector connector;
    if (address.getProtocol().equals(WebSocketProtocol.WSS)) {
        SslContextFactory sslContextFactory = new SslContextFactory();
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
        connector = new ServerConnector(server, sslConnectionFactory);
    } else {
        connector = new ServerConnector(server);
    }
    connector.setPort(address.getPort());
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    ServletHolder holderEvents = new ServletHolder("ws-events", new WebSocketServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        public void configure(WebSocketServletFactory webSocketServletFactory) {
            webSocketServletFactory.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
            webSocketServletFactory.getPolicy().setMaxTextMessageSize(maxMessageSize);
            webSocketServletFactory.setCreator(new WebSocketCreator() {

                @Override
                public Object createWebSocket(ServletUpgradeRequest servletUpgradeRequest, ServletUpgradeResponse servletUpgradeResponse) {
                    CCWebSocketMessagingSkeletonSocket socket = new CCWebSocketMessagingSkeletonSocket(WebSocketJettyServer.this);
                    openSockets.add(socket);
                    return socket;
                }
            });
        }
    });
    context.addServlet(holderEvents, address.getPath());
    try {
        server.start();
    } catch (Exception t) {
        logger.error("Error while starting websocket server: ", t);
    }
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) WebSocketServlet(org.eclipse.jetty.websocket.servlet.WebSocketServlet) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IOException(java.io.IOException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)

Aggregations

WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)16 ServletUpgradeRequest (org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)6 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)6 PathSpec (org.eclipse.jetty.http.pathmap.PathSpec)5 WebSocketServletFactory (org.eclipse.jetty.websocket.servlet.WebSocketServletFactory)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Server (org.eclipse.jetty.server.Server)3 NativeWebSocketConfiguration (org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration)3 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)3 IOException (java.io.IOException)2 ServletContext (javax.servlet.ServletContext)2 ServletPathSpec (org.eclipse.jetty.http.pathmap.ServletPathSpec)2 ServerConnector (org.eclipse.jetty.server.ServerConnector)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 WebSocketServerFactory (org.eclipse.jetty.websocket.server.WebSocketServerFactory)2 WebSocketServlet (org.eclipse.jetty.websocket.servlet.WebSocketServlet)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 InstrumentedQueuedThreadPool (com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool)1