|
7 | 7 | import io.netty.channel.ChannelFutureListener; |
8 | 8 | import io.netty.channel.ChannelHandlerContext; |
9 | 9 | import io.netty.channel.SimpleChannelInboundHandler; |
10 | | -import io.netty.handler.codec.http.DefaultFullHttpResponse; |
11 | | -import io.netty.handler.codec.http.FullHttpRequest; |
12 | | -import io.netty.handler.codec.http.HttpResponseStatus; |
13 | | -import io.netty.handler.codec.http.HttpUtil; |
14 | | -import io.netty.handler.codec.http.HttpVersion; |
15 | | -import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; |
16 | | -import io.netty.handler.codec.http.websocketx.PingWebSocketFrame; |
17 | | -import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; |
18 | | -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; |
19 | | -import io.netty.handler.codec.http.websocketx.WebSocketFrame; |
20 | | -import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; |
21 | | -import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; |
| 10 | +import io.netty.handler.codec.http.*; |
| 11 | +import io.netty.handler.codec.http.websocketx.*; |
22 | 12 | import io.netty.util.CharsetUtil; |
23 | | -import java.util.Date; |
24 | 13 | import lombok.extern.slf4j.Slf4j; |
25 | 14 |
|
| 15 | +import java.util.Date; |
| 16 | + |
26 | 17 | /** |
27 | 18 | * @author https://github.com/kuangcp on 2021-05-18 08:33 |
28 | 19 | */ |
29 | 20 | @Slf4j |
30 | 21 | public class NioWebSocketHandler extends SimpleChannelInboundHandler<Object> { |
31 | 22 |
|
32 | | - private WebSocketServerHandshaker handshaker; |
33 | | - |
34 | | - @Override |
35 | | - protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { |
36 | | - log.debug("收到消息:" + msg); |
37 | | - if (msg instanceof FullHttpRequest) { |
38 | | - //以http请求形式接入,但是走的是websocket |
39 | | - handleHttpRequest(ctx, (FullHttpRequest) msg); |
40 | | - } else if (msg instanceof WebSocketFrame) { |
41 | | - //处理websocket客户端的消息 |
42 | | - handlerWebSocketFrame(ctx, (WebSocketFrame) msg); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - @Override |
47 | | - public void channelActive(ChannelHandlerContext ctx) throws Exception { |
48 | | - log.debug("客户端加入连接:" + ctx.channel()); |
49 | | - ChannelSupervise.addChannel(ctx.channel()); |
50 | | - } |
51 | | - |
52 | | - @Override |
53 | | - public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
54 | | - log.debug("客户端断开连接:" + ctx.channel()); |
55 | | - ChannelSupervise.removeChannel(ctx.channel()); |
56 | | - } |
57 | | - |
58 | | - @Override |
59 | | - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
60 | | - ctx.flush(); |
61 | | - } |
62 | | - |
63 | | - private void handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { |
64 | | - // 判断是否关闭链路的指令 |
65 | | - if (frame instanceof CloseWebSocketFrame) { |
66 | | - handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); |
67 | | - return; |
| 23 | + private WebSocketServerHandshaker handshaker; |
| 24 | + |
| 25 | + @Override |
| 26 | + protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 27 | + log.debug("收到消息:" + msg); |
| 28 | + if (msg instanceof FullHttpRequest) { |
| 29 | + //以http请求形式接入,但是走的是websocket |
| 30 | + handleHttpRequest(ctx, (FullHttpRequest) msg); |
| 31 | + } else if (msg instanceof WebSocketFrame) { |
| 32 | + //处理websocket客户端的消息 |
| 33 | + handlerWebSocketFrame(ctx, (WebSocketFrame) msg); |
| 34 | + } |
68 | 35 | } |
69 | 36 |
|
70 | | - // 判断是否ping消息 |
71 | | - if (frame instanceof PingWebSocketFrame) { |
72 | | - ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); |
73 | | - return; |
| 37 | + @Override |
| 38 | + public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| 39 | + log.debug("客户端加入连接:" + ctx.channel()); |
| 40 | + ChannelSupervise.addChannel(ctx.channel()); |
74 | 41 | } |
75 | 42 |
|
76 | | - // 本例程仅支持文本消息,不支持二进制消息 |
77 | | - if (!(frame instanceof TextWebSocketFrame)) { |
78 | | - log.debug("本例程仅支持文本消息,不支持二进制消息"); |
79 | | - throw new UnsupportedOperationException(String.format( |
80 | | - "%s frame types not supported", frame.getClass().getName())); |
| 43 | + @Override |
| 44 | + public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
| 45 | + log.debug("客户端断开连接:" + ctx.channel()); |
| 46 | + ChannelSupervise.removeChannel(ctx.channel()); |
81 | 47 | } |
82 | 48 |
|
83 | | - // 返回应答消息 |
84 | | - String request = ((TextWebSocketFrame) frame).text(); |
85 | | - log.debug("服务端收到:" + request); |
86 | | - TextWebSocketFrame tws = new TextWebSocketFrame( |
87 | | - new Date().toString() + ctx.channel().id() + ":" + request); |
| 49 | + @Override |
| 50 | + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
| 51 | + ctx.flush(); |
| 52 | + } |
88 | 53 |
|
89 | | - // 群发至所有连接 |
| 54 | + private void handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { |
| 55 | + // 判断是否关闭链路的指令 |
| 56 | + if (frame instanceof CloseWebSocketFrame) { |
| 57 | + handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // 判断是否ping消息 |
| 62 | + if (frame instanceof PingWebSocketFrame) { |
| 63 | + ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // 本例程仅支持文本消息,不支持二进制消息 |
| 68 | + if (!(frame instanceof TextWebSocketFrame)) { |
| 69 | + log.debug("本例程仅支持文本消息,不支持二进制消息"); |
| 70 | + throw new UnsupportedOperationException(String.format( |
| 71 | + "%s frame types not supported", frame.getClass().getName())); |
| 72 | + } |
| 73 | + |
| 74 | + // 返回应答消息 |
| 75 | + String request = ((TextWebSocketFrame) frame).text(); |
| 76 | + log.info("服务端收到:final:{} txt:{}", frame.isFinalFragment(), request.length()); |
| 77 | + TextWebSocketFrame tws = new TextWebSocketFrame( |
| 78 | + new Date().toString() + ctx.channel().id() + ":" + request); |
| 79 | + |
| 80 | + // 群发至所有连接 |
90 | 81 | // ChannelSupervise.send2All(tws); |
91 | 82 |
|
92 | | - // 返回【谁发的发给谁】 |
93 | | - ctx.channel().writeAndFlush(tws); |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * 唯一的一次http请求,用于创建websocket |
98 | | - */ |
99 | | - private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { |
100 | | - //要求Upgrade为websocket,过滤掉get/Post |
101 | | - if (!req.decoderResult().isSuccess() |
102 | | - || (!"websocket".equals(req.headers().get("Upgrade")))) { |
103 | | - //若不是websocket方式,则创建BAD_REQUEST的req,返回给客户端 |
104 | | - sendHttpResponse(ctx, req, new DefaultFullHttpResponse( |
105 | | - HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); |
106 | | - return; |
| 83 | + // 返回【谁发的发给谁】 |
| 84 | + ctx.channel().writeAndFlush(tws); |
107 | 85 | } |
108 | 86 |
|
109 | | - WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( |
110 | | - "ws://localhost:7094/ws", null, false); |
111 | | - handshaker = wsFactory.newHandshaker(req); |
112 | | - if (handshaker == null) { |
113 | | - WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); |
114 | | - } else { |
115 | | - handshaker.handshake(ctx.channel(), req); |
116 | | - } |
117 | | - } |
118 | | - |
119 | | - /** |
120 | | - * 拒绝不合法的请求,并返回错误信息 |
121 | | - */ |
122 | | - private static void sendHttpResponse(ChannelHandlerContext ctx, |
123 | | - FullHttpRequest req, DefaultFullHttpResponse res) { |
124 | | - // 返回应答给客户端 |
125 | | - if (res.status().code() != 200) { |
126 | | - ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); |
127 | | - res.content().writeBytes(buf); |
128 | | - buf.release(); |
| 87 | + /** |
| 88 | + * 唯一的一次http请求,用于创建websocket |
| 89 | + */ |
| 90 | + private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { |
| 91 | + //要求Upgrade为websocket,过滤掉get/Post |
| 92 | + if (!req.decoderResult().isSuccess() |
| 93 | + || (!"websocket".equals(req.headers().get("Upgrade")))) { |
| 94 | + //若不是websocket方式,则创建BAD_REQUEST的req,返回给客户端 |
| 95 | + sendHttpResponse(ctx, req, new DefaultFullHttpResponse( |
| 96 | + HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( |
| 101 | + "ws://localhost:7094/ws", null, false); |
| 102 | + handshaker = wsFactory.newHandshaker(req); |
| 103 | + if (handshaker == null) { |
| 104 | + WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); |
| 105 | + } else { |
| 106 | + handshaker.handshake(ctx.channel(), req); |
| 107 | + } |
129 | 108 | } |
130 | | - ChannelFuture f = ctx.channel().writeAndFlush(res); |
131 | | - // 如果是非Keep-Alive,关闭连接 |
132 | | - if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { |
133 | | - f.addListener(ChannelFutureListener.CLOSE); |
| 109 | + |
| 110 | + /** |
| 111 | + * 拒绝不合法的请求,并返回错误信息 |
| 112 | + */ |
| 113 | + private static void sendHttpResponse(ChannelHandlerContext ctx, |
| 114 | + FullHttpRequest req, DefaultFullHttpResponse res) { |
| 115 | + // 返回应答给客户端 |
| 116 | + if (res.status().code() != 200) { |
| 117 | + ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); |
| 118 | + res.content().writeBytes(buf); |
| 119 | + buf.release(); |
| 120 | + } |
| 121 | + ChannelFuture f = ctx.channel().writeAndFlush(res); |
| 122 | + // 如果是非Keep-Alive,关闭连接 |
| 123 | + if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { |
| 124 | + f.addListener(ChannelFutureListener.CLOSE); |
| 125 | + } |
134 | 126 | } |
135 | | - } |
136 | 127 | } |
0 commit comments