Search in sources :

Example 51 with Session

use of javax.websocket.Session in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testText.

@org.junit.Test
public void testText() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<String>() {

                @Override
                public void onMessage(String message) {
                    session.getAsyncRemote().sendText(message);
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 52 with Session

use of javax.websocket.Session in project undertow by undertow-io.

the class TestMessagesReceivedInOrder method testMessagesReceivedInOrder.

@Test
public void testMessagesReceivedInOrder() throws Exception {
    stacks.clear();
    EchoSocket.receivedEchos = new FutureResult<>();
    final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<String> error = new AtomicReference<>();
    ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {

        @Override
        public void onOpen(final Session session, EndpointConfig endpointConfig) {
            try {
                RemoteEndpoint.Basic rem = session.getBasicRemote();
                List<String> messages = new ArrayList<>();
                for (int i = 0; i < MESSAGES; i++) {
                    byte[] data = new byte[2048];
                    (new Random()).nextBytes(data);
                    String crc = md5(data);
                    rem.sendBinary(ByteBuffer.wrap(data));
                    messages.add(crc);
                }
                List<String> received = EchoSocket.receivedEchos.getIoFuture().get();
                StringBuilder sb = new StringBuilder();
                boolean fail = false;
                for (int i = 0; i < messages.size(); i++) {
                    if (received.size() <= i) {
                        fail = true;
                        sb.append(i + ": should be " + messages.get(i) + " but is empty.");
                    } else {
                        if (!messages.get(i).equals(received.get(i))) {
                            fail = true;
                            sb.append(i + ": should be " + messages.get(i) + " but is " + received.get(i) + " (but found at " + received.indexOf(messages.get(i)) + ").");
                        }
                    }
                }
                if (fail) {
                    error.set(sb.toString());
                }
                done.countDown();
            } catch (Throwable t) {
                System.out.println(t);
            }
        }
    }, clientEndpointConfig, new URI(DefaultServer.getDefaultServerURL() + "/webSocket"));
    done.await(30, TimeUnit.SECONDS);
    if (error.get() != null) {
        Assert.fail(error.get());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) RemoteEndpoint(javax.websocket.RemoteEndpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 53 with Session

use of javax.websocket.Session in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testPingPong.

@org.junit.Test
public void testPingPong() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new PingWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(PongWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 54 with Session

use of javax.websocket.Session in project undertow by undertow-io.

the class AnnotatedEndpointTest method testThreadSafeSend.

@Test
public void testThreadSafeSend() throws Exception {
    AnnotatedClientEndpoint.reset();
    Session session = deployment.connectToServer(AnnotatedClientEndpoint.class, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/threads"));
    Set<String> expected = ThreadSafetyEndpoint.expected();
    long end = System.currentTimeMillis() + 10000;
    while (!expected.isEmpty() && System.currentTimeMillis() < end) {
        expected.remove(AnnotatedClientEndpoint.message());
    }
    session.close();
    Assert.assertEquals(0, expected.size());
}
Also used : URI(java.net.URI) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 55 with Session

use of javax.websocket.Session in project undertow by undertow-io.

the class AnnotatedEndpointTest method testRedirectHandling.

@Test
public void testRedirectHandling() throws Exception {
    AnnotatedClientEndpoint.reset();
    Session session = deployment.connectToServer(AnnotatedClientEndpoint.class, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/redirect"));
    Assert.assertEquals("hi Stuart (protocol=foo)", AnnotatedClientEndpoint.message());
    session.close();
    Assert.assertEquals("CLOSED", AnnotatedClientEndpoint.message());
}
Also used : URI(java.net.URI) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Aggregations

Session (javax.websocket.Session)115 Test (org.junit.Test)92 URI (java.net.URI)73 WebSocketContainer (javax.websocket.WebSocketContainer)42 Endpoint (javax.websocket.Endpoint)39 CountDownLatch (java.util.concurrent.CountDownLatch)34 Context (org.apache.catalina.Context)31 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)31 Tomcat (org.apache.catalina.startup.Tomcat)31 EndpointConfig (javax.websocket.EndpointConfig)29 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)27 UndertowSession (io.undertow.websockets.jsr.UndertowSession)25 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)23 ServerEndpoint (javax.websocket.server.ServerEndpoint)20 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)16 IOException (java.io.IOException)16 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 FrameChecker (io.undertow.websockets.utils.FrameChecker)14