基于Netty实现多人聊天室

本篇博客将介绍如何使用Netty框架构建一个简易的多人聊天室。该聊天室支持用户登录、查看聊天室列表、发送群聊消息以及私聊消息等功能。

1. 服务器端

1.1 代码

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

import java.util.concurrent.ConcurrentHashMap;

public class MultiChatServer {
    private final int port;
    private final ConcurrentHashMap<String, ChannelHandlerContext> userMap = new ConcurrentHashMap<>();
    private final EventExecutorGroup group = new DefaultEventExecutorGroup(16);

    public MultiChatServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new StringDecoder(), new StringEncoder(), new MultiChatServerHandler(userMap, group));
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture f = b.bind(port).sync();
            System.out.println('Server started on port ' + port);
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port = 8080;
        new MultiChatServer(port).run();
    }
}

class MultiChatServerHandler extends SimpleChannelInboundHandler<String> {
    private final ConcurrentHashMap<String, ChannelHandlerContext> userMap;
    private final EventExecutorGroup group;

    public MultiChatServerHandler(ConcurrentHashMap<String, ChannelHandlerContext> userMap, EventExecutorGroup group) {
        this.userMap = userMap;
        this.group = group;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println('Client connected: ' + ctx.channel().remoteAddress());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println('Client disconnected: ' + ctx.channel().remoteAddress());
        userMap.values().remove(ctx);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        String[] tokens = msg.split('\|');
        String command = tokens[0];

        switch (command) {
            case 'LOGIN':
                handleLogin(ctx, tokens[1], tokens[2]);
                break;
            case 'LIST':
                handleList(ctx);
                break;
            case 'MSG':
                handleMessage(ctx, tokens[1], tokens[2]);
                break;
            case 'PRIVATE':
                handlePrivateMessage(ctx, tokens[1], tokens[2]);
                break;
            default:
                ctx.writeAndFlush('Invalid command: ' + command);
                break;
        }
    }

    private void handleLogin(ChannelHandlerContext ctx, String username, String password) {
        if (userMap.containsKey(username)) {
            ctx.writeAndFlush('User already logged in: ' + username);
        } else {
            userMap.put(username, ctx);
            ctx.writeAndFlush('Login successful: ' + username);
        }
    }

    private void handleList(ChannelHandlerContext ctx) {
        StringBuilder sb = new StringBuilder();
        sb.append('Available users:\n');
        for (Map.Entry<String, ChannelHandlerContext> entry : userMap.entrySet()) {
            sb.append(entry.getKey()).append('\n');
        }
        ctx.writeAndFlush(sb.toString());
    }

    private void handleMessage(ChannelHandlerContext ctx, String roomName, String message) {
        StringBuilder sb = new StringBuilder();
        sb.append('[').append(ctx.channel().remoteAddress()).append('] ').append(message).append('\n');
        for (Map.Entry<String, ChannelHandlerContext> entry : userMap.entrySet()) {
            if (entry.getKey().equals(roomName)) {
                entry.getValue().writeAndFlush(sb.toString());
            }
        }
    }

    private void handlePrivateMessage(ChannelHandlerContext ctx, String recipient, String message) {
        StringBuilder sb = new StringBuilder();
        sb.append('[').append(ctx.channel().remoteAddress()).append('] (private) ').append(message).append('\n');
        if (userMap.containsKey(recipient)) {
            userMap.get(recipient).writeAndFlush(sb.toString());
        } else {
            ctx.writeAndFlush('User not found: ' + recipient);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

1.2 功能说明

  • 使用Netty框架搭建服务器,监听指定端口
  • 使用ConcurrentHashMap存储在线用户信息,键为用户名,值为ChannelHandlerContext
  • 处理四种命令:
    • LOGIN:用户登录,格式为LOGIN|用户名|密码
    • LIST:获取在线用户列表
    • MSG:发送群聊消息,格式为MSG|聊天室名|消息内容
    • PRIVATE:发送私聊消息,格式为PRIVATE|接收用户名|消息内容

2. 客户端

2.1 代码

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MultiChatClient {
    private final String host;
    private final int port;
    private ChannelHandlerContext ctx;

    public MultiChatClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new StringDecoder(), new StringEncoder(), new MultiChatClientHandler());
                        }
                    });

            ChannelFuture f = b.connect(host, port).sync();
            ctx = f.channel().pipeline().context(MultiChatClientHandler.class);

            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                String line = in.readLine();
                if (line == null) {
                    break;
                }
                ctx.writeAndFlush(line + '\n');
            }
        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        String host = 'localhost';
        int port = 8080;
        new MultiChatClient(host, port).run();
    }
}

class MultiChatClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

2.2 功能说明

  • 连接服务器
  • 从控制台读取用户输入
  • 将用户输入发送至服务器
  • 接收并打印服务器返回的消息

3. 运行示例

  1. 启动服务器
  2. 启动两个客户端
  3. 在客户端1输入LOGIN|user1|123登录
  4. 在客户端2输入LOGIN|user2|123登录
  5. 在客户端1输入LIST查看在线用户,应该能看到user1user2
  6. 在客户端1输入MSG|user2|Hello向user2发送私聊消息
  7. 客户端2应该能收到[user1] (private) Hello

This is an example of how to build a simple chat room application using Netty. You can further expand this example by adding features like group chat, file sharing, and user authentication.

This example demonstrates the basic concepts of Netty and how to use it to build network applications. For more advanced features and best practices, please refer to the official Netty documentation.

基于Netty实现的多人聊天室

原文地址: https://www.cveoy.top/t/topic/f3rR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录