ECHAT chat server implemented with Java
James @ www.chenshen.com
Summary: Unlike the general ASP chat room, this chat server is a completely independent server program. When it runs, the port is listened to, once the user is accessed through the browser, the simulation web server communicates via HTTP and user. Since Java technology is used, security and speed are obvious than a general ASP chat room advantage.
The technical features of the ECHAT chat server are listed below:
1) Cross-platform: can be stably run under Liunx
2) Multi-thread: improve the overall performance of chat servers
3) Server Push: Make customers and servers uninterrupted, realize "no refresh" chat
First, the main description
Before introducing the specific model, I have to explain the few main classes involved:
1) Chat Server
The main thread of the entire program, by it controls the beginning of other threads
2) TextChatServer
Text chat thread, you can expand voice chat, etc.
3) Room
Room category, saved all and room-related information (including room name, room port, list of users, etc.)
4) RoomlistentHread:
Room monitor thread, accept all customers connect this room Socket
5) SYSMSGTHREAD
System message sending threads, through it to room broadcast system messages (login information or advertisements)
6) ClientThread
Customer connection thread, each user will build a socket connection with the server
7) ClientManageThread
Customer tracking threads are used to monitor customers' connection conditions (because read methods cause blocking, you must assign a separate thread for each user)
8) User
User class, save the login user
9) Actions
I defined 7 anctions, this class is a specific implementation (specific to the third chapter)
10) config.xml
System configuration file, including detailed configuration of each room (example below)
Room Name>
Administrator>
Discussion topic>
Port>
Number of people>
Room>
Configuration Information>
Second, thread model
The thread model is designed as follows:
The process of running the program is as follows:
1) Chat server main thread read information from all rooms from config.xml
2) Establish a Room object according to the information in the configuration file, and transfer the information to the room object.
3) Establish a room listening thread and system message thread according to the port attribute in the ROOM object
4) When the user connects to the server, the room listens thread accepts and the client establishes a socket connection and establishes a client connection thread (each user has a separate connection thread).
5) Then establish a user tracking thread to each user to determine whether it is online (see Chapter 5)
Such a complete thread model is established, and the client is communicating via Socket and the server.
Third, the interaction between the Action model client and the server is more complicated. Here I define 7 Actions and give a specific implementation in the Actions class. The definition of Actions is as follows:
1) INDEX: Handling INDEX Action [Login Area]
2) Main: Treatment Main Action [Framework Area]
3) Talk: Treat Talk Action [Enter Area]
4) List: Handling List Action [User Line List]
5) Login: Verify the user and log in
6) Send: Processing Send Request
7) Outinfo: Handling Outinfo Action
Each Actions corresponds to the request page as follows:
The communication between the client and the server is based on the HTTP protocol, and the client issues a connection request. The server returns an HTTP message header. Then, the client will issue a GET / POST request. The server receives the parameters of the client to respond according to the different Actions and determine if the Socket connection is disconnected in the action. The code segment is as follows:
// Return to parames Params
Httpparser Requestparam = new httpparser (_clientsocket);
Hashmap _param = requestparam.getparams ();
// Create an ActionS object
Actions action = new actions (_out, _param, _room);
IF (RequestParam.Action! = null) {
//System.out.println ("
Acton
: " RequestParam.Action);
IF (RequestParam.Action.equals (")) {
Action.index ();
} else if (RequestParam.Action.equals ("main")) {
Action.main ();
} else if (RequestParam.Action.equals ("login")) {
Action.login ();
} else if (RequestParam.Action.equals ("talk")) {
Action.talk ();
} else if (RequestParam.Action.equals ("send")) {
Action.send ();
} else if (RequestParam.Action.equals ("chat")) {
Action.chat ();
} else if (RequestParam.Action.equals ("List")) {
Action.list ();
} else if (RequestParam.Action.equals ("outinfo")) {
// Add this continuous connection to the User object
Action.outInfo (_ClientSocket);
Return; // constantly open
} else {
// Unknown Action transfer to Index
Action.index ();
}
}
_out.flush ();
//Disconnect
_clientsocket.close ();
This is a complete action processing model
Fourth, Server Push Technology
It should be noted: I implemented chat server is to use Server Push technology to ensure "no refresh" chat. What is this technique from Linux? I am briefly explained:
The basic principle of the chat room chat room of Server Push Technology is that the HTTP server program is not used, and the 80-port of the server is listened by its own socket program. After the HTML specification, after receiving the browser, the response of the WWW server will be chatted. The content is sent back to the browser. In the browser, it is always on the page reception status like browsing a huge page. That is, we no longer use CGI and other ways to process the contents of chat, and use our own programs to handle all transactions. In fact it is a specialized chat server. Said so much, in fact, the core is: the user requesting the web page of the web page is always open, just like a huge page is always accepted so that the server can send chat information to the client uninterrupted. Let's take a look at my specific implementation.
I have processed server push in my ActionS model. Please note:
} else if (RequestParam.Action.equals ("outinfo")) {
// Add this continuous connection to the User object
Action.outInfo (_ClientSocket);
Return; // constantly open
I have return before turning off the socket connection, so the requested Socket always remains acceptable. I can continue to write information from the source, which is an application of Server Push technology.
5. Judging the user disconnection
It is a more tricky problem to judge whether the user is short. (I have not found a better way, I can only use old earth methods)
When the user is close to the browser or turn the page to other places, the client and the server keeps the connected socket actually not broken, but cannot be written. So you can judge the following code:
Try {
BufferedInputStream_IN;
_IN = New BufferedInputStream (ClientSocket.getInputStream ());
// r 方法 方法 causes blocking, so you must assign a separate thread for each user (affect efficiency)
_IN.READ ();
} catch (ioexception e) {
DELUSER (ClientSocket);
}
When the read method occurs an exception, you can capture and then delete the user. The headache is the read () method is to block (can be solved with java.nio) If you use a thread to manage it is obviously unsuitable, I have a tracking thread to each user thread, you have to block it. Don't affect others ~~
Sixth, end
Here I wrote it relatively simple, if you are interested, you can go to my website and communicate with me.
The source code of the chat server can also be downloaded at http://www.chenshen.com.
About the Author:
Shen Yachang, James, Senior Programmer, SCJP
Www.chenshen.com
James@njut.edu.cn
July 3, 2003