Search in sources :

Example 1 with StreamSourceFrameChannel

use of io.undertow.websockets.core.StreamSourceFrameChannel in project syncany by syncany.

the class InternalWebSocketHandler method onConnect.

@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
    logger.log(Level.INFO, "Connecting to websocket server.");
    // Validate origin header (security!)
    String originHeader = exchange.getRequestHeader("Origin");
    if (!allowedOriginHeader(originHeader)) {
        logger.log(Level.INFO, channel.toString() + " disconnected due to invalid origin header: " + originHeader);
        exchange.close();
    } else {
        logger.log(Level.INFO, "Valid origin header, setting up connection.");
        channel.getReceiveSetter().set(new AbstractReceiveListener() {

            @Override
            protected void onFullTextMessage(WebSocketChannel clientChannel, BufferedTextMessage message) {
                handleMessage(clientChannel, message.getData());
            }

            @Override
            protected void onError(WebSocketChannel webSocketChannel, Throwable error) {
                logger.log(Level.INFO, "Server error : " + error.toString());
            }

            @Override
            protected void onClose(WebSocketChannel clientChannel, StreamSourceFrameChannel streamSourceChannel) throws IOException {
                logger.log(Level.INFO, clientChannel.toString() + " disconnected");
                daemonWebServer.removeClientChannel(clientChannel);
            }
        });
        logger.log(Level.INFO, "A new client (" + channel.hashCode() + ") connected using format " + requestFormatType);
        daemonWebServer.addClientChannel(channel, requestFormatType);
        channel.resumeReceives();
    }
}
Also used : StreamSourceFrameChannel(io.undertow.websockets.core.StreamSourceFrameChannel) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) IOException(java.io.IOException) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage)

Example 2 with StreamSourceFrameChannel

use of io.undertow.websockets.core.StreamSourceFrameChannel in project undertow by undertow-io.

the class SuspendResumeTestCase method testConnectionClosedOnPause.

@Test
public void testConnectionClosedOnPause() throws Exception {
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<String> message = new AtomicReference<>();
    WebSocketChannel channel = WebSocketClient.connectionBuilder(DefaultServer.getWorker(), DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL() + "/")).connect().get();
    channel.getReceiveSetter().set(new ChannelListener<WebSocketChannel>() {

        @Override
        public void handleEvent(WebSocketChannel channel) {
            try {
                StreamSourceFrameChannel res = channel.receive();
                if (res == null) {
                    return;
                }
                if (res.getType() == WebSocketFrameType.CLOSE) {
                    message.set("closed");
                    done.countDown();
                }
                Channels.drain(res, Long.MAX_VALUE);
            } catch (IOException e) {
                if (message.get() == null) {
                    e.printStackTrace();
                    message.set("error");
                    done.countDown();
                }
            }
        }
    });
    channel.resumeReceives();
    Assert.assertTrue(channel.isOpen());
    Thread.sleep(500);
    serverContainer.pause(null);
    try {
        Assert.assertTrue(done.await(10, TimeUnit.SECONDS));
        Assert.assertEquals("closed", message.get());
    } finally {
        serverContainer.resume();
    }
}
Also used : StreamSourceFrameChannel(io.undertow.websockets.core.StreamSourceFrameChannel) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Test(org.junit.Test)

Aggregations

StreamSourceFrameChannel (io.undertow.websockets.core.StreamSourceFrameChannel)2 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)2 IOException (java.io.IOException)2 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)1 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)1 URI (java.net.URI)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1