Question

For the past 2 weeks, I have been working on a java group chat application. So far a user can login and join groups created by other users. The user can also send chat messages to each other inside of each group. The message distribution is handled by a server and the server only sends the chat messages to clients of the group from which it was sent. A list of groups which the user is a part of is also displayed, and I would like for there to be a small green mark(or anything else to notify the user) to let the user know if there are other users currently active on that chat room group.

Here is how my groups are stored in the database

**TABLE groups**

 - id
 - user_count

**TABLE group_members**

 - groupid
 - userid

The sending and receiving of chat messages are handled using sockets, and none of the sent messages are stored on the database. I was thinking of maybe having a field in the groups table where every time a user joins would increment the value in it by 1 and every time a user leaves, the value goes down by 1. This way if the number stored in this field is 0, there are no current users on here. I think there may be a better way to handle this though.

Was it helpful?

Solution 2

I just went with my idea as stated in the question. Works well and fast enough for my needs. heres how the database looks TABLE groups

  • id
  • user_count
  • activity <--- this is where the values are increased or decresed.

TABLE group_members

  • groupid
  • userid

OTHER TIPS

You could have an arraylist for every group that holds the names of every player in that group. Then you could easily get the names of people in the group and the number of people in it. so if you have a class named Group you could have something like:

class Group {
    private ArrayList<String> users; // or instead of String if you have a User class use that

    public Group(Type par) {
        users = new ArrayList<String>();
    }

    public static void addUser(String userName) {
        users.add(userName);
            updateUsers();
    }

    public static void removeUser(String userName) {
        for(int i = 0; i < users.size(); i++) {
            if(users.get(i).equalsIgnoreCase(userName)) {
                users.remove(i);
            }
    }
            updateUsers();
    }

    public static int getNumberOfUsers() {
        return users.size();
    }

    public static void updateUsers() {
        for(String e : users) {
            // send the user the necessary info to all users in the group
        }
    }

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top