List of usage examples for io.netty.handler.codec.http.websocketx TextWebSocketFrame duplicate
@Override
public TextWebSocketFrame duplicate()
From source file:app.WebSocketServerHandler.java
License:Apache License
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { channels.remove(ctx.channel());/*from w ww . j av a2 s . c om*/ handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.content().retain())); return; } if (frame instanceof TextWebSocketFrame) { JSONObject msg = (JSONObject) JSONValue.parse(((TextWebSocketFrame) frame).text()); if (msg == null) { System.out.println("Unknown message type"); return; } switch (msg.get("type").toString()) { case "broadcast": final TextWebSocketFrame outbound = new TextWebSocketFrame(msg.toJSONString()); channels.forEach(gc -> gc.writeAndFlush(outbound.duplicate().retain())); msg.replace("type", "broadcastResult"); ctx.writeAndFlush(new TextWebSocketFrame(msg.toJSONString())); break; case "echo": ctx.writeAndFlush(new TextWebSocketFrame(msg.toJSONString())); break; default: System.out.println("Unknown message type"); } return; } }