From 0632a02beb78705dc93b760c7bfa86ecedacb970 Mon Sep 17 00:00:00 2001 From: shikeying <pxzsky@163.com> Date: 星期四, 22 二月 2024 11:10:34 +0800 Subject: [PATCH] 增加注释 --- walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java | 58 ++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 38 insertions(+), 20 deletions(-) diff --git a/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java b/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java index 68d0670..ffa3481 100644 --- a/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java +++ b/walker-tcp/src/main/java/com/walker/tcp/netty/WebSocketNettyHandler.java @@ -26,58 +26,76 @@ public class WebSocketNettyHandler extends DefaultLongHandler { private WebSocketServerHandshaker handshaker; - + private String uri; - + public String getUri() { return uri; } + /** + * 璁剧疆澶栭儴閰嶇疆鐨剋ebsocket璁块棶鍦板潃銆� + * <pre> + * 1) 璇ュ湴鍧�鏄祻瑙堝櫒鍙闂殑鏈�缁堝湴鍧�锛屽锛歸s://my.com/websocket 鎴� wss://my.com/wss + * 2) 寮曟搸鏈韩骞朵笉浼氱敓鎴愬湴鍧�锛屼唬鐮佷腑log鏃ュ織涓瓀ebsocket浠呮槸璋冭瘯骞堕潪鐪熸璺緞 + * 3) 寮曟搸浠呯洃鍚缃殑绔彛锛堥粯璁�60000锛夛紝閫氬父鍦� nginx 閰嶇疆涓紝浠g悊璇ョ鍙e嵆鍙紝濡傦細 + * + * ... + * location /wss { + * proxy_pass http://my.com:60000; + * proxy_http_version 1.1; + * proxy_set_header Upgrade $http_upgrade; + * proxy_set_header Connection "Upgrade"; + * } + * + * </pre> + * @param uri + */ public void setUri(String uri) { this.uri = uri; } @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { - + String textMessage = null; - + if(msg instanceof FullHttpRequest){ //-----------> http://xxxx handleHttpRequest(ctx,(FullHttpRequest)msg); textMessage = WebSocketUtils.WEB_SOCKET_HTTP_MSG; msgThreadLocal.set(textMessage); - + }else if(msg instanceof WebSocketFrame){ - //-----------> ws://xxxx + //-----------> ws://xxxx textMessage = handlerWebSocketFrame(ctx,(WebSocketFrame)msg); String id = ctx.channel().id().asLongText() ; LongConnection conn = (LongConnection)connectionManager.getConnection(id); - + msgThreadLocal.set(textMessage); tcpServerHandler.onRead(conn, textMessage); - + } else { throw new UnsupportedOperationException("涓嶆敮鎸佽娑堟伅绫诲瀷锛�" + msg.getClass().getName()); } } - + @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // System.out.println("鏈嶅姟绔鍙栧畬鎴愭暟鎹竴娆°��" + ctx.channel().remoteAddress() ); - + String id = ctx.channel().id().asLongText() ; // String msg = msgThreadLocal.get(); Object msg = msgThreadLocal.get(); if(msg != null && msg.toString().equals(WebSocketUtils.WEB_SOCKET_HTTP_MSG)){ return; } - + tcpServerHandler.onReadComplete(msg, id, ctx); msgThreadLocal.set(null); super.channelReadComplete(ctx); } - + private String handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // 鍒ゆ柇鏄惁鍏抽棴閾捐矾鐨勬寚浠� if (frame instanceof CloseWebSocketFrame) { @@ -85,7 +103,7 @@ handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return null; } - + // 鍒ゆ柇鏄惁ping娑堟伅 if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); @@ -95,14 +113,14 @@ //ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return null; } - - + + // 鏈緥绋嬩粎鏀寔鏂囨湰娑堟伅锛屼笉鏀寔浜岃繘鍒舵秷鎭� if (!(frame instanceof TextWebSocketFrame)) { //System.out.println("鏈緥绋嬩粎鏀寔鏂囨湰娑堟伅锛屼笉鏀寔浜岃繘鍒舵秷鎭�"); throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } - + // 杩斿洖搴旂瓟娑堟伅 return ((TextWebSocketFrame) frame).text(); //System.out.println("鏈嶅姟绔敹鍒帮細" + request); @@ -113,7 +131,7 @@ // 杩斿洖銆愯皝鍙戠殑鍙戠粰璋併�� // ctx.channel().writeAndFlush(tws); } - + /** * 绗竴娆¤姹傛槸http璇锋眰锛岃姹傚ご鍖呮嫭ws鐨勪俊鎭� * @param ctx @@ -127,12 +145,12 @@ sendHttpResponse(ctx,req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); return; } - + logger.debug("================ " + "ws://"+ctx.channel()+ "/websocket"); // WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws://"+ctx.channel()+ "/websocket",null,false); WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(uri,null,false); handshaker = wsFactory.newHandshaker(req); - + if(handshaker == null){ //涓嶆敮鎸� WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); @@ -140,7 +158,7 @@ handshaker.handshake(ctx.channel(), req); } } - + private static void sendHttpResponse(ChannelHandlerContext ctx,FullHttpRequest req,DefaultFullHttpResponse res){ // 杩斿洖搴旂瓟缁欏鎴风 if (res.status().code() != 200) -- Gitblit v1.9.1