Search in sources :

Example 51 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.

the class SessionInfoIT method testSessionInfo.

@Test
public void testSessionInfo() throws Exception {
    URI serverURI = new URI("ws://localhost:58080/cdi-webapp/");
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientSessionInfoSocket socket = new ClientSessionInfoSocket();
    container.connectToServer(socket, serverURI.resolve("sessioninfo"));
    assertThat("Await open", socket.openLatch.await(1, TimeUnit.SECONDS), is(true));
    socket.session.getBasicRemote().sendText("info");
    socket.messages.awaitEventCount(1, 2, TimeUnit.SECONDS);
    System.out.printf("socket.messages.size = %s%n", socket.messages.size());
    String msg = socket.messages.poll();
    System.out.printf("Message is [%s]%n", msg);
    assertThat("Message", msg, containsString("HttpSession = HttpSession"));
    socket.session.getBasicRemote().sendText("close");
    assertThat("Await close", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Test(org.junit.Test)

Example 52 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.

the class AnnotatedEchoTest method testEcho.

@Test
public void testEcho() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    AnnotatedEchoClient echoer = new AnnotatedEchoClient();
    Session session = container.connectToServer(echoer, serverUri);
    session.getBasicRemote().sendText("Echo");
    echoer.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Session(javax.websocket.Session) Test(org.junit.Test)

Example 53 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.

the class AnnotatedEndpointConfigTest method startEnv.

@BeforeClass
public static void startEnv() throws Exception {
    // Server
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    handler = new EchoHandler();
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setHandler(handler);
    server.setHandler(context);
    // Start Server
    server.start();
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("ws://%s:%d/", host, port));
    // Connect client
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    socket = new AnnotatedEndpointClient();
    session = container.connectToServer(socket, serverUri);
    Assert.assertThat("Session", session, notNullValue());
    config = socket.config;
    Assert.assertThat("EndpointConfig", config, notNullValue());
    Assert.assertThat("EndpointConfig", config, instanceOf(ClientEndpointConfig.class));
    ceconfig = (ClientEndpointConfig) config;
    Assert.assertThat("EndpointConfig", ceconfig, notNullValue());
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Example 54 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.

the class MisbehavingClassTest method testEndpointRuntimeOnOpen.

@SuppressWarnings("Duplicates")
@Test
public void testEndpointRuntimeOnOpen() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    EndpointRuntimeOnOpen socket = new EndpointRuntimeOnOpen();
    try (StacklessLogging logging = new StacklessLogging(EndpointRuntimeOnOpen.class, WebSocketSession.class)) {
        // expecting IOException during onOpen
        expectedException.expect(IOException.class);
        expectedException.expectCause(instanceOf(RuntimeException.class));
        container.connectToServer(socket, serverUri);
        expectedException.reportMissingExceptionWithMessage("Should have failed .connectToServer()");
        assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
        Throwable cause = socket.errors.pop();
        assertThat("Error", cause, instanceOf(RuntimeException.class));
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 55 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.

the class EndpointEchoTest method testAbstractEchoInstance.

@Test
public void testAbstractEchoInstance() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    EchoStringEndpoint echoer = new EchoStringEndpoint();
    Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
    // Issue connect using instance of class that extends abstract that extends Endpoint
    Session session = container.connectToServer(echoer, serverUri);
    if (LOG.isDebugEnabled())
        LOG.debug("Client Connected: {}", session);
    session.getBasicRemote().sendText("Echo");
    if (LOG.isDebugEnabled())
        LOG.debug("Client Message Sent");
    echoer.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) EchoStringEndpoint(org.eclipse.jetty.websocket.jsr356.samples.EchoStringEndpoint) EchoStringEndpoint(org.eclipse.jetty.websocket.jsr356.samples.EchoStringEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)85 URI (java.net.URI)52 Session (javax.websocket.Session)52 Test (org.junit.Test)51 Context (org.apache.catalina.Context)29 Tomcat (org.apache.catalina.startup.Tomcat)29 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)27 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 DeploymentException (javax.websocket.DeploymentException)13 Endpoint (javax.websocket.Endpoint)13 EndpointConfig (javax.websocket.EndpointConfig)13 ServerEndpoint (javax.websocket.server.ServerEndpoint)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)10 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)10 IOException (java.io.IOException)8 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)8 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)7 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 PrintWriter (java.io.PrintWriter)6