基于Netty实时通信场景Demo

方式:

  • Ajax轮询:前端使用ajax不断访问后台(异步);一般适用于小项目或者后台管理;
  • Long pull:阻塞式请求;如果服务器不响应便会阻塞;
  • webSocket:不同于上面两种方式,webSocket使用了http协议完成了一小部分的握手,是一种持久化协议;只需要建立一次请求,服务端就可以推送到客户端;

实现步骤:

参考一个简单的Netty项目

  1. 后台代码
    1. 建立一个package命名为webSocket,在webSocket包中建立server启动类WSServer.java;
      import io.netty.bootstrap.ServerBootstrap;
      import io.netty.channel.ChannelFuture;
      import io.netty.channel.nio.NioEventLoopGroup;
      import io.netty.channel.socket.nio.NioServerSocketChannel;
      
      public class WSServer {
      
          public static void main(String[] args) throws Exception {
      
              NioEventLoopGroup mainGroup = new NioEventLoopGroup();
              NioEventLoopGroup subGroup = new NioEventLoopGroup();
      
              try {
                  ServerBootstrap server = new ServerBootstrap();
                  server.group(mainGroup,subGroup)
                          .channel(NioServerSocketChannel.class)
                          .childHandler(null);
      
                  ChannelFuture future = server.bind(8088).sync();
                  future.channel().closeFuture().sync();
              } finally {
                  mainGroup.shutdownGracefully();
                  subGroup.shutdownGracefully();
              }
      
          }
      }

       

    2. 在webSocket包中建立server初始化类WSServerInitializer.java;
      package com.gyzl.netty.webSocket;
      
      import io.netty.channel.ChannelInitializer;
      import io.netty.channel.ChannelPipeline;
      import io.netty.channel.socket.nio.NioSocketChannel;
      import io.netty.handler.codec.http.HttpObjectAggregator;
      import io.netty.handler.codec.http.HttpServerCodec;
      import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
      import io.netty.handler.stream.ChunkedWriteHandler;
      
      public class WSServerInitializer extends ChannelInitializer {
          protected void initChannel(NioSocketChannel channel) throws Exception {
              ChannelPipeline pipeline = channel.pipeline();
              //webSocket基于http协议,所以需要http编解码器
              pipeline.addLast(new HttpServerCodec())
                      //对写大数据流的支持
                      .addLast(new ChunkedWriteHandler())
                      //聚合器,将零散的HttpMessage聚合成为FullHttpRequest和FullHttpResponse,几乎Netty的编程都需要用到,设置消息的最大的长度
                      .addLast(new HttpObjectAggregator(1024*64));
      
              //=================================================以上是用户支持http协议===========================================================
      
              /**
               * 针对WebSocket处理的协议,用于指定给客户端连接访问的路由
               * 本handler会帮忙处理一些繁琐复杂的工作
               * 处理握手动作:handshaking (close-关闭,ping-请求,pong-响应) ping+pong=心跳
               *对于webSocket来说,传输都是以frames进行传输,不同的数据类型对于的frames不同
               */
              pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
              pipeline.addLast(new ChatHandler());//自定义的handler
          }
      }
      

       

    3. 在webSocket包中建立server自定义处理器类ChatHandler.java;
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.channel.SimpleChannelInboundHandler;
      import io.netty.channel.group.ChannelGroup;
      import io.netty.channel.group.DefaultChannelGroup;
      import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
      import io.netty.util.concurrent.GlobalEventExecutor;
      
      import java.time.LocalDateTime;
      
      /**
       * @Description: 自定义助手类
       */
      //SimpleChannelInboundHandler:对于请求来讲,相当于入站
      //TextWebSocketFrame为专门为WebSocket处理消息的对象,frame是消息的载体
      public class ChatHandler extends SimpleChannelInboundHandler {
      
          //用于记录和管理所有客户端的Channel
          private static ChannelGroup clients=new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
      
      
      
          protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
              //获取客户端传输过来的消息
              String content = msg.text();
              System.out.println("接受到的消息"+content);
              /*    for (Channel channel : clients) {
                      //writeAndFlush不可以传输一个字符串
                      channel.writeAndFlush(
                              new TextWebSocketFrame("[服务器在]"+ LocalDateTime.now()+"接受到消息,消息为:"+content));
              }*/
              // 与上面的循环一致
              clients.writeAndFlush( new TextWebSocketFrame("[服务器在]"+ LocalDateTime.now()+"接受到消息,消息为:"+content));
      
      
          }
      
          @Override
          public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
              //当客户端连接服务端,获取客户但的channel并且放到channelGroup中管理
              clients.add(ctx.channel());
      
          }
      
          @Override
          public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
              //当触发handlerRemoved,channelGroup会自动移除相对应的channel,所以下面一行代码可以省略
              //clients.remove(ctx.channel());
              System.out.println("客户端断开,channel对应的长id为"+ctx.channel().id().asLongText());
              System.out.println("客户端断开,channel对应的短id为"+ctx.channel().id().asShortText());
          }
      }
      

       

    4. 将ChatHandler声明到WSServerInitializer初始化器中,将WSServerInitializer初始化器声明到WSServer启动类中;
       pipeline.addLast(new ChatHandler());//自定义的handler
       server.group(mainGroup,subGroup)
                          .channel(NioServerSocketChannel.class)
                          .childHandler(new WSServerInitializer());

       

    5. 启动WSServer,后台完成;
  2. 前端连接:
    1. 核心概念:
      1. Websocket api
        1. var soket=new WebSocket("ws://[ip]:[port]");      
        2. 生命周期:onopen()   onmessage()   onerror()   onclose()
        3. 主动方法:Socket.send()         Socket.close()                                                                                                  
    2. 使用工具Hbuilder建立index.html
      
      
      	
      		
      		
      	
      	
      		
      		
      发送消息:
      接受消息:

       

    3. 完成后使用浏览器运行index.html
  3. 测试:
    1. 浏览器:基于Netty实时通信场景Demo_第1张图片
    2. 后台:
    3. 完成

 

 

你可能感兴趣的:(学习笔记)