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();
}
}
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();
}
}
Aggregations