Chat System - Server Commands & Kicking Clients
Submitted by GeePee on Friday, March 13, 2015 - 21:19.
Introduction:
This tutorial is the fifteenth in my Java Network Programming using KryoNet series, or tenth in creating a chat client and server system, in which we are going to be adding a feature to the server where the user in control of the server is able to kick specific clients out of the chat room.
Previous:
In the previous tutorial we finished making the ChatGUI work along with adding the functionality of commands.
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 we are going to add commands to the server, similarly to how we did for the ChatGUI, and creating one command to make the server kick a client out.
The Server Input Loop:
So before we can take commands in to the server console - we might make it a GUI later on in the series - we need to create a loop where the user has the option to enter a command...
As you can see, the above method askCommands gives the instruction that commands can be entered at any time. Then constantly loops waiting for a new entry to the console made by the user (scanner.nextLine()).
It then checks if the entry starts with a forward slash (/), if it does it checks if the command exists and performs it, otherwise it gives instructions again.
To focus on the kick command, it takes one parameter which we can by splitting the command by a space and putting it in to a string array (what I've named args). If it has the right amount of args for the command kick (one, the username for the client to kick out of the chat room) it runs a new method in our ClientHandler (kickClient) which accepts the username parameter.
So now we need to make the ClientHandler kickClient method...
One final feature we can add is a simple message to every other client to notify them that someone has been kicked out of the chat room. Just before we return from the kickClient method, we want to add a new PacketMessage and send it to each of the clients...
- private void askCommands() {
- while (true) {
- if (user.startsWith("/")) {
- if (user.equalsIgnoreCase("/help")) {
- }else if(user.toLowerCase().startsWith("/kick")) {
- if (args.length == 2) {
- clientHandler.kickClient(args[1]);
- }else
- }
- }else
- System.out.println("To enter a command, it must start with a forward slash (/). Type /help for a list.");
- }
- }
Again, it loops through each clients, compares the usernames and if they are the same and the current iterating client is the one to be kicked, it gets their connection and closes it.
Finally we just need to call the initial looping method from underneath our server listener...
- askCommands();
- Packet2Message mes = new Packet2Message();
- mes.message = "[SERVER] " + username + " Kicked by Server Controller.";
- for (CustomClient cc : this.clients) {
- cc.getConnection().sendTCP(mes);
- }
Add new comment
- 69 views