Netty(3)之WebSocket协议开发时间服务器

WebSocket协议开发

1. Http协议弊端

  • 半双工协议:同一时刻,只有一个方向上的数据传送(客户端 --> 服务端 或者 服务端 --> 客户端)
  • 消息冗长繁琐
  • 针对服务器推送的黑客攻击。例如长时间轮询,比较新的技术Comet,使用了Ajax,这种技术会大量消耗服务器带宽和资源

2. WebSocket入门

2.1 特点

  • 单一的TCP连接,采用全双工模式通信
  • 对代理、防火墙和路由器透明
  • 无头部信息、Cookie和身份验证
  • 无安全开销
  • 通过“ping/pong”帧保持链路激活
  • 服务器可以主动传递消息给客户端,不在需要客户端轮询

2.2 请求消息

  • Upgrade:表示将http协议升级到WebSocket协议

  • Sec-WebSocket-Key:随机字符串

Netty(3)之WebSocket协议开发时间服务器_第1张图片

2.3 应答消息

  • Sec-WebSocket-Accept: 将WebSocket-Key加上一个魔幻字符串,然后使用SHA-1加密,进行BASE-64编码
    在这里插入图片描述

2.4 请求关闭

正常情况下,应该由服务器先关闭。异常情况下(一个合理时间周期后没有收到服务器的TCP close),客户端可以发起TCP Close。当服务器被指示关闭时,会立即发起关闭WebSocket TCP close操作;客户端应该等待服务器的TCP Close。WebSocket的握手关闭消息带有一个状态码核可选的关闭愿意,必须按照协议要求发送一个Close控制帧,对端收到后需要主动关闭WebSocket连接。

3. 时间服务器

  • 绑定端口,创建netty服务端
  • http-codec:添加Http请求和响应的编码和解码器
  • aggregator:合并http请求
  • http-chunked:添加大文件传输器
  • handler:自定义处理器
public void bind(int port) {
     
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        try {
     
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                .group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
     

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
     
                        ch.pipeline()
                            //将请求和应答消息解码或者编码成http消息
                            .addLast("http-codec", new HttpServerCodec())
                            .addLast("aggregator", new HttpObjectAggregator(65536))
                            .addLast("http-chunked", new ChunkedWriteHandler())
                            .addLast("handler", new TimeWebSocketServerHandler());
                    }
                });
            Channel channelFuture = serverBootstrap.bind(port).sync().channel();
            System.out.println("Web Socket Server started at port :" + port + ".");
            System.out.println("open you browser and navigate to http://localhost:" + port + "/");
            channelFuture.closeFuture().sync();
        } catch (InterruptedException e) {
     
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

3.1 自定义处理器

public static class TimeWebSocketServerHandler extends SimpleChannelInboundHandler<Object> {
     

        private WebSocketServerHandshaker webSocketServerHandshaker;

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
     
            if (msg instanceof FullHttpRequest) {
     
                this.handleHttpRequest(ctx, (FullHttpRequest) msg);
            } else {
     
                this.handleWebSocketRequest(ctx, (WebSocketFrame) msg);
            }
        } 
    }

handleHttpRequest()

  • 判断http请求有没有解码成功,或者协议中没有 Upgrade = websocket 值
  • websocket握手工厂创建 握手协议,如果不支持返回错误码
  • 握手时,就会把 websocket相关的编解码器动态的添加到管道中,后续使用websocket相关对象可以直接获取
/**
         * 处理http请求
         * @param ctx
         * @param fullHttpRequest
         */
        private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) {
     
            //http解码失败,并且协议并不是升级为websocket
            if (!fullHttpRequest.decoderResult().isSuccess() ||
                (!"websocket".equals(fullHttpRequest.headers().get("Upgrade")))) {
     
                    sendHttpResponse(ctx, fullHttpRequest, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                                                                                       HttpResponseStatus.BAD_REQUEST));
                    return;
            }
            WebSocketServerHandshakerFactory webSocketServerHandshakerFactory
                = new WebSocketServerHandshakerFactory("ws://localhost:8080/websocket", null, false);
            webSocketServerHandshaker = webSocketServerHandshakerFactory.newHandshaker(fullHttpRequest);
            if (Objects.isNull(webSocketServerHandshaker)) {
     
                WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
            } else {
     
                //创建握手时,就会将websocket的编码解码添加到管道中
                webSocketServerHandshaker.handshake(ctx.channel(), fullHttpRequest);
            }
        }

handleWebSocketRequest()

  • 判断各种类型的WebSocketFrame请求,根据类型操作
private void handleWebSocketRequest(ChannelHandlerContext ctx, WebSocketFrame webSocketFrame) {
     
            //判断链路是否是关闭的指令
            if (webSocketFrame instanceof CloseWebSocketFrame) {
     
                webSocketServerHandshaker.close(ctx.channel(), (CloseWebSocketFrame) webSocketFrame.retain());
                return;
            }

            if (webSocketFrame instanceof PingWebSocketFrame) {
     
                ctx.channel().write(new PongWebSocketFrame(webSocketFrame.content().retain()));
                return;
            }

            //只支持文本消息,不支持二进制消息
            if (!(webSocketFrame instanceof TextWebSocketFrame)) {
     
                throw new UnsupportedOperationException(String.format("%s frame types not supported", webSocketFrame.getClass().getName()));
            }

            //返回应答消息
            String text = ((TextWebSocketFrame) webSocketFrame).text();
            ctx.channel().writeAndFlush(new TextWebSocketFrame(text + ", 欢迎使用netty websocket服务,现在是北京时间:"+ new Date().toString()));
        }

sendHttpResponse()

  • 判断状态,写入返回值数据
  • 判断请求是否是长连接,如果不是,添加关闭的监听器
private static void sendHttpResponse(ChannelHandlerContext ctx,
                                             FullHttpRequest request,
                                             FullHttpResponse response) {
     
            if (response.status().code() != 200) {
     
                ByteBuf byteBuf = Unpooled.copiedBuffer(response.status().toString(), CharsetUtil.UTF_8);
                response.content().writeBytes(byteBuf);
                byteBuf.release();
                HttpUtil.setContentLength(response, response.content().readableBytes());
            }

            ChannelFuture future = ctx.channel().writeAndFlush(response);
            if (!HttpUtil.isKeepAlive(request) || response.status().code() != 200) {
     
                future.addListener(ChannelFutureListener.CLOSE);
            }
        }

4. JS页面代码

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Netty WebSocket 时间服务器title>
head>
<body>
<script>
  var socket;
  if (!window.WebSocket) {
       
    window.WebSocket = window.MozWebSocket;
  }
  if (window.WebSocket) {
       
    socket = new WebSocket("ws://localhost:8080/websocket");
    socket.onmessage = function (event) {
       
      var ta = document.getElementById('responseText');
      ta.value = "";
      ta.value = event.data;
    }
    socket.onopen = function (event) {
       
      var ta = document.getElementById('responseText');
      ta.value = "打开WebSocket服务正常,浏览器支持WebSocket"
    }
    socket.onclose = function (event) {
       
      var ta = document.getElementById('responseText');
      ta.value = ""
      ta.value = "WebSocket关闭"
    }
  } else {
       
    alert("抱歉您的浏览器不支持WebSocket协议");
  }
  function send(message) {
       
    if (!window.WebSocket) {
       
      return;
    }
    if (socket.readyState == WebSocket.OPEN) {
       
      socket.send(message)
    } else {
       
      alert("WebSocket连接没有建立成功");
    }
  }
script>

<form onsubmit="return false;">
    <input type="text" name="message" value="Netty 最佳实践">
    <br><br>
    <input type="button" value="发送 WebSocket请求消息" onclick="send(this.form.message.value)"/>
    <hr color="blue"/>
    <h3>服务端返回应答消息h3>
    <textarea id="responseText" style="width: 500px;height: 300px">

        textarea>
form>
body>
html>

你可能感兴趣的