use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project undertow by undertow-io.
the class AbstractWebSocketServerTest method testCloseFrame.
@Test
public void testCloseFrame() 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 onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
message.getData().close();
channel.sendClose();
}
});
channel.resumeReceives();
}
}));
final AtomicBoolean receivedResponse = new AtomicBoolean(false);
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 CloseWebSocketFrame(), new FrameChecker(CloseWebSocketFrame.class, new byte[0], latch));
latch.getIoFuture().get();
Assert.assertFalse(receivedResponse.get());
client.destroy();
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testCloseFrame.
@org.junit.Test
public void testCloseFrame() throws Exception {
final int code = 1000;
final String reasonText = "TEST";
final AtomicReference<CloseReason> reason = new AtomicReference<>();
ByteBuffer payload = ByteBuffer.allocate(reasonText.length() + 2);
payload.putShort((short) code);
payload.put(reasonText.getBytes(StandardCharsets.UTF_8));
payload.flip();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
final CountDownLatch clientLatch = new CountDownLatch(1);
final AtomicInteger closeCount = new AtomicInteger();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
}
@Override
public void onClose(Session session, CloseReason closeReason) {
closeCount.incrementAndGet();
reason.set(closeReason);
clientLatch.countDown();
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new CloseWebSocketFrame(code, reasonText), new FrameChecker(CloseWebSocketFrame.class, payload.array(), latch));
// FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
latch.getIoFuture().await();
clientLatch.await();
assertEquals(code, reason.get().getCloseCode().getCode());
assertEquals(reasonText, reason.get().getReasonPhrase());
assertEquals(1, closeCount.get());
client.destroy();
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testCloseFrameWithoutReasonBody.
/**
* Section 5.5.1 of RFC 6455 says the reason body is optional
*/
@org.junit.Test
public void testCloseFrameWithoutReasonBody() throws Exception {
final int code = 1000;
final AtomicReference<CloseReason> reason = new AtomicReference<>();
ByteBuffer payload = ByteBuffer.allocate(2);
payload.putShort((short) code);
payload.flip();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
final CountDownLatch clientLatch = new CountDownLatch(1);
final AtomicInteger closeCount = new AtomicInteger();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
}
@Override
public void onClose(Session session, CloseReason closeReason) {
closeCount.incrementAndGet();
reason.set(closeReason);
clientLatch.countDown();
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new CloseWebSocketFrame(code, null), new FrameChecker(CloseWebSocketFrame.class, payload.array(), latch));
assertEquals(DONE, latch.getIoFuture().await(10, TimeUnit.SECONDS));
clientLatch.await();
assertEquals(code, reason.get().getCloseCode().getCode());
assertEquals("", reason.get().getReasonPhrase());
assertEquals(1, closeCount.get());
client.destroy();
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project vert.x by eclipse.
the class WebSocketImplBase method close.
@Override
public Future<Void> close(short statusCode, String reason) {
boolean sendCloseFrame;
synchronized (conn) {
if (sendCloseFrame = closeStatusCode == null) {
closeStatusCode = statusCode;
closeReason = reason;
}
}
if (sendCloseFrame) {
// Close the WebSocket by sending a close frame with specified payload
ByteBuf byteBuf = HttpUtils.generateWSCloseFrameByteBuf(statusCode, reason);
CloseWebSocketFrame frame = new CloseWebSocketFrame(true, 0, byteBuf);
PromiseInternal<Void> promise = context.promise();
conn.writeToChannel(frame, promise);
return promise;
} else {
return context.succeededFuture();
}
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project vert.x by eclipse.
the class Http1xConnectionBase method decodeFrame.
private WebSocketFrameInternal decodeFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame msg) {
ByteBuf payload = safeBuffer(msg.content());
boolean isFinal = msg.isFinalFragment();
WebSocketFrameType frameType;
if (msg instanceof BinaryWebSocketFrame) {
frameType = WebSocketFrameType.BINARY;
} else if (msg instanceof CloseWebSocketFrame) {
frameType = WebSocketFrameType.CLOSE;
} else if (msg instanceof PingWebSocketFrame) {
frameType = WebSocketFrameType.PING;
} else if (msg instanceof PongWebSocketFrame) {
frameType = WebSocketFrameType.PONG;
} else if (msg instanceof TextWebSocketFrame) {
frameType = WebSocketFrameType.TEXT;
} else if (msg instanceof ContinuationWebSocketFrame) {
frameType = WebSocketFrameType.CONTINUATION;
} else {
throw new IllegalStateException("Unsupported WebSocket msg " + msg);
}
return new WebSocketFrameImpl(frameType, payload, isFinal);
}
Aggregations