use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class WebSocketServer method main.
public static void main(final String[] args) {
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/myapp", websocket(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();
}
})).addPrefixPath("/", resource(new ClassPathResourceManager(WebSocketServer.class.getClassLoader(), WebSocketServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
server.start();
}
use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class WebSocketProtocolHandshakeHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (!exchange.getRequestMethod().equals(Methods.GET)) {
// Only GET is supported to start the handshake
next.handleRequest(exchange);
return;
}
final AsyncWebSocketHttpServerExchange facade = new AsyncWebSocketHttpServerExchange(exchange, peerConnections);
Handshake handshaker = null;
for (Handshake method : handshakes) {
if (method.matches(facade)) {
handshaker = method;
break;
}
}
if (handshaker == null) {
next.handleRequest(exchange);
} else {
WebSocketLogger.REQUEST_LOGGER.debugf("Attempting websocket handshake with %s on %s", handshaker, exchange);
final Handshake selected = handshaker;
if (upgradeListener == null) {
exchange.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
peerConnections.add(channel);
callback.onConnect(facade, channel);
}
});
} else {
exchange.upgradeChannel(upgradeListener);
}
handshaker.handshake(facade);
}
}
use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class WebSocket13ClientHandshake method createChannel.
@Override
public WebSocketChannel createChannel(final StreamConnection channel, final String wsUri, final ByteBufferPool bufferPool, OptionMap options) {
if (negotiation != null && negotiation.getSelectedExtensions() != null && !negotiation.getSelectedExtensions().isEmpty()) {
List<WebSocketExtension> selected = negotiation.getSelectedExtensions();
List<ExtensionFunction> negotiated = new ArrayList<>();
if (selected != null && !selected.isEmpty()) {
for (WebSocketExtension ext : selected) {
for (ExtensionHandshake extHandshake : extensions) {
if (ext.getName().equals(extHandshake.getName())) {
negotiated.add(extHandshake.create());
}
}
}
}
return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation.getSelectedSubProtocol(), true, !negotiated.isEmpty(), CompositeExtensionFunction.compose(negotiated), new HashSet<WebSocketChannel>(), options);
} else {
return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation != null ? negotiation.getSelectedSubProtocol() : "", true, false, NoopExtensionFunction.INSTANCE, new HashSet<WebSocketChannel>(), options);
}
}
use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class WebSocketClient13TestCase method testMessageViaProxy.
@Test
@ProxyIgnore
public void testMessageViaProxy() throws Exception {
final WebSocketChannel webSocketChannel = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL())).setProxyUri(new URI("http", null, DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default") + 10, "/proxy", null, null)).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();
Assert.assertEquals("CONNECT " + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default"), connectLog.poll());
}
use of io.undertow.websockets.core.WebSocketChannel 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();
}
Aggregations