Search in sources :

Example 1 with ServerContainer

use of javax.websocket.server.ServerContainer in project jetty.project by eclipse.

the class BasicEchoEndpointConfigContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    if (container == null)
        throw new IllegalStateException("No Websocket ServerContainer in " + sce.getServletContext());
    // Build up a configuration with a specific path
    String path = "/echo";
    ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(BasicEchoEndpoint.class, path);
    try {
        container.addEndpoint(builder.build());
    } catch (DeploymentException e) {
        throw new RuntimeException("Unable to add endpoint via config file", e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Example 2 with ServerContainer

use of javax.websocket.server.ServerContainer in project jetty.project by eclipse.

the class BasicEchoEndpointContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    try {
        container.addEndpoint(ServerEndpointConfig.Builder.create(PongMessageEndpoint.class, "/ping").build());
        container.addEndpoint(ServerEndpointConfig.Builder.create(PongMessageEndpoint.class, "/pong").build());
    } catch (DeploymentException e) {
        throw new RuntimeException("Unable to add endpoint via config file", e);
    }
}
Also used : DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Example 3 with ServerContainer

use of javax.websocket.server.ServerContainer in project AngularBeans by bessemHmidi.

the class AngularBeansServletContextListener method initJSR356.

public void initJSR356() throws ServletException {
    sockJsServer = new SockJsServer();
    sockJsServer.init();
    if (sockJsServer.options.websocket) {
        // Make sure we listen on all possible mappings of the servlet
        // for (String mapping :
        // getServletContext().getServletRegistration(context.getServletName()).getMappings())
        // {
        final String commonPrefix = extractPrefixFromMapping("/rt-service/*");
        // 
        String websocketPath = commonPrefix + "/{server}/{session}/websocket";
        ServerEndpointConfig sockJsConfig = ServerEndpointConfig.Builder.create(SockJsEndpoint.class, websocketPath).configurator(configuratorFor(commonPrefix, false)).build();
        // rt-service/websocket
        String rawWebsocketPath = commonPrefix + "/websocket";
        ServerEndpointConfig rawWsConfig = ServerEndpointConfig.Builder.create(RawWebsocketEndpoint.class, rawWebsocketPath).configurator(configuratorFor(commonPrefix, true)).build();
        ServerContainer serverContainer = (ServerContainer) context.getAttribute("javax.websocket.server.ServerContainer");
        try {
            serverContainer.addEndpoint(sockJsConfig);
            serverContainer.addEndpoint(rawWsConfig);
            Logger.getLogger(this.getClass().getSimpleName()).info("deployement of programmatic Web socket EndPoint :" + rawWebsocketPath);
        } catch (DeploymentException ex) {
            throw new ServletException("Error deploying websocket endpoint:", ex);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) SockJsServer(org.projectodd.sockjs.SockJsServer) ServerContainer(javax.websocket.server.ServerContainer)

Example 4 with ServerContainer

use of javax.websocket.server.ServerContainer in project undertow by undertow-io.

the class BinaryEndpointServlet method init.

@Override
public void init(ServletConfig c) throws ServletException {
    String websocketPath = "/partial";
    ServerEndpointConfig config = ServerEndpointConfig.Builder.create(BinaryPartialEndpoint.class, websocketPath).build();
    ServerContainer serverContainer = (ServerContainer) c.getServletContext().getAttribute("javax.websocket.server.ServerContainer");
    try {
        serverContainer.addEndpoint(config);
    } catch (DeploymentException ex) {
        throw new ServletException("Error deploying websocket endpoint:", ex);
    }
}
Also used : ServletException(javax.servlet.ServletException) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Example 5 with ServerContainer

use of javax.websocket.server.ServerContainer in project quickstart by wildfly.

the class Frontend method init.

public void init(@Observes @Initialized(ApplicationScoped.class) ServletContext servletContext) {
    if (servletContext == null) {
        log.severe(format("Failed to deploy frontend endpoint %s: %s", WEBSOCKET_PATH, "ServletContext not available"));
        return;
    }
    ServerContainer serverContainer = (ServerContainer) servletContext.getAttribute("javax.websocket.server.ServerContainer");
    if (serverContainer == null) {
        log.severe(format("Failed to deploy frontend endpoint %s: %s", WEBSOCKET_PATH, "javax.websocket.server.ServerContainer ServerContainer not available"));
        return;
    }
    // WebSocket does not honor CDI contexts in the default configuration; by default a new object is created for each new
    // websocket.
    // We can work around this by supplying a custom ServerEndpointConfig.Configurator that returns the same instance of the
    // endpoint for each new websocket. Unfortunately this precludes us from using annotations, and forces us to extend the
    // abstract class Endpoint, and forces us to add the server endpoint programmatically upon startup of the servlet
    // context.
    ServerEndpointConfig config = ServerEndpointConfig.Builder.create(Frontend.class, WEBSOCKET_PATH).configurator(new ServerEndpointConfig.Configurator() {

        @SuppressWarnings("unchecked")
        @Override
        public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException {
            if (endpointClass.isAssignableFrom(Frontend.class)) {
                return (T) Frontend.this;
            }
            return super.getEndpointInstance(endpointClass);
        }
    }).build();
    try {
        serverContainer.addEndpoint(config);
    } catch (DeploymentException e) {
        log.log(Level.SEVERE, format("Failed to deploy frontend endpoint %s: %s", WEBSOCKET_PATH, e.getMessage()), e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Aggregations

ServerContainer (javax.websocket.server.ServerContainer)16 DeploymentException (javax.websocket.DeploymentException)11 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)9 ServletException (javax.servlet.ServletException)4 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 File (java.io.File)3 Server (org.eclipse.jetty.server.Server)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ServletContext (javax.servlet.ServletContext)2 EmbeddedCdiHandler (org.eclipse.jetty.cdi.servlet.EmbeddedCdiHandler)2 BeforeClass (org.junit.BeforeClass)2 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1