代码演示
可以和【使用 Netty 实现简单群聊系统(https://www.geekerstar.com/30862.html)】二者结合对比NIO与Netty使用的区别
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadLocalRandom; public class ChatServer { public static void main(String[] args) throws IOException { Selector selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.bind(new InetSocketAddress(8080)); serverSocketChannel.configureBlocking(false); // 将accept事件绑定到selector上 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { // 阻塞在select上 selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); // 遍历selectKeys Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); // 如果是accept事件 if (selectionKey.isAcceptable()) { ServerSocketChannel ssc = (ServerSocketChannel) selectionKey.channel(); SocketChannel socketChannel = ssc.accept(); System.out.println("accept new conn: " + socketChannel.getRemoteAddress()); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); // 加入群聊 ChatHolder.join(socketChannel); } else if (selectionKey.isReadable()) { // 如果是读取事件 SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); // 将数据读入到buffer中 int length = socketChannel.read(buffer); if (length > 0) { buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; // 将数据读入到byte数组中 buffer.get(bytes); // 换行符会跟着消息一起传过来 String content = new String(bytes, "UTF-8").replace("\r\n", ""); if (content.equalsIgnoreCase("quit")) { // 退出群聊 ChatHolder.quit(socketChannel); selectionKey.cancel(); socketChannel.close(); } else { // 扩散 ChatHolder.propagate(socketChannel, content); } } } iterator.remove(); } } } private static class ChatHolder { private static final Map<SocketChannel, String> USER_MAP = new ConcurrentHashMap<>(); /** * 加入群聊 * @param socketChannel */ public static void join(SocketChannel socketChannel) { // 有人加入就给他分配一个id String userId = "用户"+ ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE); send(socketChannel, "您的id为:" + userId + "\n\r"); for (SocketChannel channel : USER_MAP.keySet()) { send(channel, userId + " 加入了群聊" + "\n\r"); } // 将当前用户加入到map中 USER_MAP.put(socketChannel, userId); } /** * 退出群聊 * @param socketChannel */ public static void quit(SocketChannel socketChannel) { String userId = USER_MAP.get(socketChannel); send(socketChannel, "您退出了群聊" + "\n\r"); USER_MAP.remove(socketChannel); for (SocketChannel channel : USER_MAP.keySet()) { if (channel != socketChannel) { send(channel, userId + " 退出了群聊" + "\n\r"); } } } /** * 扩散说话的内容 * @param socketChannel * @param content */ public static void propagate(SocketChannel socketChannel, String content) { String userId = USER_MAP.get(socketChannel); for (SocketChannel channel : USER_MAP.keySet()) { if (channel != socketChannel) { send(channel, userId + ": " + content + "\n\r"); } } } /** * 发送消息 * @param socketChannel * @param msg */ private static void send(SocketChannel socketChannel, String msg) { try { ByteBuffer writeBuffer = ByteBuffer.allocate(1024); writeBuffer.put(msg.getBytes()); writeBuffer.flip(); socketChannel.write(writeBuffer); } catch (Exception e) { e.printStackTrace(); } } } }
效果展示
模拟群聊,启动四个 xshell窗口,模拟聊天
常见问题FAQ
- 如果资源链接失效了怎么办?
- 本站用户分享的所有资源都有自动备份机制,如果资源链接失效,请联系本站客服QQ:2580505920更新资源地址。
- 如果用户分享的资源与描述不符怎么办?
- 如何分享个人资源获取赞助积分或其他奖励?
- 如果您发现了本资源有侵权行为怎么办?