简单介绍:netty做服务器的websocket项目,需要取得用户的真实IP,线上项目前面加了Nginx代理;
没有Nginx的的话,netty可以通过一下代码获取客户端IP,加了取到的是Nginx主机的IP:
InetSocketAddress address = (InetSocketAddress) channel.remoteAddress(); String ip = address.getAddress().getHostAddress();所以需要在Nginx转发真实IP
http { include mime.types; default_type application/octet-stream; server { listen 39200; server_name www.aaa.com:39200; #防XSS攻擊 add_header X-Xss-Protection 1; location / { # WebSocket Header proxy_http_version 1.1; proxy_set_header Upgrade websocket; proxy_set_header Connection "Upgrade"; # 将客户端的 Host 和 IP 信息一并转发到对应节点 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; # 客户端与服务端无交互 60s 后自动断开连接,请根据实际业务场景设置 proxy_read_timeout 300s ; # 执行代理访问真实服务器 proxy_pass http://192.168.0.41:39200; } } }首先看看服务器用到的handler:
pipeline.addLast(new HttpServerCodec());// HTTP编码解码器 pipeline.addLast(new HttpObjectAggregator(wsHttpObjectAggregator));// 把HTTP头、HTTP体拼成完整的HTTP请求 pipeline.addLast(new WebSocketServerProtocolHandler("/"));//完成http到websocket的升级,相当于只是建立连接过程中用到 pipeline.addLast(new NettyWsTextHandler());//自定义消息处理类NettyWsTextHandler:唯一自己实现的处理消息类:
public class NettyWsTextHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { @Override public void channelActive(ChannelHandlerContext session) throws Exception { LoggerUtil.info("Server channelActive: {}", session); } @Override protected void channelRead0(ChannelHandlerContext session, TextWebSocketFrame msg) throws Exception { LoggerUtil.info("message: {}", msg.text()); } }这步的消息已经是websocket的数据了,拿不到http里面的header信息,比如nginx转发的X-Real-IP
所以需要加一个handle专门用来获取http数据,如下:
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpHeaders; public class HttpHeadersHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof FullHttpRequest) { HttpHeaders headers = ((FullHttpRequest) msg).headers(); String ip = headers.get("X-Real-IP"); System.out.println("Realip:" + ip); } ctx.fireChannelRead(msg); } }要注意,集成:ChannelInboundHandlerAdapter 不要集成:SimpleChannelInboundHandler 后者需要维护引用计数
添加后的handler管道如下:
ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec());// HTTP编码解码器 pipeline.addLast(new HttpObjectAggregator(wsHttpObjectAggregator));// 把HTTP头、HTTP体拼成完整的HTTP请求 // 获取header需要在http到websocket升级完成之前,升级后就取不到了 pipeline.addLast(new HttpHeadersHandler()); pipeline.addLast(new WebSocketServerProtocolHandler(wsPath));// 完成握手,http到websocket升级 pipeline.addLast(new NettyWsTextHandler());