一、实例要求

  • Http 协议是无状态的,浏览器和服务器间的请求响应一次,下一次会重新创建连接。
  • 要求:实现基于 WebSocket 的长连接的全双工的交互。
  • 改变 Http 协议多次请求的约束,实现长连接了,服务器可以发送消息给浏览器。
  • 客户端浏览器和服务器端会相互感知,比如服务器关闭了,浏览器会感知,同样浏览器关闭了,服务器会感知。
  • 运行界面。

    二、代码演示

    MyServer

    1. public class MyServer {
    2. public static void main(String[] args) throws InterruptedException {
    3. // 创建两个线程
    4. NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    5. NioEventLoopGroup workerGroup = new NioEventLoopGroup(3);
    6. try {
    7. ServerBootstrap serverBootstrap = new ServerBootstrap();
    8. serverBootstrap.group(bossGroup, workerGroup)
    9. .channel(NioServerSocketChannel.class)
    10. .handler(new LoggingHandler(LogLevel.INFO))// 在 bossGroup 增加一个日志处理器
    11. .childHandler(new ChannelInitializer<SocketChannel>() {
    12. @Override
    13. protected void initChannel(SocketChannel ch) throws Exception {
    14. ChannelPipeline pipeline = ch.pipeline();
    15. // 因为基于 http 协议,使用 http 的编码和解码器
    16. pipeline.addLast(new HttpServerCodec());
    17. // 是以块方式写,添加 ChunkedWriteHandler 处理器
    18. pipeline.addLast(new ChunkedWriteHandler());
    19. /**
    20. * 说明
    21. * 1、http 数据在传输过程中是分段,HttpObjectAggregator,就是可以将多个段聚合起来
    22. * 2、这就是为什么,当浏览器发送大量数据时,就会发出多次http请求
    23. */
    24. pipeline.addLast(new HttpObjectAggregator(8192));
    25. /**
    26. * 说明
    27. * 1、对应 websocket,它的数据是以 帧(frame)形式传递
    28. * 2、可以看到 WebSocketFrame,下面有六个子类
    29. * 3、浏览器请求时 ws://localhost:6699/hello 表示请求的url
    30. * 4、WebSocketServerProtocolHandler 核心功能是将 Http 协议 升级成 ws 协议,保持长连接
    31. * 5、是通过一个状态码 101
    32. */
    33. pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));
    34. // 自定义handler,处理业务逻辑
    35. pipeline.addLast(new MyTextWebsocketFrameHandler());
    36. }
    37. });
    38. // 启动服务器
    39. ChannelFuture channelFuture = serverBootstrap.bind(6699).sync();
    40. channelFuture.channel().closeFuture().sync();
    41. } finally {
    42. bossGroup.shutdownGracefully();
    43. workerGroup.shutdownGracefully();
    44. }
    45. }
    46. }

    MyTextWebsocketFrameHandler

    1. public class MyTextWebsocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    2. @Override
    3. protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
    4. System.out.println("服务器端收到消息 " + msg.text());
    5. // 回复
    6. ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器时间 " + LocalDateTime.now() + " " + msg.text()));
    7. }
    8. /**
    9. * 当 web 客户端连接后,触发方法
    10. *
    11. * @param ctx
    12. * @throws Exception
    13. */
    14. @Override
    15. public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    16. // id 表示唯一的值,LongText 是唯一的 ShortText 不是唯一
    17. System.out.println("handlerAdded 被调用" + ctx.channel().id().asLongText());
    18. System.out.println("handlerAdded 被调用" + ctx.channel().id().asShortText());
    19. }
    20. @Override
    21. public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    22. System.out.println("handlerRemoved 被调用" + ctx.channel().id().asLongText());
    23. }
    24. @Override
    25. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    26. System.out.println("异常发生 " + cause.getMessage());
    27. ctx.close(); // 关闭
    28. }
    29. }

    hellp.html

    ```java <!DOCTYPE html>

  1. <a name="T1HaP"></a>
  2. # 三、演示![image.png](https://cdn.nlark.com/yuque/0/2021/png/1460038/1639916459477-2368b52e-e64f-4153-8630-8e8646efa2eb.png#clientId=u2532108b-e099-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=394&id=u3f7b3172&margin=%5Bobject%20Object%5D&name=image.png&originHeight=788&originWidth=1818&originalType=binary&ratio=1&rotation=0&showTitle=false&size=190158&status=done&style=none&taskId=u05ffd2bf-ddac-4fdb-a056-5f6c23df47d&title=&width=909)
  3. <a name="s4RYl"></a>
  4. # 四、分析
  5. 关注以下代码段,netty 通过 WebSocketServerProtocolHandler 将 http 协议连接升级到 Websocket 协议。
  6. ```java
  7. .childHandler(new ChannelInitializer<SocketChannel>() {
  8. @Override
  9. protected void initChannel(SocketChannel ch) throws Exception {
  10. ChannelPipeline pipeline = ch.pipeline();
  11. // 因为基于 http 协议,使用 http 的编码和解码器
  12. pipeline.addLast(new HttpServerCodec());
  13. // 是以块方式写,添加 ChunkedWriteHandler 处理器
  14. pipeline.addLast(new ChunkedWriteHandler());
  15. /**
  16. * 说明
  17. * 1、http 数据在传输过程中是分段,HttpObjectAggregator,就是可以将多个段聚合起来
  18. * 2、这就是为什么,当浏览器发送大量数据时,就会发出多次http请求
  19. */
  20. pipeline.addLast(new HttpObjectAggregator(8192));
  21. /**
  22. * 说明
  23. * 1、对应 websocket,它的数据是以 帧(frame)形式传递
  24. * 2、可以看到 WebSocketFrame,下面有六个子类
  25. * 3、浏览器请求时 ws://localhost:6699/hello 表示请求的url
  26. * 4、WebSocketServerProtocolHandler 核心功能是将 Http 协议 升级成 ws 协议,保持长连接
  27. * 5、是通过一个状态码 101
  28. */
  29. pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));
  30. // 自定义handler,处理业务逻辑
  31. pipeline.addLast(new MyTextWebsocketFrameHandler());
  32. }
  33. });