@Override public Object serverClosingConnectionMessage(int statusCode, String reasonText) { return new CloseWebSocketFrame(statusCode, reasonText); }
@Override public Object serverClosingConnectionMessage(int statusCode, String reasonText) { return new CloseWebSocketFrame(statusCode, reasonText); }
private void protocolViolation(ChannelHandlerContext ctx, CorruptedFrameException ex) { state = State.CORRUPT; if (ctx.channel().isActive()) { Object closeMessage; if (receivedClosingHandshake) { closeMessage = Unpooled.EMPTY_BUFFER; } else { closeMessage = new CloseWebSocketFrame(1002, null); } ctx.writeAndFlush(closeMessage).addListener(ChannelFutureListener.CLOSE); } throw ex; }
@Override public Future<Void> sendCloseFrame(int statusCode, String reasonText) { if (channel.isOpen()) { return channel.writeAndFlush(new CloseWebSocketFrame(1000, "normal closure")); } return ImmediateEventExecutor.INSTANCE.newSucceededFuture(null); }
private void writeCloseFrameAndTerminate(ChannelHandlerContext ctx, int code, String reason) { final CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(code, reason); ctx.channel().writeAndFlush(closeFrame) .addListener(future -> handleCloseFrame(ctx, new CloseWebSocketFrame(code, reason))); } }
@Override public void closeWithPayload(ByteBuf byteBuf) { if (ws == null) { super.close(); } else { endReadAndFlush(); chctx .writeAndFlush(new CloseWebSocketFrame(true, 0, byteBuf)) .addListener(ChannelFutureListener.CLOSE); } }
@Override public void close() { if (ws == null) { super.close(); } else { endReadAndFlush(); chctx .writeAndFlush(new CloseWebSocketFrame(true, 0, 1000, null)) .addListener(ChannelFutureListener.CLOSE); } }
@Override public void closeWithPayload(ByteBuf byteBuf) { if (handshaker == null) { super.close(); } else { // make sure everything is flushed out on close endReadAndFlush(); // close the websocket connection by sending a close frame with specified payload. CloseWebSocketFrame closeFrame; if (byteBuf != null) { closeFrame = new CloseWebSocketFrame(true, 0, byteBuf); } else { closeFrame = new CloseWebSocketFrame(true, 0, 1000, null); } handshaker.close(chctx.channel(), closeFrame); } }
@Override public void close(final int status, final String reason) { handshaker.close(ctx.channel(), new CloseWebSocketFrame(status, reason)) .addListener(CLOSE); Attribute<NettyWebSocket> ws = ctx.channel().attr(KEY); if (ws != null) { ws.set(null); } }
@Override public void close(CloseReason closeReason) { if (channel.isOpen()) { channel.writeAndFlush(new CloseWebSocketFrame(closeReason.getCode(), closeReason.getReason())) .addListener(future -> channel.close()); } }
@Override public CloseWebSocketFrame replace(ByteBuf content) { return new CloseWebSocketFrame(isFinalFragment(), rsv(), content); }
CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(frame.closeStatusCode(), frame.closeReason()); chctx.writeAndFlush(closeFrame).addListener(ChannelFutureListener.CLOSE); closeFrameSent = true; CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(frame.statusCode(), frame.reasonText()); chctx.writeAndFlush(closeFrame).addListener(ChannelFutureListener.CLOSE); closeFrameSent = true;
private WebSocketFrame decodeBinaryFrame(ChannelHandlerContext ctx, byte type, ByteBuf buffer) { long frameSize = 0; int lengthFieldSize = 0; byte b; do { b = buffer.readByte(); frameSize <<= 7; frameSize |= b & 0x7f; if (frameSize > maxFrameSize) { throw new TooLongFrameException(); } lengthFieldSize++; if (lengthFieldSize > 8) { // Perhaps a malicious peer? throw new TooLongFrameException(); } } while ((b & 0x80) == 0x80); if (type == (byte) 0xFF && frameSize == 0) { receivedClosingHandshake = true; return new CloseWebSocketFrame(); } ByteBuf payload = readBytes(ctx.alloc(), buffer, (int) frameSize); return new BinaryWebSocketFrame(payload); }
@Override protected void encode(ChannelHandlerContext ctx, WebSocketConvertHandler.MyWebSocketFrame msg, List<Object> out) throws Exception { ByteBuf payload = msg.getData().duplicate().retain(); switch (msg.getType()) { case BINARY: out.add(new BinaryWebSocketFrame(payload)); break; case TEXT: out.add(new TextWebSocketFrame(payload)); break; case CLOSE: out.add(new CloseWebSocketFrame(true, 0, payload)); break; case CONTINUATION: out.add(new ContinuationWebSocketFrame(payload)); break; case PONG: out.add(new PongWebSocketFrame(payload)); break; case PING: out.add(new PingWebSocketFrame(payload)); break; default: throw new IllegalStateException( "Unsupported websocket msg " + msg);} }
CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(frame.closeStatusCode(), frame.closeReason()); chctx.writeAndFlush(closeFrame).addListener(ChannelFutureListener.CLOSE); closeFrameSent = true;
WebSocketFrame encodeFrame(WebSocketFrameImpl frame) { ByteBuf buf = frame.getBinaryData(); if (buf != Unpooled.EMPTY_BUFFER) { buf = safeBuffer(buf, chctx.alloc()); } switch (frame.type()) { case BINARY: return new BinaryWebSocketFrame(frame.isFinal(), 0, buf); case TEXT: return new TextWebSocketFrame(frame.isFinal(), 0, buf); case CLOSE: return new CloseWebSocketFrame(true, 0, buf); case CONTINUATION: return new ContinuationWebSocketFrame(frame.isFinal(), 0, buf); case PONG: return new PongWebSocketFrame(buf); case PING: return new PingWebSocketFrame(buf); default: throw new IllegalStateException("Unsupported websocket msg " + frame); } }
LOG.error("Error opening WebSocket: " + e.getMessage(), e); ctx.writeAndFlush(new CloseWebSocketFrame(CloseReason.INTERNAL_ERROR.getCode(), CloseReason.INTERNAL_ERROR.getReason()));
receivedClosingHandshake = true; checkCloseFrameBody(ctx, payloadBuffer); out.add(new CloseWebSocketFrame(frameFinalFlag, frameRsv, payloadBuffer)); payloadBuffer = null; return;
int status = 4000 + TestUtils.randomPositiveInt() % 100; String reason = TestUtils.randomAlphaString(10); so.writeMessage(new CloseWebSocketFrame(status, reason)); so.closeHandler(v -> { assertEquals(1, received.size());
int status = 4000 + TestUtils.randomPositiveInt() % 100; String reason = TestUtils.randomAlphaString(10); so.writeMessage(new CloseWebSocketFrame(status, reason)); Deque<Object> received = new ArrayDeque<>(); so.messageHandler(received::add);