首页 > 解决方案 > 无法使私有广播方法起作用

问题描述

我在 github 上为一个学校项目修改了一个开源项目以满足我的需要,它有一个broadcast()发送消息的方法,并且run()在 while 循环中的方法中调用它,但问题是向我想要broadcast()的所有用户发送消息userList<>添加通过写入向其中一位用户发送私人消息的功能@username

以下是广播方法的代码:

private synchronized void broadcast(String msg) {
    for (int i = 0; i < clientList.size(); i++) {
        clientList.get(i).write(msg);
    }
    System.out.println("Log: Message broadcast --> " + msg);
}

这是 run() 方法

public void run() {
    System.out.println("Log: Got input/output streams for connected client.");

    /** Get the first message from the client, attempt communication */
    String clientMsg = null;
    boolean accepted = false;

    /** Allow client to create an account, login, or quit */
    do {
        clientMsg = client.read();
        if (clientMsg.equals("QUIT")) {
            System.out.println("Log: Client disconnected without signing in.");
            client.disconnect();
            return;
        }
        else if (clientMsg.startsWith("NEWUSER: ")) {
            createUser(clientMsg);
        }
        else if (clientMsg.startsWith("LOGIN: ")) {
            accepted = authenticate(clientMsg);
        }
        else
        {
            System.out.println("Log: Unexpected client message -> " + clientMsg);
            client.disconnect();
            return;
        }
    } while(!accepted);

    /** Run main chat loop. Will read from the client, and broadcast each read
     *  until the client disconnects. */
    while (true) {
                int i=0;
                String username= clientList.get(i).getUsername();
        String line = client.read();
        if (line == null) break;
                    else if(line.startsWith("@"+username)){
                     broadcastp(line,username);
                    }
        else {

                        broadcast(line);

                    }
i++;
    }

    /** The only way for the client to exit the above loop is to disconnect.
     *  Therefore, call the handler's exit routine */
    exit();
}

这是broadcastp()我尝试实现此功能的方法,但它不起作用。尽管没有私人聊天功能,但它可以完美地编译和运行。

  private synchronized void broadcastp(String msg,String username) {
             for (int i = 0; i < clientList.size(); i++) {
        username = clientList.get(i).getUsername();
        if(msg.startsWith("@"+username))
        {clientList.get(i).write(msg);}
        else {
        continue;
        }}
    System.out.println("Log: Message broadcast --> " + msg);}

标签: javamysqlsocketsservertcp-ip

解决方案


我不了解您的程序如何工作的全貌,但您说程序运行完美,但不执行私人消息传递部分。

如果我查看您的代码,在while循环中,您总是从clientList( i = 0) 中获取第一个用户名,并且仅broadcastp在该行以该名称开头时才调用。

首先..broadcastp曾经被调用过吗?在broadcastp你有另一个循环,但它总是匹配i == 0给定你调用它的方式(使用循环中的行和用户名while)。

问题似乎就在那里。所以在 while 循环中这样的事情可能对你有用(删除i变量,不需要 for broadcastp):

boolean isPrivate = false;
String line = client.read();

for (User user : clientList) {
    if (line.startsWith("@" + user.getUsername())) {
        user.write(line);
        isPrivate = true;
        break;
    }
}

if (!isPrivate) {
    broadcast(line);
}

推荐阅读