gpt4 book ai didi

java - 从聊天中删除用户 (Java)

转载 作者:行者123 更新时间:2023-12-01 04:45:46 25 4
gpt4 key购买 nike

我使用 Java 开发了一个客户端/服务器聊天应用程序,我想知道如何从数组中删除用户。当特定客户端登录时,用户名保存在用户名数组中,客户端 ID 保存在客户端数组中。为了允许服务器接受多个客户端,我使用线程。现在任何人都可以指导我如何从阵列中删除用户并关闭该用户的连接。

添加新客户端并将 ID 保存在客户端数组中

public class AddClient implements Runnable {

Thread t;

AddClient(String tot) {
t = new Thread(this, tot);
t.start();
}

public void run() {
while (true) {
try {
try {
waitClient();
} catch (Exception ex) {
ex.printStackTrace();

}

for (int i = 0; i < client.length; i++) {
if (client[i] == 0) {
client[i] = i + 1;
id = i;
break;
}
}

//set stream to send and receive data
out[client[id]] = new ObjectOutputStream(connect.getOutputStream());
out[client[id]].flush();
in[client[id]] = new ObjectInputStream(connect.getInputStream());

用户名保存在用户名数组中

username[client[id]] = cm.sender; //Add user in username[] array

正在删除用户

public synchronized void removeUser(int number) {
int position = number;
System.out.println("Server removing user " + username[number] + "which is client " + number);

for (int i = 0; i <= client.length; i++) {
if (position == client[i]) {
System.out.println("User to be remove found");
try {
client[i + 1] = client[i];
in[position].close();
out[position].close();
username[position] = null;
position = position - 1;

} catch (Exception e) {
e.printStackTrace();
}
}
}

我正在尝试使用 HashTable 来添加和删除客户端

public class ChatServerProtocol {
private String nick;
private AddClient a;

private Hashtable<String, AddClient> nicks = new Hashtable<String, AddClient>();


private boolean add_nick(String nick, AddClient a) {
if (nicks.containsKey(nick)) {
return false;
} else {
nicks.put(nick, a);
return true;
}
}

private boolean remove_nick(String nick, AddClient a) {
if (!(nicks.containsKey(nick))) {
return false;
} else {
nicks.remove(nick);
return true;
}
}

public ChatServerProtocol(AddClient a) throws IOException {
nick = null;
a = a;
}

但是现在我如何调用方法add_nick。每当客户端登录时,用户名就会发送到服务器,服务器将其读取为 cm.sender。我还需要包含线程变量。那么如何添加用户名以便稍后我可以将其删除。

  ChatServerProtocol.add_nick(cm.sender);

最佳答案

不,保存在数据库中不是一个好主意。请记住,您仅在 session 长度内保存详细信息,而数据库的基本概念是在 session 后使用它。如果您的 session 由于网络问题等而中断会发生什么?

只需使用 Map 而不是普通数组,使用键作为客户端 ID,值作为用户名。删除用户名将是一个简单的调用,例如 map.remove(clientID);

按您的要求进行编辑:请注意,此代码并不完整,并且仅与您提供的内容相同。

公共(public)类AddClient实现Runnable { 线程t;

private Map<int, String> users = new HashMap <int, String>();

AddClient(String tot) {
t = new Thread(this, tot);
t.start();
}

public void run() {
while (true) {
try {
try {
waitClient();
} catch (Exception ex) {
ex.printStackTrace();

}

int clientId = users.size() + 1;
users.put(clientId, cm.sender);


//set stream to send and receive data
out[clientId] = new ObjectOutputStream(connect.getOutputStream());
out[clientId].flush();
in[clientId] = new ObjectInputStream(connect.getInputStream());

删除用户方法

公共(public)同步无效removeUser(int number){

            if(users.containsKey(number)) {
System.out.println("Server removing user " + users.get(number) + "which is client " + number);
users.remove(number);
} else {
System.out.println("User not in session");
}
}

关于java - 从聊天中删除用户 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com