Search in sources :

Example 11 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestWsWebSocketContainer method doTestPerMessageDeflateClient.

private void doTestPerMessageDeflateClient(String msg, int count) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME);
    List<Extension> extensions = new ArrayList<>(1);
    extensions.add(perMessageDeflate);
    ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().extensions(extensions).build();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientConfig, new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(count);
    BasicText handler = new BasicText(latch, msg);
    wsSession.addMessageHandler(handler);
    for (int i = 0; i < count; i++) {
        wsSession.getBasicRemote().sendText(msg);
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    ((WsWebSocketContainer) wsContainer).destroy();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Extension(javax.websocket.Extension) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session)

Example 12 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestWsWebSocketContainer method doMaxMessageSize.

private void doMaxMessageSize(String path, long size, boolean expectOpen) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session s = connectToEchoServer(wsContainer, new EndpointA(), path);
    // One for the client, one for the server
    validateBackgroundProcessCount(2);
    StringBuilder msg = new StringBuilder();
    for (long i = 0; i < size; i++) {
        msg.append('x');
    }
    s.getBasicRemote().sendText(msg.toString());
    // Wait for up to 5 seconds for the client session to open
    boolean open = s.isOpen();
    int count = 0;
    while (open != expectOpen && count < 50) {
        Thread.sleep(100);
        count++;
        open = s.isOpen();
    }
    Assert.assertEquals(Boolean.valueOf(expectOpen), Boolean.valueOf(s.isOpen()));
    // Close the session if it is expected to be open
    if (expectOpen) {
        s.close();
    }
    // Ensure both server and client have shutdown
    validateBackgroundProcessCount(0);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Session(javax.websocket.Session)

Example 13 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestWsWebSocketContainer method testGetOpenSessions.

@Test
public void testGetOpenSessions() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    EndpointA endpointA = new EndpointA();
    Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    EndpointB endpointB = new EndpointB();
    Session s1b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC);
    Session s2b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC);
    Set<Session> setA = s3a.getOpenSessions();
    Assert.assertEquals(3, setA.size());
    Assert.assertTrue(setA.remove(s1a));
    Assert.assertTrue(setA.remove(s2a));
    Assert.assertTrue(setA.remove(s3a));
    s1a.close();
    setA = s3a.getOpenSessions();
    Assert.assertEquals(2, setA.size());
    Assert.assertFalse(setA.remove(s1a));
    Assert.assertTrue(setA.remove(s2a));
    Assert.assertTrue(setA.remove(s3a));
    Set<Session> setB = s1b.getOpenSessions();
    Assert.assertEquals(2, setB.size());
    Assert.assertTrue(setB.remove(s1b));
    Assert.assertTrue(setB.remove(s2b));
    // Close sessions explicitly as Gump reports a session remains open at
    // the end of this test
    s2a.close();
    s3a.close();
    s1b.close();
    s2b.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session) Test(org.junit.Test)

Example 14 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestWsWebSocketContainer method doTestWriteTimeoutClient.

private void doTestWriteTimeoutClient(boolean setTimeoutOnContainer) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(BlockingConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // Set the async timeout
    if (setTimeoutOnContainer) {
        wsContainer.setAsyncSendTimeout(TIMEOUT_MS);
    }
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + BlockingConfig.PATH));
    if (!setTimeoutOnContainer) {
        wsSession.getAsyncRemote().setSendTimeout(TIMEOUT_MS);
    }
    long lastSend = 0;
    // Should send quickly until the network buffers fill up and then block
    // until the timeout kicks in
    Exception exception = null;
    try {
        while (true) {
            lastSend = System.currentTimeMillis();
            Future<Void> f = wsSession.getAsyncRemote().sendBinary(ByteBuffer.wrap(MESSAGE_BINARY_4K));
            f.get();
        }
    } catch (Exception e) {
        exception = e;
    }
    long timeout = System.currentTimeMillis() - lastSend;
    // Clear the server side block and prevent further blocks to allow the
    // server to shutdown cleanly
    BlockingPojo.clearBlock();
    // Close the client session, primarily to allow the
    // BackgroundProcessManager to shut down.
    wsSession.close();
    String msg = "Time out was [" + timeout + "] ms";
    // Check correct time passed
    Assert.assertTrue(msg, timeout >= TIMEOUT_MS - MARGIN);
    // Check the timeout wasn't too long
    Assert.assertTrue(msg, timeout < TIMEOUT_MS * 2);
    Assert.assertNotNull(exception);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) DeploymentException(javax.websocket.DeploymentException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Session(javax.websocket.Session)

Example 15 with WebSocketContainer

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

the class StreamTest method upload.

private void upload(String filename) throws Exception {
    File inputFile = MavenTestingUtils.getTestResourceFile("data/" + filename);
    WebSocketContainer client = ContainerProvider.getWebSocketContainer();
    ClientSocket socket = new ClientSocket();
    URI uri = serverUri.resolve("/upload/" + filename);
    client.connectToServer(socket, uri);
    socket.uploadFile(inputFile);
    socket.awaitClose();
    File sha1File = MavenTestingUtils.getTestResourceFile("data/" + filename + ".sha");
    assertFileUpload(new File(outputDir, filename), sha1File);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) File(java.io.File) URI(java.net.URI)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)67 URI (java.net.URI)46 Session (javax.websocket.Session)42 Test (org.junit.Test)39 Context (org.apache.catalina.Context)32 Tomcat (org.apache.catalina.startup.Tomcat)32 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)30 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 Endpoint (javax.websocket.Endpoint)14 ServerEndpoint (javax.websocket.server.ServerEndpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 EndpointConfig (javax.websocket.EndpointConfig)11 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)9 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)9 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)9 ClientEndpoint (javax.websocket.ClientEndpoint)8 DeploymentException (javax.websocket.DeploymentException)8 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 ServerConfigListener (org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener)7