Search in sources :

Example 1 with WebSocketServlet

use of io.undertow.servlet.websockets.WebSocketServlet in project undertow by undertow-io.

the class WebSocketServletTest method testText.

@Test
public void testText() throws Exception {
    final AtomicBoolean connected = new AtomicBoolean(false);
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentUtils.setupServlet(new ServletInfo("websocket", WebSocketServlet.class, new ImmediateInstanceFactory<Servlet>(new WebSocketServlet(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
            connected.set(true);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
                    final String string = message.getData();
                    if (string.equals("hello")) {
                        WebSockets.sendText("world", channel, null);
                    } else {
                        WebSockets.sendText(string, channel, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    }))).addMapping("/*"));
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(io.netty.handler.codec.http.websocketx.WebSocketVersion.V13, new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/servletContext/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) WebSocketServlet(io.undertow.servlet.websockets.WebSocketServlet) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) ServletInfo(io.undertow.servlet.api.ServletInfo) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) ServletContainer(io.undertow.servlet.api.ServletContainer) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) WebSocketServlet(io.undertow.servlet.websockets.WebSocketServlet) Servlet(javax.servlet.Servlet) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Test(org.junit.Test)

Aggregations

TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 ServletContainer (io.undertow.servlet.api.ServletContainer)1 ServletInfo (io.undertow.servlet.api.ServletInfo)1 WebSocketServlet (io.undertow.servlet.websockets.WebSocketServlet)1 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)1 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)1 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)1 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)1 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)1 FrameChecker (io.undertow.websockets.utils.FrameChecker)1 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)1 URI (java.net.URI)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Servlet (javax.servlet.Servlet)1 Test (org.junit.Test)1 FutureResult (org.xnio.FutureResult)1