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:co.paralleluniverse.comsat.webactors.AbstractWebActorTest.java

@Test
public void testWebSocketMsg()
        throws IOException, InterruptedException, ExecutionException, DeploymentException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    final HttpGet httpGet = new HttpGet("http://localhost:8080");
    HttpClients.custom().setDefaultRequestConfig(requestConfig).setDefaultCookieStore(cookieStore).build()
            .execute(httpGet, new BasicResponseHandler());

    final SettableFuture<String> res = new SettableFuture<>();
    final WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    wsContainer.setAsyncSendTimeout(timeout);
    wsContainer.setDefaultMaxSessionIdleTimeout(timeout);
    try (final Session ignored = wsContainer.connectToServer(sendAndGetTextEndPoint("test it", res),
            getClientEndPointConfig(cookieStore), URI.create("ws://localhost:8080/ws"))) {
        final String s = res.get();
        assertEquals("test it", s);
    }//  w  w w  . j  a v  a  2s . c om
}

From source file:eu.agilejava.snoop.scan.SnoopClient.java

/**
 * Sends message to the WebSocket server.
 *
 * @param endpoint The server endpoint//from   w  ww. ja va2  s.com
 * @param msg The message
 * @return a return message
 */
private String sendMessage(String endpoint, String msg) {

    LOGGER.config(() -> "Sending message: " + msg);

    String returnValue = "-1";
    try {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = serviceUrl + endpoint;
        Session session = container.connectToServer(this, URI.create(uri));
        session.getBasicRemote().sendText(msg != null ? msg : "");
        returnValue = session.getId();

    } catch (DeploymentException | IOException ex) {
        LOGGER.warning(ex.getMessage());
    }

    return returnValue;
}

From source file:io.undertow.js.test.cdi.CDIInjectionProviderDependentTestCase.java

@Test
public void testWebsocketInjection() throws Exception {
    Session session = ContainerProvider.getWebSocketContainer().connectToServer(ClientEndpointImpl.class,
            new URI("ws://" + DefaultServer.getHostAddress("default") + ":"
                    + DefaultServer.getHostPort("default") + "/cdi/websocket"));
    Assert.assertEquals("Barpong", messages.poll(5, TimeUnit.SECONDS));

    session.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, "foo"));
    Thread.sleep(1000);//from w  w w  . j av a  2 s.com
    assertEquals(Bar.DESTROYED.size(), 1);
}

From source file:com.raspoid.network.pushbullet.Pushbullet.java

/**
 * Constructor for a new Pushbullet instance with a specific access token, a device name
 * corresponding to the name that your robot will take in your Pushbullet list of devices,
 * and the Raspoid router to use with this Pushbullet instance.
 * /*from  w w  w . ja  v a  2  s .  com*/
 * <p>The access token can easily be retrieved from your Pushbullet account settings.</p>
 * 
 * <p>Note that if a device with the specified name already exists, this device will be
 * retrieved. If no devices with this name exists, a new one will be created.</p>
 * 
 * <p>As for other types of servers, the router is used to deal with requests
 * received on this Pushbullet instance.</p>
 * 
 * @param accessToken the access token used to access Pushbullet services.
 * @param deviceName the name corresponding to your robot's Pushbullet device.
 * @param router the router to use to deal with requests received on the deviceName.
 */
public Pushbullet(String accessToken, String deviceName, Router router) {
    this.accessToken = accessToken;

    gson = new Gson();

    this.deviceIden = initDevice(deviceName);
    this.lastPushReceivedTime = initLastPushReceivedTime();

    // WebSocket
    final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    WebSocketContainer websocketClient = ContainerProvider.getWebSocketContainer();
    try {
        session = websocketClient.connectToServer(new PushbulletClientEndpoint(router), clientEndpointConfig,
                new URI("wss://stream.pushbullet.com/websocket/" + accessToken));
    } catch (DeploymentException | IOException | URISyntaxException e) {
        throw new RaspoidException("Error when connecting to Pushbullet server.", e);
    }

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Websocket closed by client"));
        } catch (IOException e) {
            throw new RaspoidException("Error when closing the websocket session with Pushbullet.", e);
        }
    }));
}

