@Override public Future<Void> sendPongFrame() { return channel.writeAndFlush(new PongWebSocketFrame()); }
@Override public Future<Void> sendPongFrame(ByteBuf payload) { return channel.writeAndFlush(new PongWebSocketFrame(wrappedBuffer(payload))); }
@Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception { if (frame instanceof PingWebSocketFrame) { frame.content().retain(); ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); return; } if (frame instanceof PongWebSocketFrame && dropPongFrames) { return; } out.add(frame.retain()); }
private void handleWebSocketFrame(final ChannelHandlerContext ctx, WebSocketFrame frame) { if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); } else if (frame instanceof TextWebSocketFrame) { webSocketClientRegistry.receivedTextWebSocketFrame(((TextWebSocketFrame) frame)); } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.content().retain())); } else { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } }
@Override public PongWebSocketFrame replace(ByteBuf content) { return new PongWebSocketFrame(isFinalFragment(), rsv(), content); }
ctx.writeAndFlush(new PongWebSocketFrame());
private void handleWebSocketFrame(final ChannelHandlerContext ctx, WebSocketFrame frame) throws JsonProcessingException { if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { clientRegistry.remove(ctx); } }); } else if (frame instanceof TextWebSocketFrame) { try { HttpRequest httpRequest = httpRequestSerializer.deserialize(((TextWebSocketFrame) frame).text()); clientRegistry.put(ctx, httpRequest); sendUpdate(httpRequest, ctx); } catch (IllegalArgumentException iae) { sendMessage(ctx, ImmutableMap.<String, Object>of("error", iae.getMessage())); } } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.content().retain())); } else { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } }
/** * Only supported TextWebSocketFrame * * @param ctx * @param frame */ private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { if (frame instanceof CloseWebSocketFrame) { this.handler.onDisConnect(this.context); this.handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException("unsupported frame type: " + frame.getClass().getName()); } this.context.setReqText(((TextWebSocketFrame) frame).text()); this.handler.onText(this.context); }
/** * Only supported TextWebSocketFrame * * @param ctx * @param frame */ private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { if (frame instanceof CloseWebSocketFrame) { this.handler.onDisConnect(this.context); this.handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException("unsupported frame type: " + frame.getClass().getName()); } this.context.setReqText(((TextWebSocketFrame) frame).text()); this.handler.onText(this.context); }
case PING: chctx.writeAndFlush(new PongWebSocketFrame(frame.getBinaryData())); return; case CLOSE: chctx.writeAndFlush(new PongWebSocketFrame(frame.content())); return; } else if (msg instanceof CloseWebSocketFrame) {
@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);} }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) { Channel ch = ctx.channel(); if (msg instanceof FullHttpResponse) { FullHttpResponse httpResponse = (FullHttpResponse) msg; final SettableFuture<String> registrationFuture = ch.attr(REGISTRATION_FUTURE).get(); if (httpResponse.headers().contains(UPGRADE, WEBSOCKET, true) && !handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, httpResponse); final String clientRegistrationId = httpResponse.headers().get("X-CLIENT-REGISTRATION-ID"); registrationFuture.set(clientRegistrationId); mockServerLogger.trace("web socket client " + clientRegistrationId + " connected!"); } else if (httpResponse.status().equals(HttpResponseStatus.NOT_IMPLEMENTED)) { String message = readRequestBody(httpResponse); registrationFuture.setException(new WebSocketException(message)); mockServerLogger.warn(message); } else { registrationFuture.setException(new WebSocketException("Unsupported web socket message " + new FullHttpResponseToMockServerResponse().mapMockServerResponseToFullHttpResponse(httpResponse))); } } else if (msg instanceof WebSocketFrame) { WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { webSocketClient.receivedTextWebSocketFrame((TextWebSocketFrame) frame); } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.content().retain())); } else if (frame instanceof CloseWebSocketFrame) { mockServerLogger.trace("web socket client received request to close"); ch.close(); } } }
ctx.writeAndFlush(new PongWebSocketFrame());
case PING: chctx.writeAndFlush(new PongWebSocketFrame(frame.getBinaryData().copy())); break; case CLOSE:
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); } }
protected WebSocketFrame toFrame(WebSocketMessage message) { ByteBuf byteBuf = NettyDataBufferFactory.toByteBuf(message.getPayload()); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { return new TextWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { return new BinaryWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { return new PingWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { return new PongWebSocketFrame(byteBuf); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } }
out.add(new PongWebSocketFrame(frameFinalFlag, frameRsv, payloadBuffer)); payloadBuffer = null; return;
ctx.writeAndFlush(new PongWebSocketFrame(frame.content()));
@Override protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { final Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { // web socket client connected handshaker.finishHandshake(ch, (FullHttpResponse) msg); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) msg; throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } // a close frame doesn't mean much here. errors raised from closed channels will mark the host as dead final WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof PingWebSocketFrame) { ctx.writeAndFlush(new PongWebSocketFrame()); }else if (frame instanceof PongWebSocketFrame) { logger.debug("Received response from keep-alive request"); } else if (frame instanceof BinaryWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof CloseWebSocketFrame) ch.close(); }
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); } }