| | |
| | | import io.netty.util.AttributeKey; |
| | | import io.netty.util.CharsetUtil; |
| | | import org.springblade.modules.nettyServer.NettyConfig; |
| | | import org.springblade.modules.suser.service.ISuserService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | |
| | | |
| | | private WebSocketServerHandshaker handshaker; |
| | | |
| | | private String on=null; |
| | | |
| | | @Autowired |
| | | private ISuserService suserService; |
| | | private String on = null; |
| | | |
| | | private static WebSocketHandler webSocketHandler; |
| | | |
| | |
| | | //用户离线状态 |
| | | String name = ChannelSupervise.findName(ctx.channel().id().asShortText()); |
| | | System.out.println(name); |
| | | if ( name != null &&!name.equals("ping") ){ |
| | | if (name != null && !name.equals("ping")) { |
| | | NettyConfig.getChannelGroup().remove(ctx.channel()); |
| | | removeUserId(ctx); |
| | | } |
| | |
| | | // 返回应答消息 |
| | | String request = ((TextWebSocketFrame) frame).text(); |
| | | |
| | | if (!request.equals("ping")){ |
| | | if (!request.equals("ping")) { |
| | | |
| | | JSONObject jsonObj = JSON.parseObject(request); |
| | | String type = jsonObj.get("type").toString(); |
| | | |
| | | if (type != null && type.equals("login")){ |
| | | if (type != null && type.equals("login")) { |
| | | //登录链接 |
| | | String id = jsonObj.get("id").toString(); |
| | | NettyConfig.getUserChannelMap().put(id,ctx.channel()); |
| | | NettyConfig.getUserChannelMap().put(id, ctx.channel()); |
| | | |
| | | System.out.println(jsonObj.get("type")); |
| | | System.out.println(jsonObj.get("id")); |
| | |
| | | AttributeKey<String> key = AttributeKey.valueOf("userId"); |
| | | ctx.channel().attr(key).setIfAbsent(id); |
| | | //把用户信息添加到通道里 |
| | | ChannelSupervise.addChannel(ctx.channel(),id); |
| | | ChannelSupervise.addChannel(ctx.channel(), id); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | // TextWebSocketFrame tws = new TextWebSocketFrame(new Date().toString() |
| | |
| | | } |
| | | //握手 |
| | | WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( |
| | | "ws://localhost:9034/websocket", null, false); |
| | | "ws://localhost:2086/websocket", null, false); |
| | | handshaker = wsFactory.newHandshaker(req); |
| | | if (handshaker == null) { |
| | | WebSocketServerHandshakerFactory |
| | |
| | | // f.addListener(ChannelFutureListener.CLOSE); |
| | | // } |
| | | } |
| | | |
| | | @Override |
| | | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| | | cause.printStackTrace(); |
| | |
| | | |
| | | /** |
| | | * 删除用户与channel 对应关系 |
| | | * |
| | | * @param ctx |
| | | */ |
| | | private void removeUserId(ChannelHandlerContext ctx){ |
| | | private void removeUserId(ChannelHandlerContext ctx) { |
| | | AttributeKey<String> key = AttributeKey.valueOf("userId"); |
| | | String userId = ctx.channel().attr(key).get(); |
| | | NettyConfig.getUserChannelMap().remove(userId); |