Chat System - Admin Command Kick - Server Side
Submitted by GeePee on Monday, March 16, 2015 - 00:27.
Introduction:
This tutorial is the nineteenth in my Java Network Programming using KryoNet series, or fourteenth in creating a chat client and server system, in which we are going to be continuing the previous tutorial.
Previous:
In the previous tutorial we added private message features to the client.
The System:
We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat.
When a client connects, add them to a list.
Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation.
This Tutorial:
This tutorial is going to be making the admin requests from the previous tutorial handle on the server.
Listener:
First we need to handle the packet within the server listener. We have two options here, either we can send the request to all admins, or one randomly. I have made both for this tutorial, you may choose one or use both...
sendToAdmin Method:
So first lets make the sendToAdmin method in our ClientHandler class. This method will simply randomly select an admin, compile a Packet2Message packet from the information given in the parsed Packet7AdminLKick packet, and send it to the randomly selected admin.
So as you can see, the method iterates through all the clients adding the admins to a new ArrayList, then it randomly selects one of them and sets them as the value of the CustomClient variable 'admin'.
Then it creates the Packet2Message and sends it to the admin.
sendToAllAdmins Method:
This method is very similar to the sendToAdmin method except it doesn't randomly select just one single admin, it simply iterates through the currently connected clients list, and sends a new Packet2Message poacket to each of the clients who have the level of 2 (admin).
convertReason Method:
Finally we need to write the convertReason method. This method has the same reasons String array as the one in the client side application. It simply returns the option indexed at the parsed reason. If the reason does not exist (there are not enough elements in the array, we return 'Unfound.')...
Then within the two other clients, type a few requests, change the global variable from false to true and vice versa and see if the right people receive the requests and global messages.
Example...
- else if(object instanceof Packet7AdminKick) {
- Packet7AdminKick packet = (Packet7AdminKick) object;
- clientHandler.sendToAdmin(packet);
- clientHandler.sendToAllAdmins(packet);
- if (packet.broadcast){
- Packet2Message p2 = new Packet2Message();
- p2.message = (packet.userFrom + " has requested to kick " + packet.userKick + " for " + clientHandler.convertReason(packet.reason));
- clientHandler.circulateMessage(connection, p2);
- }
- }
- public void sendToAdmin(Packet7AdminKick packet) {
- CustomClient admin;
- ArrayList<CustomClient> admins = new ArrayList<CustomClient>();
- for (CustomClient c : this.clients) {
- if (c.getLevel() == 2) //isAdmin
- admins.add(c);
- }
- if (admins.size() > 0) {
- admin = admins.get(random.nextInt(admins.size()));
- Packet2Message p2 = new Packet2Message();
- if (packet.broadcast)
- globality = "This message has been publicly broadcast within the chat.";
- p2.message = "You've received a kick request from " + packet.userFrom + " to kick " + packet.userKick + ". Reason: " + convertReason(packet.reason) + ". " + globality;
- admin.getConnection().sendTCP(p2);
- }else
- }
- public void sendToAllAdmins(Packet7AdminKick packet) {
- for (CustomClient c : this.clients) {
- if (c.getLevel() == 2) { //isAdmin
- Packet2Message p2 = new Packet2Message();
- if (packet.broadcast)
- globality = "This message has been publicly broadcast within the chat.";
- p2.message = "You've received a kick request from " + packet.userFrom + " to kick " + packet.userKick + ". Reason: " + convertReason(packet.reason) + ". " + globality;
- c.getConnection().sendTCP(p2);
- }
- }
- }
Test:
You can test this by opening one server, four clients, make two clients admins by typing:
- /admin {username}
- /admin kick admin2 4 true
Add new comment
- 79 views