基于Netty的聊天小Demo

原理

每个客户端通过WebSocket与服务器建立连接后,服务端新建了一个ChannelGroup,把所有的客户端添加进去,每当客户端有信息时,服务端会把客户端发送过来的信息,通过遍历ChannelGroup,发送给所有的客户端。

maven

<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.25.Final</version>
        </dependency>

客户端

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>在线聊天室</title>
  </head>
  <body>
    <input type="text" id="message" />
    <input type="button" value="发送消息" onclick="sendMsg()" />

    接收到的消息:
    <p id="server_message" style="background-color: #aaaaaa"></p>

    <script>
      var websocket = null;

      // 判断当前浏览器是否支持websocket
      if (window.WebSocket) {
        websocket = new WebSocket("ws://127.0.0.1:9090/ws");

        websocket.onopen = function () {
          console.log("建立连接.");
        };
        websocket.onclose = function () {
          console.log("断开连接");
        };
        websocket.onmessage = function (e) {
          console.log("接收到服务器消息:" + e.data);
          var server_message = document.getElementById("server_message");
          server_message.innerHTML += e.data + "<br/>";
        };
      } else {
        alert("当前浏览器不支持web socket");
      }

      function sendMsg() {
        var message = document.getElementById("message");
        websocket.send(message.value);
      }
    </script>
  </body>
</html>

服务端

WebSocketNettyServer

public class WebSocketNettyServer {
    public static void main(String[] args) {
        // 创建两个线程池
        NioEventLoopGroup mainGrp = new NioEventLoopGroup(); // 主线程池
        NioEventLoopGroup subGrp = new NioEventLoopGroup(); // 从线程池

        try {
            // 创建Netty服务器启动对象
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            // 初始化服务器启动对象
            serverBootstrap
                    // 指定使用上面创建的两个线程池
                    .group(mainGrp, subGrp)
                    // 指定Netty通道类型
                    .channel(NioServerSocketChannel.class)
                    // 指定通道初始化器用来加载当Channel收到事件消息后,
                    // 如何进行业务处理
                    .childHandler(new WebSocketChannelInitializer());

            // 绑定服务器端口,以同步的方式启动服务器
            ChannelFuture future = serverBootstrap.bind(9090).sync();
            // 等待服务器关闭
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 优雅关闭服务器
            mainGrp.shutdownGracefully();
            subGrp.shutdownGracefully();
        }
    }
}

WebSocketChannelInitializer

public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    // 初始化通道
    // 在这个方法中去加载对应的ChannelHandler
    protected void initChannel(SocketChannel ch) throws Exception {
        // 获取管道,将一个一个的ChannelHandler添加到管道中
        ChannelPipeline pipeline = ch.pipeline();

        // 添加一个http的编解码器
        pipeline.addLast(new HttpServerCodec());
        // 添加一个用于支持大数据流的支持
        pipeline.addLast(new ChunkedWriteHandler());
        // 添加一个聚合器,这个聚合器主要是将HttpMessage聚合成FullHttpRequest/Response
        pipeline.addLast(new HttpObjectAggregator(1024 * 64));

        // 需要指定接收请求的路由
        // 必须使用以ws后缀结尾的url才能访问
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        // 添加自定义的Handler
        pipeline.addLast(new ChatHandler());
    }
}

WebSocketNettyServer

public class WebSocketNettyServer {
    public static void main(String[] args) {
        // 创建两个线程池
        NioEventLoopGroup mainGrp = new NioEventLoopGroup(); // 主线程池
        NioEventLoopGroup subGrp = new NioEventLoopGroup(); // 从线程池

        try {
            // 创建Netty服务器启动对象
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            // 初始化服务器启动对象
            serverBootstrap
                    // 指定使用上面创建的两个线程池
                    .group(mainGrp, subGrp)
                    // 指定Netty通道类型
                    .channel(NioServerSocketChannel.class)
                    // 指定通道初始化器用来加载当Channel收到事件消息后,
                    // 如何进行业务处理
                    .childHandler(new WebSocketChannelInitializer());

            // 绑定服务器端口,以同步的方式启动服务器
            ChannelFuture future = serverBootstrap.bind(9090).sync();
            // 等待服务器关闭
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 优雅关闭服务器
            mainGrp.shutdownGracefully();
            subGrp.shutdownGracefully();
        }
    }
}

效果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值