|
@@ -5,10 +5,12 @@ import lombok.Data;
|
|
|
import java.io.Closeable;
|
|
import java.io.Closeable;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.net.InetSocketAddress;
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
+import java.nio.ByteBuffer;
|
|
|
import java.nio.channels.SelectionKey;
|
|
import java.nio.channels.SelectionKey;
|
|
|
import java.nio.channels.Selector;
|
|
import java.nio.channels.Selector;
|
|
|
import java.nio.channels.ServerSocketChannel;
|
|
import java.nio.channels.ServerSocketChannel;
|
|
|
import java.nio.channels.SocketChannel;
|
|
import java.nio.channels.SocketChannel;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
|
|
@@ -59,6 +61,13 @@ public class ChatServer implements Runnable, Closeable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ * @description: 服务端监听客户端连接
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/22 19:24:34
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
private void listen() throws IOException {
|
|
private void listen() throws IOException {
|
|
|
//注册连接事件
|
|
//注册连接事件
|
|
|
serverSocketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
|
|
serverSocketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
|
|
@@ -86,6 +95,51 @@ public class ChatServer implements Runnable, Closeable {
|
|
|
this.close();
|
|
this.close();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param msg 信息
|
|
|
|
|
+ * @description: 服务端广播信息到客户端
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/22 19:25:58
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ public void broadcast(String msg){
|
|
|
|
|
+ //获取所有的key
|
|
|
|
|
+ Set<SelectionKey> keys = readSelector.keys();
|
|
|
|
|
+ for (SelectionKey key : keys) {
|
|
|
|
|
+ SocketChannel channel = (SocketChannel) key.channel();
|
|
|
|
|
+ try {
|
|
|
|
|
+ //发信息
|
|
|
|
|
+ channel.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param msg 信息
|
|
|
|
|
+ * @param target 目标
|
|
|
|
|
+ * @description: 发送私聊信息
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/22 19:31:09
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendMessageWithPrivate(String msg,String target){
|
|
|
|
|
+ //todo
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param msg 信息
|
|
|
|
|
+ * @param group 群
|
|
|
|
|
+ * @description: 群发信息
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/22 19:32:08
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ public void sendMessageWithGroup(String msg,String group){
|
|
|
|
|
+ //todo
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public void close() throws IOException {
|
|
public void close() throws IOException {
|
|
|
exit();
|
|
exit();
|