@OnOpen
public void onOpen(NettySession nettySession, HttpHeaders headers, @RequestParam String req,@RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap) {
System.out.println("new connection");
ChannelId channelId = nettySession.id();
//这里最好先校验下是否登录
sessionMap.put(nettySession.id(), nettySession);
//扫码连接开始同步商品信息
redisUtil.hset(String.valueOf(reqMap.get("ableCodeId")),"1000","用户信息");
for (Map.Entry<ChannelId, NettySession> channelIdNettySessionEntry : sessionMap.entrySet()) {
ChannelId channelIdTemp = channelIdNettySessionEntry.getKey();
NettySession session = channelIdNettySessionEntry.getValue();
if (!channelIdTemp.equals(channelId)) {
session.sendText("找到你了");
} else {
session.sendText("成功连接服务");
}
}
@OnClose
public void onClose(NettySession nettySession) {
int cnt = OnlineCount.decrementAndGet();
System.out.println("有连接关闭,当前连接数为:" + cnt);
//这里要把Session从缓存中移除
System.out.println("one connection closed");
}
@OnError
public void onError(NettySession nettySession, Throwable throwable) {
throwable.printStackTrace();
}
@OnMessage
public void onMessage(NettySession nettySession, String message) {
System.out.println(message);
nettySession.sendText("Hello Netty!");
}
@OnBinary
public void onBinary(NettySession nettySession, byte[] bytes) {
for (byte b : bytes) {
System.out.println(b);
}
nettySession.sendBinary(bytes);
}
@OnEvent
public void onEvent(NettySession nettySession, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
switch (idleStateEvent.state()) {
case READER_IDLE:
System.out.println("read idle");
break;
case WRITER_IDLE:
System.out.println("write idle");
break;
case ALL_IDLE:
System.out.println("all idle");
break;
default:
break;
}
}
}