Java Internet live chat system (with source code)

Java Internet live chat system (with source code)

0. Preface

Decided to Netty as the core, WebSocket as the application layer communication protocol to make an Internet chat system, as a whole just like the WeChat web version, but considering that the chat system has many functions, so it is only intended to implement the core chat functions, including single hair, group sending, file sending, and then integrate  the project with Spring into an open source, expandable way, for everyone’s reference, discussion, use, welcome everyone’s guidance. touch panel screen

About Netty

 

Netty is a client/server framework that takes advantage of the advanced networking capabilities of Java, hiding the complexities behind it and providing an easy-to-use API.

About the WebSocket communication protocol

WebSocket is to solve the HTTP protocol in the communication can only be initiated by the client this drawback and appear, WebSocket based on the HTTP5 protocol, borrowing HTTP for handshake, upgrade, can be lightweight, efficient, two-way transmission of text data between the client and the server.

1. Technical preparation
IDE:MyEclipse 2016

JDK version: 1.8.0_121

Browser: Google Chrome, 360 browser (speed mode) (involving web front-end design, back-end development is very boring)

Technology involved:
Netty 4

WebSocket + HTTP

Spring MVC + Spring

JQuery

Bootstrap 3 + Bootstrap-fileinput

Maven 3.5

Tomcat 8.0

2. General Description

2.1 Design Thinking
The entire communication system is run with Tomcat as the core server, and another thread is opened to run the Netty WebSocket server, the Tomcat server mainly handles HTTP type requests (usually business types) for customer login, personal information management, etc., the port is 8080, and the Netty WebSockt server mainly handles WebSocket type requests for user message communication, and the port is 3333. After the user logs in through the browser, the browser will maintain a Session object (valid for 30 minutes) to maintain the login state, the Tomcat server will return the user’s personal information, while recording the online user, according to the user ID to establish a WebSocket connection and save it on the backend for real-time communication. When a user initiates communication to another user, the server will find the saved WebSocket connection according to the user ID of the intercom in the message content, and send a message through the connection, and the other party can receive the message immediately. When the user logs off or logs out, the WebSocket connection is released, clearing the login state in the Session object. (Insert an advertisement: you need to open the genuine IDEA can contact me, 56 yuan a year, genuine authorization activation, the official website can check the validity period, if necessary, add me WeChat: poxiaozhiai6, remarks: 914.) )

In fact, Netty can also be used as an HTTP server, and here Spring MVC is used to handle HTTP requests for familiarity and is closer to the traditional way of development.

2.2 System structure
The system adopts B/S (Browser/Server), that is, the structure of the browser/server, and the main transaction logic is implemented on the server side (Server). Drawing on the idea of MVC mode, from top to bottom, it is specifically divided into view layer (View), control layer (Controller), service layer (Service), model layer (Model), data access layer (Data Access)

2.3 Project structure
Project back-end structure:
Java Internet live chat system (with source code)

2.4 System function modules

The system consists of only two modules: the login module and the chat management module.

Login module: Since it is a system, then the role authentication of the login is essential, here to use a simple, traditional Session to maintain the login state, of course, there is a corresponding logout function, but the logout here in addition to emptying the Session object, but also to release the WebSocket connection, otherwise it will cause memory leakage.

Chat management module: the core module of the system, this part is mainly implemented using the Netty framework, the functions include single and multiple messages of information, files, and also support emoticon sending.

Other modules: such as friend management module, chat record management, registration module, etc., I did not implement, if you are interested, you can implement it yourself, similar to the traditional development method.

Since this system involves multiple user states, it is necessary to explain, and the user state transition diagram of this system is given below.
Java Internet live chat system (with source code)

2.5 System Interface

The system chat interface is as follows:

Java Internet live chat system (with source code)

Java Internet live chat system (with source code)

3. Core coding

Here is only to explain the need to pay attention to the place, please see the source code for details

3.1 Netty Server Startup and Shutdown
When shutting down the Tomcat server, it is also necessary to free Netty-related resources, otherwise it will cause a memory leak, close the method such as close() below, if you only use the shutdownGracefully() method, the memory leak Memory Leak exception will be reported when shutting down (but the IDE may not be able to output to the console)
/**
* 描述: Netty WebSocket服务器
* 使用独立的线程启动
* @author Kanarien
* @version 1.0
* @date 2018年5月18日 上午11:22:51
*/
public class WebSocketServer implements Runnable{

/**
* 描述:启动Netty Websocket服务器
*/
public void build() {
// 略,详细请看源码
}

/**
* 描述:关闭Netty Websocket服务器,主要是释放连接
* 连接包括:服务器连接serverChannel,
* 客户端TCP处理连接bossGroup,
* 客户端I/O操作连接workerGroup
*
* 若只使用
* bossGroupFuture = bossGroup.shutdownGracefully();
* workerGroupFuture = workerGroup.shutdownGracefully();
* 会造成内存泄漏。
*/
public void close(){
serverChannelFuture.channel().close();
Future<?> bossGroupFuture = bossGroup.shutdownGracefully();
Future<?> workerGroupFuture = workerGroup.shutdownGracefully();

try {
bossGroupFuture.await();
workerGroupFuture.await();
} catch (InterruptedException ignore) {
ignore.printStackTrace();
}
}

}

4. Effect and operation demonstration

4.1 Login Operations
The login entry is: http://localhost:8080/WebSocket/login or http://localhost:8080/WebSocket/ The current system users are fixed to 9, and the group is 1, including 9 users.

User1 Username: Member001 Password: 001

User2 Username: Member002 Password: 002

······

User9 Username: Member009 Password: 009

4.2 Chat demo

Java Internet live chat system (with source code)