Created
March 19, 2018 18:57
-
-
Save KowalczykBartek/8079765b49c15c04c349b53474b97ea5 to your computer and use it in GitHub Desktop.
is it true ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSocketFrame> { | |
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketMessageHandler.class); | |
private static final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); | |
@Override | |
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { | |
if (frame instanceof TextWebSocketFrame) { | |
final String text = ((TextWebSocketFrame) frame).text(); | |
LOGGER.info("Received text frame {}", text); | |
allChannels.stream() | |
.filter(c -> c != ctx.channel()) | |
.forEach(c -> { | |
frame.retain(); | |
c.writeAndFlush(frame.duplicate()); | |
}); | |
} else { | |
throw new UnsupportedOperationException("Invalid websocket frame received"); | |
} | |
} | |
@Override | |
public void channelActive(ChannelHandlerContext ctx) throws Exception { | |
LOGGER.info("Adding new channel {} to list of channels", ctx.channel().remoteAddress()); | |
allChannels.add(ctx.channel()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment