Java實現NIO聊天室的示例代碼(群聊+私聊)

功能介紹

功能:群聊+私發+上線提醒+下線提醒+查詢在線用戶

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

文件

Utils

需要用maven導入下面兩個包

 <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
package moremorechat_nio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author mazouri
 * @create 2021-05-09 22:26
 */
@Slf4j
public class Utils {
    /**
     * 將二進制數據轉為對象
     *
     * @param buf
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Message decode(byte[] buf) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (Message) ois.readObject();
    }

    /**
     * 將對象轉為二進制數據
     *
     * @param message
     * @return
     */
    public static byte[] encode(Message message) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(message);
        oos.flush();
        return baos.toByteArray();
    }
}

FinalValue

package moremorechat_nio;
/**
 * @author mazouri
 * @create 2021-05-05 21:00
 */
public final class FinalValue {
    /**
     * 系統消息
     */
    public static final int MSG_SYSTEM = 0;
    /**
     * 群發消息
     */
    public static final int MSG_GROUP = 1;
    /**
     * 私發消息
     */
    public static final int MSG_PRIVATE = 2;
    /**
     * 客戶端請求在線人員
     */
    public static final int MSG_ONLINE = 3;
    /**
     * 客戶端將用戶名稱發送給服務端
     */
    public static final int MSG_NAME = 4;
}

Message

package moremorechat_nio;

import java.io.Serializable;

/**
 * @author mazouri
 * @create 2021-05-05 21:00
 */
public class Message implements Serializable {
    public int type;
    public String message;

    public Message() {
    }

    public Message(String message) {
        this.message = message;
    }

    public Message(int type, String message) {
        this.type = type;
        this.message = message;
    }

    @Override
    public String toString() {
        return "Message{" +
                "type=" + type +
                ", message='" + message + '\'' +
                '}';
    }
}

NioServer

package moremorechat_nio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static moremorechat_nio.FinalValue.*;

/**
 * ctrl+f12 方法
 * ctrl+alt+左鍵
 * @author mazouri
 * @create 2021-05-09 19:24
 */
@Slf4j
public class NioServer {
    private Selector selector;
    private ServerSocketChannel ssc;

    public NioServer() {
        try {
            // 創建 selector, 管理多個 channel
            selector = Selector.open();
            //打開ServerSocketChannel,用於監聽客戶端的連接,它是所有客戶端連接的父通道
            ssc = ServerSocketChannel.open();
            ssc.bind(new InetSocketAddress(8888));
            //設置連接為非堵塞模式
            ssc.configureBlocking(false);
            // 2. 建立 selector 和 channel 的聯系(註冊)
            // SelectionKey 就是將來事件發生後,通過它可以知道事件和哪個channel的事件
            //將ServerSocketChannel註冊到Reactor線程的多路復用器Selector上,監聽ACCEPT事件
            ssc.register(selector, SelectionKey.OP_ACCEPT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        NioServer server = new NioServer();
        log.debug("server啟動完成,等待用戶連接...");
        try {
            server.listen();
        } catch (Exception e) {
            log.debug("發生瞭一些問題");
        }
    }

    /**
     * 監聽用戶的連接
     *
     * @throws Exception
     */
    private void listen() throws Exception {
        while (true) {
            // select 方法, 沒有事件發生,線程阻塞,有事件,線程才會恢復運行, 通過Selector的select()方法可以選擇已經準備就緒的通道 (這些通道包含你感興趣的的事件)
            //通過Selector的select()方法可以選擇已經準備就緒的通道 (這些通道包含你感興趣的的事件)
            // select 在事件未處理時,它不會阻塞, 事件發生後要麼處理,要麼取消,不能置之不理
            selector.select();
            // 處理事件, selectedKeys 內部包含瞭所有發生的事件
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                // 處理key 時,要從 selectedKeys 集合中刪除,否則下次處理就會有問題
                iterator.remove();
                // 區分事件類型
                if (key.isAcceptable()) {
                    ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                    SocketChannel sc = channel.accept();
                    sc.configureBlocking(false);
                    sc.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    dealReadEvent(key);
                }
            }
        }
    }

    /**
     * 處理讀事件
     *
     * @param key
     */
    private void dealReadEvent(SelectionKey key) {
        SocketChannel channel = null;
        try {
            channel = (SocketChannel) key.channel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int read = channel.read(buffer);
            // 如果是正常斷開,read 的方法的返回值是 -1
            if (read == -1) {
                //cancel 會取消註冊在 selector 上的 channel,並從 keys 集合中刪除 key 後續不會再監聽事件
                key.cancel();
            } else {
                buffer.flip();
                Message msg = Utils.decode(buffer.array());
                log.debug(msg.toString());
                dealMessage(msg, key, channel);
            }
        } catch (IOException | ClassNotFoundException e) {
            System.out.println((key.attachment() == null ? "匿名用戶" : key.attachment()) + " 離線瞭..");
            dealMessage(new Message(MSG_SYSTEM, key.attachment() + " 離線瞭.."), key, channel);
            //取消註冊
            key.cancel();
            //關閉通道
            try {
                channel.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    /**
     * 處理各種消息,並發送給客戶端
     *
     * @param msg
     * @param key
     * @param channel
     */
    private void dealMessage(Message msg, SelectionKey key, SocketChannel channel) {
        switch (msg.type) {
            case MSG_NAME:
                key.attach(msg.message);
                log.debug("用戶{}已上線", msg.message);
                getConnectedChannel(channel).forEach(selectionKey -> {
                    SocketChannel sc = (SocketChannel) selectionKey.channel();
                    sendMsgToClient(new Message("收到一條系統消息: " + msg.message + "已上線"), sc);
                });
                break;
            case MSG_GROUP:
                getConnectedChannel(channel).forEach(selectionKey -> {
                    SocketChannel sc = (SocketChannel) selectionKey.channel();
                    sendMsgToClient(new Message(key.attachment() + "給大傢發送瞭一條消息: " + msg.message), sc);
                });
                break;
            case MSG_PRIVATE:
                String[] s = msg.message.split("_");
                AtomicBoolean flag = new AtomicBoolean(false);
                getConnectedChannel(channel).stream().filter(sk -> s[0].equals(sk.attachment())).forEach(selectionKey -> {
                    SocketChannel sc = (SocketChannel) selectionKey.channel();
                    sendMsgToClient(new Message(key.attachment() + "給你發送瞭一條消息: " + s[1]), sc);
                    flag.set(true);
                });
                if (!flag.get()){
                    sendMsgToClient(new Message(s[1]+"用戶不存在,請重新輸入!!!"), channel);
                }

                break;
            case MSG_ONLINE:
                ArrayList<String> onlineList = new ArrayList<>();
                onlineList.add((String) key.attachment());
                getConnectedChannel(channel).forEach(selectionKey -> onlineList.add((String) selectionKey.attachment()));
                sendMsgToClient(new Message(onlineList.toString()), channel);
                break;
            case MSG_SYSTEM:
                getConnectedChannel(channel).forEach(selectionKey -> {
                    SocketChannel sc = (SocketChannel) selectionKey.channel();
                    sendMsgToClient(new Message("收到一條系統消息: " + msg.message), sc);
                });
                break;
            default:
                break;
        }
    }

    /**
     * 發送消息給客戶端
     *
     * @param msg
     * @param sc
     */
    private void sendMsgToClient(Message msg, SocketChannel sc) {
        try {
            byte[] bytes = Utils.encode(msg);
            sc.write(ByteBuffer.wrap(bytes));
        } catch (IOException e) {
            log.debug("sendMsgToClient出現瞭一些問題");
        }
    }

    /**
     * 獲取所有channel,除去調用者
     *
     * @param channel
     * @return
     */
    private Set<SelectionKey> getConnectedChannel(SocketChannel channel) {
        return selector.keys().stream()
                .filter(item -> item.channel() instanceof SocketChannel && item.channel().isOpen() && item.channel() != channel)
                .collect(Collectors.toSet());
    }
}

NioClient

package moremorechat_nio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

import static moremorechat_nio.FinalValue.*;

/**
 * @author mazouri
 * @create 2021-04-29 12:02
 */
@Slf4j
public class NioClient {
    private Selector selector;
    private SocketChannel socketChannel;
    private String username;
    private static Scanner input;

    public NioClient() throws IOException {
        selector = Selector.open();
        socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8888));
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ);
        log.debug("client啟動完成......");
        log.debug("請輸入你的名字完成註冊");
        input = new Scanner(System.in);
        username = input.next();
        log.debug("歡迎{}來到聊天系統", username);
    }

    public static void main(String[] args) throws IOException {
        System.out.println("tips: \n1. 直接發送消息會發給當前的所有用戶 \n2. @用戶名:消息  會私發給你要發送的用戶 \n3. 輸入  查詢在線用戶  會顯示當前的在線用戶");
        NioClient client = new NioClient();

        //啟動一個子線程接受服務器發送過來的消息
        new Thread(() -> {
            try {
                client.acceptMessageFromServer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "receiveClientThread").start();

        //調用sendMessageToServer,發送消息到服務端
        client.sendMessageToServer();
    }

    /**
     * 將消息發送到服務端
     *
     * @throws IOException
     */
    private void sendMessageToServer() throws IOException {
        //先把用戶名發給客戶端
        Message message = new Message(MSG_NAME, username);
        byte[] bytes = Utils.encode(message);

        socketChannel.write(ByteBuffer.wrap(bytes));
        while (input.hasNextLine()) {
            String msgStr = input.next();

            Message msg;
            boolean isPrivate = msgStr.startsWith("@");
            if (isPrivate) {
                int idx = msgStr.indexOf(":");
                String targetName = msgStr.substring(1, idx);
                msgStr = msgStr.substring(idx + 1);
                msg = new Message(MSG_PRIVATE, targetName + "_" + msgStr);
            } else if ("查詢在線用戶".equals(msgStr)) {
                msg = new Message(MSG_ONLINE, "請求在線人數");
            } else {
                msg = new Message(MSG_GROUP, msgStr);
            }

            byte[] bytes1 = Utils.encode(msg);
            socketChannel.write(ByteBuffer.wrap(bytes1));
        }
    }

    /**
     * 接受從服務器發送過來的消息
     */
    private void acceptMessageFromServer() throws Exception {
        while (selector.select() > 0) {
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();
                if (key.isReadable()) {
                    SocketChannel sc = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    sc.read(buffer);
                    Message message = Utils.decode(buffer.array());
                    log.debug(String.valueOf(message.message));
                }
            }
        }
    }
}

到此這篇關於Java實現NIO聊天室的示例代碼(群聊+私聊)的文章就介紹到這瞭,更多相關Java NIO聊天室內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: