Search in sources :

Example 1 with UndertowSession

use of io.undertow.websockets.jsr.UndertowSession in project undertow by undertow-io.

the class AnnotatedEndpoint method onOpen.

@Override
public void onOpen(final Session session, final EndpointConfig endpointConfiguration) {
    this.released = false;
    final UndertowSession s = (UndertowSession) session;
    boolean partialText = textMessage == null || (textMessage.hasParameterType(boolean.class) && !textMessage.getMessageType().equals(boolean.class));
    boolean partialBinary = binaryMessage == null || (binaryMessage.hasParameterType(boolean.class) && !binaryMessage.getMessageType().equals(boolean.class));
    if (textMessage != null) {
        if (partialText) {
            addPartialHandler(s, textMessage);
        } else {
            if (textMessage.getMaxMessageSize() > 0) {
                s.setMaxTextMessageBufferSize((int) textMessage.getMaxMessageSize());
            }
            addWholeHandler(s, textMessage);
        }
    }
    if (binaryMessage != null) {
        if (partialBinary) {
            addPartialHandler(s, binaryMessage);
        } else {
            if (binaryMessage.getMaxMessageSize() > 0) {
                s.setMaxBinaryMessageBufferSize((int) binaryMessage.getMaxMessageSize());
            }
            addWholeHandler(s, binaryMessage);
        }
    }
    if (pongMessage != null) {
        addWholeHandler(s, pongMessage);
    }
    if (webSocketOpen != null) {
        final Map<Class<?>, Object> params = new HashMap<>();
        params.put(Session.class, session);
        params.put(EndpointConfig.class, endpointConfiguration);
        params.put(Map.class, session.getPathParameters());
        invokeMethod(params, webSocketOpen, s);
    }
}
Also used : HashMap(java.util.HashMap) UndertowSession(io.undertow.websockets.jsr.UndertowSession)

Example 2 with UndertowSession

use of io.undertow.websockets.jsr.UndertowSession in project undertow by undertow-io.

the class AnnotatedEndpointTest method testGenericMessageHandling.

@Test
public void testGenericMessageHandling() throws Exception {
    //make a sub class
    AnnotatedGenericClientEndpoint c = new AnnotatedGenericClientEndpoint() {
    };
    Session session = deployment.connectToServer(c, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/error"));
    Assert.assertEquals("hi", ErrorEndpoint.getMessage());
    session.getAsyncRemote().sendText("app-error");
    Assert.assertEquals("app-error", ErrorEndpoint.getMessage());
    Assert.assertEquals("ERROR: java.lang.RuntimeException", ErrorEndpoint.getMessage());
    Assert.assertTrue(c.isOpen());
    session.getBasicRemote().sendText("io-error");
    Assert.assertEquals("io-error", ErrorEndpoint.getMessage());
    Assert.assertEquals("ERROR: java.io.IOException", ErrorEndpoint.getMessage());
    Assert.assertTrue(c.isOpen());
    ((UndertowSession) session).forceClose();
    Assert.assertEquals("CLOSED", ErrorEndpoint.getMessage());
}
Also used : UndertowSession(io.undertow.websockets.jsr.UndertowSession) URI(java.net.URI) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 3 with UndertowSession

use of io.undertow.websockets.jsr.UndertowSession in project undertow by undertow-io.

the class AnnotatedEndpointTest method testErrorHandling.

@Test
public void testErrorHandling() throws Exception {
    //make a sub class
    AnnotatedClientEndpoint c = new AnnotatedClientEndpoint() {
    };
    Session session = deployment.connectToServer(c, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/error"));
    Assert.assertEquals("hi", ErrorEndpoint.getMessage());
    session.getAsyncRemote().sendText("app-error");
    Assert.assertEquals("app-error", ErrorEndpoint.getMessage());
    Assert.assertEquals("ERROR: java.lang.RuntimeException", ErrorEndpoint.getMessage());
    Assert.assertTrue(c.isOpen());
    session.getBasicRemote().sendText("io-error");
    Assert.assertEquals("io-error", ErrorEndpoint.getMessage());
    Assert.assertEquals("ERROR: java.io.IOException", ErrorEndpoint.getMessage());
    Assert.assertTrue(c.isOpen());
    ((UndertowSession) session).forceClose();
    Assert.assertEquals("CLOSED", ErrorEndpoint.getMessage());
}
Also used : UndertowSession(io.undertow.websockets.jsr.UndertowSession) URI(java.net.URI) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 4 with UndertowSession

use of io.undertow.websockets.jsr.UndertowSession in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testErrorHandling.

@Test
public void testErrorHandling() throws Exception {
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(ProgramaticErrorEndpoint.class, "/").configurator(new InstanceConfigurator(new ProgramaticErrorEndpoint())).build());
    deployServlet(builder);
    AnnotatedClientEndpoint c = new AnnotatedClientEndpoint();
    Session session = ContainerProvider.getWebSocketContainer().connectToServer(c, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    Assert.assertEquals("hi", ProgramaticErrorEndpoint.getMessage());
    session.getAsyncRemote().sendText("app-error");
    Assert.assertEquals("app-error", ProgramaticErrorEndpoint.getMessage());
    Assert.assertEquals("ERROR: java.lang.RuntimeException", ProgramaticErrorEndpoint.getMessage());
    Assert.assertTrue(c.isOpen());
    session.getBasicRemote().sendText("io-error");
    Assert.assertEquals("io-error", ProgramaticErrorEndpoint.getMessage());
    Assert.assertEquals("ERROR: java.lang.RuntimeException", ProgramaticErrorEndpoint.getMessage());
    Assert.assertTrue(c.isOpen());
    ((UndertowSession) session).forceClose();
    Assert.assertEquals("CLOSED", ProgramaticErrorEndpoint.getMessage());
}
Also used : ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) UndertowSession(io.undertow.websockets.jsr.UndertowSession) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) URI(java.net.URI) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Aggregations

UndertowSession (io.undertow.websockets.jsr.UndertowSession)4 URI (java.net.URI)3 Session (javax.websocket.Session)3 Test (org.junit.Test)3 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)1 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)1 HashMap (java.util.HashMap)1