Skip to content

Instantly share code, notes, and snippets.

@KowalczykBartek
Created March 19, 2018 18:57
Show Gist options
  • Save KowalczykBartek/8079765b49c15c04c349b53474b97ea5 to your computer and use it in GitHub Desktop.
Save KowalczykBartek/8079765b49c15c04c349b53474b97ea5 to your computer and use it in GitHub Desktop.
is it true ?
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