Example usage for javax.websocket ContainerProvider getWebSocketContainer

List of usage examples for javax.websocket ContainerProvider getWebSocketContainer

Introduction

In this page you can find the example usage for javax.websocket ContainerProvider getWebSocketContainer.

Prototype

public static WebSocketContainer getWebSocketContainer() 

Source Link

Document

Create a new container used to create outgoing WebSocket connections.

Usage

From source file:com.josue.tiled2.client.network.WSNetworkClient.java

public WSNetworkClient(String host, int port, String map) {
    this.container = ContainerProvider.getWebSocketContainer();
    String url = "ws://" + host + ":" + port + "/game/" + map;
    try {/*  w w  w .j a  va2s. c  o m*/
        endpointURI = new URI(url);
    } catch (URISyntaxException ex) {
        System.err.println(":: NETWORK ERROR, MALFORMED URI SYNTAX: " + url + " ::");
    }

    mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    mapper.setSerializationInclusion(Include.NON_NULL);

}

From source file:org.cloudfoundry.metron.MetronMetricWriter.java

MetronMetricWriter(URI uri) throws IOException, DeploymentException {
    ContainerProvider.getWebSocketContainer().connectToServer(this, uri);
}

From source file:io.hosuaby.restful.simulators.TeapotSimulator.java

/**
 * Creates and starts teapot simulator.//w w w.  ja v  a 2 s.  c om
 *
 * @param teapot    teapot domain object
 * @param port      port number of the server
 *
 * @throws URISyntaxException
 * @throws DeploymentException
 * @throws IOException
 */
public TeapotSimulator(Teapot teapot, int port) throws URISyntaxException, DeploymentException, IOException {

    /* Get websocket container */
    final WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    /* Configuration of teapot client endpoint */
    final ClientEndpointConfig teapotConfig = ClientEndpointConfig.Builder.create().build();

    /* Disable websocket timeout */
    container.setDefaultMaxSessionIdleTimeout(0);

    URI uri = new URI(String.format(REGISTER_URL, port, teapot.getId()));

    /* Create websocket client for the teapot */
    container.connectToServer(new TeapotSimulatorEndpoint(this), teapotConfig, uri);

    /* Create the file system */
    fs = new TeapotFs();

    /* Create help.txt file */
    fs.cat("help.txt", createHelpFileContent());

    /* Create license file */
    fs.cat("license", createLicenseFileContent());

    /* Create config.json file */
    fs.cat("config.json", createConfigFileContent(teapot));
}

From source file:org.bubblecloud.vr4java.rpc.RpcWsClient.java

/**
 * The server URI./* w w  w . j a v a  2  s . com*/
 * @param uri the uri
 * @param rpcHandler
 * @param messageHandler
 */
public RpcWsClient(final String identity, URI uri, Object rpcHandler, MessageHandler messageHandler) {
    super();
    this.uri = uri;
    this.server = new JsonRpcServer(
            new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true),
            rpcHandler);
    this.messageHandler = messageHandler;
    this.container = ContainerProvider.getWebSocketContainer();
    sealer = new RpcSealerImpl(identity);
}

From source file:org.atteo.moonshine.websocket.jsonmessages.JsonMessages.java

public static Factory factory() {
    final HandlerDispatcher dispatcher = new HandlerDispatcher();
    return new Factory() {
        @Override/*w  w w.j  a v a  2 s  .  c  om*/
        public <T> Factory addHandler(T handler) {
            dispatcher.addHandler(handler);
            return this;
        }

        @Override
        public <T> SenderProvider<T> addSender(Class<T> senderClass) {
            return dispatcher.addSender(senderClass);
        }

        @Override
        public Session connect(URI uri) {
            ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();

            try {
                return ContainerProvider.getWebSocketContainer()
                        .connectToServer(new JsonMessagesEndpoint(dispatcher), config, uri);
            } catch (DeploymentException | IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        @Override
        public Session connect(WebServerAddress webServerAddress) throws URISyntaxException {
            String path = "/json-messages";
            URI uri = new URI(webServerAddress.getUrl().replace("http://", "ws://") + path);
            return connect(uri);
        }
    };
}

From source file:dk.dma.navnet.server.AbstractServerConnectionTest.java

protected TesstEndpoint newClient() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    TesstEndpoint t = new TesstEndpoint();
    container.connectToServer(t, new URI("ws://localhost:" + clientPort));
    allClient.add(t);// ww  w .jav  a  2 s  .  co m
    return t;
}

From source file:org.sample.client.TestLocalClient.java

/**
 * Processes requests for both HTTP/* ww  w  .j ava2s .com*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:8080" + request.getContextPath() + "/websocket";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class, URI.create(uri));
        out.println("<br><br>Look in server.log for log messages from message exchange between client/server.");

        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestLocalClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.sample.client.TestRemoteClient.java

/**
 * Processes requests for both HTTP/* ww w.  jav  a  2  s  .  co  m*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestRemoteClient</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestRemoteClient at " + request.getContextPath() + "</h1>");

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://echo.websocket.org:80/";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class, URI.create(uri));
        out.println("<br><br>Look in server.log for log messages from message exchange between client/server.");

        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestRemoteClient.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        out.close();
    }
}

From source file:org.sample.client.TestClient.java

/**
 * Processes requests for both HTTP/*w  w  w.  j  a  va2  s . co  m*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");

        List<Class<? extends Encoder>> encoders = new ArrayList<>();
        encoders.add(MyMessageEncoder.class);
        List<Class<? extends Decoder>> decoders = new ArrayList<>();
        decoders.add(MyMessageDecoder.class);

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:8080" + request.getContextPath() + "/websocket";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class,
                ClientEndpointConfig.Builder.create().encoders(encoders).decoders(decoders).build(),
                URI.create(uri));
        out.println("<br><br>Look in server.log for message exchange between client/server.");

        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.codeveo.lago.bot.stomp.client.AbstractHttpLagoBot.java

protected AbstractHttpLagoBot() {
    objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    /* jaxb only */
    JaxbAnnotationModule module = new JaxbAnnotationModule();
    objectMapper.registerModule(module);

    taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setMaxPoolSize(THREAD_POOL_SIZE);
    taskExecutor.initialize();/* w ww.  j  a v  a 2s .c  o  m*/

    WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
    webSocketContainer.setDefaultMaxSessionIdleTimeout(CLIENT_MAX_IDLE_TIMEOUT);

    webSocketClient = new StandardWebSocketClient(webSocketContainer);
    webSocketClient.setTaskExecutor(taskExecutor);
}