use of io.undertow.websockets.core.BufferedTextMessage 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.BufferedTextMessage in project undertow by undertow-io.
the class WebSocketServer method main.
public static void main(final String[] args) {
// Demonstrates how to use Websocket Protocol Handshake to enable Per-message deflate
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/myapp", new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
WebSockets.sendText(message.getData(), channel, null);
}
});
channel.resumeReceives();
}
}).addExtension(new PerMessageDeflateHandshake(false, 6))).addPrefixPath("/", resource(new ClassPathResourceManager(WebSocketServer.class.getClassLoader(), WebSocketServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
server.start();
}
use of io.undertow.websockets.core.BufferedTextMessage in project undertow by undertow-io.
the class WebSocketClient13TestCase method testTextMessage.
@Test
public void testTextMessage() throws Exception {
final WebSocketChannel webSocketChannel = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL())).connect().get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
result.set(data);
latch.countDown();
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
webSocketChannel.resumeReceives();
StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello World").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello World", result.get());
webSocketChannel.sendClose();
}
use of io.undertow.websockets.core.BufferedTextMessage in project undertow by undertow-io.
the class AbstractWebSocketServerTest method testText.
@Test
public void testText() throws Exception {
if (getVersion() == WebSocketVersion.V00) {
// ignore 00 tests for now
return;
}
final AtomicBoolean connected = new AtomicBoolean(false);
DefaultServer.setRootHandler(new WebSocketProtocolHandshakeHandler(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 {
String string = message.getData();
if (string.equals("hello")) {
WebSockets.sendText("world", channel, null);
} else {
WebSockets.sendText(string, channel, null);
}
}
});
channel.resumeReceives();
}
}));
final FutureResult<?> latch = new FutureResult();
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", CharsetUtil.US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(CharsetUtil.US_ASCII), latch));
latch.getIoFuture().get();
client.destroy();
}
use of io.undertow.websockets.core.BufferedTextMessage in project undertow by undertow-io.
the class WebSocketClient13TestCase method testTextMessageSecure.
public void testTextMessageSecure(final String urlProtocol) throws Exception {
UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(urlProtocol + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default"))).setSsl(ssl);
IoFuture<WebSocketChannel> future = connectionBuilder.connect();
future.await(4, TimeUnit.SECONDS);
final WebSocketChannel webSocketChannel = future.get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
result.set(data);
latch.countDown();
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
webSocketChannel.resumeReceives();
StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello World").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello World", result.get());
webSocketChannel.sendClose();
}
Aggregations