From source file:com.github.mrstampy.gameboot.otp.websocket.OtpWebSocketTest.java

private void createClearChannel() throws Exception {
    ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
    config.getUserProperties().put(WsWebSocketContainer.SSL_CONTEXT_PROPERTY, sslContext);
    clearChannel = ContainerProvider.getWebSocketContainer().connectToServer(endpoint, config,
            new URI(createClearUriString()));

    assertTrue(clearChannel.isOpen());/*from   w ww.  ja  v a2s.  com*/

    CountDownLatch cdl = new CountDownLatch(1);
    endpoint.setResponseLatch(cdl);

    cdl.await(1, TimeUnit.SECONDS);

    assertNotNull(endpoint.getSystemId());
    assertEquals(clearChannel, endpoint.getSession());
}

From source file:com.github.mrstampy.gameboot.otp.websocket.OtpWebSocketTest.java

private void createEncryptedChannel() throws Exception {
    ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
    config.getUserProperties().put(WsWebSocketContainer.SSL_CONTEXT_PROPERTY, sslContext);
    encChannel = ContainerProvider.getWebSocketContainer().connectToServer(endpoint, config,
            new URI(createEncUriString()));

    assertTrue(encChannel.isOpen());/*from  www  .  j a v a 2s.c  o  m*/
}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Create session/*  w  w  w .ja v  a  2s .c om*/
 *
 * @param jsessionid
 * @param userpwd
 * @return
 */
protected Session createAndGetSession(String jsessionid, String userpwd) {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    try {
        StringBuilder sb = new StringBuilder("ws://localhost:");
        sb.append(PORT).append(Constants.SLASH).append(CTXPATH).append(Constants.SLASH)
                .append("ocelot-endpoint");
        URI uri = new URI(sb.toString());
        return container.connectToServer(new Endpoint() {
            @Override
            public void onOpen(Session session, EndpointConfig config) {
            }
        }, createClientEndpointConfigWithJsession(jsessionid, userpwd), uri);
    } catch (URISyntaxException | DeploymentException | IOException ex) {
        ex.getCause().printStackTrace();
        fail("CONNEXION FAILED " + ex.getMessage());
    }
    return null;
}

From source file:org.apache.hadoop.gateway.websockets.BadUrlTest.java

/**
 * Test websocket proxying through gateway.
 *
 * @throws Exception/*from   ww w .j  a  va  2s.  c o  m*/
 */

@Test
public void testBadUrl() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    WebsocketClient client = new WebsocketClient();

    container.connectToServer(client, new URI(serverUri.toString() + "gateway/websocket/ws"));

    client.awaitClose(CloseReason.CloseCodes.UNEXPECTED_CONDITION.getCode(), 1000, TimeUnit.MILLISECONDS);

    Assert.assertThat(client.close.getCloseCode().getCode(),
            CoreMatchers.is(CloseReason.CloseCodes.UNEXPECTED_CONDITION.getCode()));

}

From source file:org.apache.hadoop.gateway.websockets.WebsocketEchoTest.java

/**
 * Test direct connection to websocket server without gateway
 * /*from w  ww .  j  a v  a 2 s  .c o m*/
 * @throws Exception
 */
@Test
public void testDirectEcho() throws Exception {

    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    WebsocketClient client = new WebsocketClient();

    Session session = container.connectToServer(client, backendServerUri);

    session.getBasicRemote().sendText("Echo");
    client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);

}

From source file:org.apache.hadoop.gateway.websockets.WebsocketEchoTest.java

/**
 * Test websocket proxying through gateway.
 * /* w  w  w. j av a2  s.  c  o  m*/
 * @throws Exception
 */
@Test
public void testGatewayEcho() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    WebsocketClient client = new WebsocketClient();
    Session session = container.connectToServer(client, new URI(serverUri.toString() + "gateway/websocket/ws"));

    session.getBasicRemote().sendText("Echo");
    client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);

    assertThat(client.messageQueue.get(0), is("Echo"));

}