实现websocket通讯,和广播消息
添加依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons.lang.version}</version>
</dependency>
排除tomcat的依赖
Netty Http服务端编写
handler 处理类
@Component
@Slf4j
@ChannelHandler.Sharable //@Sharable 注解用来说明ChannelHandler是否可以在多个channel直接共享使用
@ConditionalOnProperty( //配置文件属性是否为true
value = {"netty.ws.enabled"},
matchIfMissing = false
)
public class WsServerHandler extends ChannelInboundHandlerAdapter {
@Autowired
NettyWsProperties nettyWsProperties;
public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
private WebSocketServerHandshaker handshaker;
//websocket握手升级绑定页面
String wsFactoryUri = "";
@Value("${netty.ws.endPoint:/ws}")
private String wsUri;
//static Set<Channel> channelSet = new HashSet<>();
/*
* 握手建立
*/
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel incoming = ctx.channel();
channels.add(incoming);
}
/*
* 握手取消
*/
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exc