|
|
Chat Room
This applet will let you run your own private chat room. It consists
of two parts: the chat server and the chat client. The chat server must
run on the web server that is hosting the web site. This means you
will have to be able to telnet or ssh to your web server in order to
start it. You will also need an ISP that doesn't mind you running
server processes. The client part is what will run in the web browser
on the users machines.
Some of its features: shows list of people in the room,has a room
topic,plays a sound when someone enters or leaves, comes with the
source
code.
My web site hoster won't let me run a server process so I can't show
you a live demonstration but here are some screen
shots.
Starting The Chat Server: (on
a
linux machine)
Telnet or ssh to the machine that has your web site and enter this
command.
$ java ChatServer.class
It generates a log
of actions
to standard output.
You may want to send it to a log file. The & character makes it
run it the background.
$ java ChatServer.class > chat.log &
The Chat Client:
Put this code on the web page where you want the chat room.
<applet code=ChatClient.class width=600 height=300>
<param name=host value="www.yourdomain.com">
<param name=port value=1123>
</applet>
What they mean:
host - Name or IP of the computer that the chat server is running on. port - TCP port that the chat server is running on. This is set in ChatServerThread.java
Commands:
| /MESS some action |
Used to tell others something happened with out saying it.
Example: Jon moves next to Robin |
| /OPER alias |
Changes that user to an operator. Requires a password.
Password is "secret". You can change it in ChatClient.java. Operators
will have a @ beside the name. Certain commands like TOPIC,KICK can
only be executed by an operator. |
| /TOPIC some topic |
Changes the topic of the chat room. |
| /KICK alias |
Kicks a user out. |
| /STOPSERVER |
Stops the chat server. |
Download The Applet
Download chatroom.zip
The Basic Theory
Here is a general overview of how it all works. Basically the clients send a message to the server and the server sends the message to all of the other clients.
By the way I can't take credit for all of this. This is all based on a simple chat project in a book called "The Black Art of Java Game Programming".
ChatServer \ +----------+ ChatServerThread------------+ Internet +----- ChatClient | +---+ | sClientGroup | +---+------+\ | \ | | \ sClientThread sClientThread | ChatClient +-------------------------+
ChatServer is just used to launch a thread. ChatServerThread creates sClientGroup then listens on the tcp port for new connections from the chatclients. When a new connection occurs, ChatServerThread tells sClientGroup to create a sClientThread. So there will be a sClientThread for each ChatClient. The socket connection is transferred to the sClientThread, so all communication from the ChatClient goes to its sClientThread. The ChatClient sends a message to sClientThread which then sends it to sClientGroup. sClientGroup will then send an appropiate message/command to all of the other sClientThread's which in turn relay the data back the ChatClient. Sometimes an action might cause multiple messages. For instance, when someone enters the room:
1. ChatClient sends a "login" command.
2. sClientGroup gets it and checks to make the alias is not used already.
3. sClientGroup sends back an "OK" to the sClientThread which send it to the ChatClient.
4. sClientGroup sends an "topic" command to the sClientThread which send it to the ChatClient.
5. sClientGroup sends a "userin" to all of the sClientThread which send it to the ChatClient. The ChatClient will then make a sound.
6. sClientGroup sends a "list" of users to all of the sClientThread which send it to the ChatClient.
Whew.
The actual chatting is easy.
1. ChatClient sends a "say" command.
2. sClientGroup gets it and sends the "say" to all of the sClientThread which send it to the ChatClient.
One interesting idea I have is what if you sent binary data around instead of text messages, such as audio data? This basic theory might be used to create a voice over ip application. You would have to send things in packets and there would be timing issues to deal with. Also, I'm not sure if java would work for the ChatClient. Java can play a sound but I don't know if you can get a sound straight from an audio souce like a microphone. An activex control would resolve that problem.
|