use of io.undertow.websockets.spi.WebSocketHttpExchange in project undertow by undertow-io.
the class WebSocket07ServerTest method testPing.
@Test
public void testPing() throws Exception {
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 onFullPingMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
final Pooled<ByteBuffer[]> data = message.getData();
WebSockets.sendPong(data.getResource(), channel, new WebSocketCallback<Void>() {
@Override
public void complete(WebSocketChannel channel, Void context) {
data.close();
}
@Override
public void onError(WebSocketChannel channel, Void context, Throwable throwable) {
data.close();
}
});
}
});
channel.resumeReceives();
}
}));
final FutureResult latch = new FutureResult();
final byte[] payload = "payload".getBytes();
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ':' + DefaultServer.getHostPort("default") + '/'));
client.connect();
client.send(new PingWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(PongWebSocketFrame.class, payload, latch));
latch.getIoFuture().get();
client.destroy();
}
use of io.undertow.websockets.spi.WebSocketHttpExchange 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();
}
use of io.undertow.websockets.spi.WebSocketHttpExchange in project undertow by undertow-io.
the class ChatServer method main.
public static void main(final String[] args) {
System.out.println("To see chat in action is to open two different browsers and point them at http://localhost:8080");
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) {
final String messageData = message.getData();
for (WebSocketChannel session : channel.getPeerConnections()) {
WebSockets.sendText(messageData, session, null);
}
}
});
channel.resumeReceives();
}
})).addPrefixPath("/", resource(new ClassPathResourceManager(ChatServer.class.getClassLoader(), ChatServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
server.start();
}
use of io.undertow.websockets.spi.WebSocketHttpExchange in project actframework by actframework.
the class UndertowWebSocketConnectionHandler method handle.
@Override
public void handle(final ActionContext context) {
if (logger.isTraceEnabled()) {
logger.trace("handle websocket connection request to %s", context.req().url());
}
final UndertowRequest req = (UndertowRequest) context.req();
HttpServerExchange exchange = req.exchange();
try {
Handlers.websocket(new WebSocketConnectionCallback() {
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
final WebSocketConnection connection = new UndertowWebSocketConnection(channel, context.session());
channel.setAttribute("act_conn", connection);
connectionManager.registerNewConnection(connection, context);
final WebSocketContext wsCtx = new WebSocketContext(req.url(), connection, connectionManager, context, connectionManager.app());
if (logger.isTraceEnabled()) {
logger.trace("websocket context[%s] created for %s", connection.sessionId(), context.req().url());
}
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
WebSocketContext.current(wsCtx);
String payload = message.getData();
if (logger.isTraceEnabled()) {
logger.trace("websocket message received: %s", payload);
}
wsCtx.messageReceived(payload);
invoke(wsCtx);
}
@Override
protected void onClose(WebSocketChannel webSocketChannel, StreamSourceFrameChannel channel) throws IOException {
if (logger.isTraceEnabled()) {
logger.trace("websocket closed: ", connection.sessionId());
}
WebSocketContext.current(wsCtx);
super.onClose(webSocketChannel, channel);
connection.destroy();
context.app().eventBus().emit(new WebSocketCloseEvent(wsCtx));
}
});
channel.resumeReceives();
Act.eventBus().emit(new WebSocketConnectEvent(wsCtx));
}
}).handleRequest(exchange);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw ActErrorResult.of(e);
}
}
Aggregations