1: preface
Recently I am learning network knowledge. Netty is a very important framework for Java development. Whether it is for interview or daily work, for example, RPC framework Dubbo actually uses Netty at the bottom, or our chat communication function, Netty may be involved. As Java developers we should master it. This project is a chat system with Netty as the core and Websocket as the communication protocol. The overall function is modeled after the computer version of wechat, but the computer version of wechat has many functions, so only some functions are realized, such as: User login (OAUTH2 authentication), view my friends list, single chat, create group chat, group message send, delete chat, top chat, emoticon function send, file send, heartbeat and idle detection. Git project address: github.com/holiday-jq/…
2: Technical route
- Front end: Webpack + Vue + less + Element UI
- Back end: SpringBoot framework + Netty network programming framework + Spring-security-OAuth2 + jdbcTemplate
- Database: mysql
- Protocol: the websocket
What is Spring Boot?
Spring Boot is a new framework from the Pivotal team designed to simplify the initial setup and development process for new Spring applications. The framework uses a specific way to configure so that developers no longer need to define boilerplate configurations. In my opinion, Spring Boot is not a new framework. It has many frameworks configured by default, just like Maven integrates all jars and Spring Boot integrates all frameworks.
What is Netty?
Netty is a client/server framework that leverages Java’s advanced networking capabilities to provide an easy-to-use API while hiding the complexity behind it. Netty is a widely used Java network programming framework (Netty won Duke’s Choice Award in 2011, see www.java.net/dukeschoice… Facebook and Instagram, as well as popular open source projects such as Infinispan, HornetQ, vert. x, Apache Cassandra and Elasticsearch, take advantage of its powerful core code for web abstraction.
About WebSocket communication protocol
WebSocket is to solve the problem that HTTP communication can only be initiated by the client. WebSocket is based on HTTP5 protocol and uses HTTP to shake hands and upgrade. Can achieve lightweight, efficient, bidirectional text data transmission between the client and the server.
- How does WebSocket work? Why can persistent connection be implemented?
3: Environment construction
Front end: Build the operating environment: first install node. js installation link nodejs.org/zh-cn/ and then run CMD to enter the console to verify whether the installation is successful: Use VsCode(or whatever editor you prefer) to go to the root directory of your project and first run NPM install to install dependencies. NPM Run Serve then starts a Node service to access front-end resources
Back end: Install and configure Maven, configure maven in the development editor (such as Eclipse, Intellij idea), and then start the SpringBoot project
4. Display of specific functions
- User login:
Here, the authorization mode of Oauth2 is password mode. After the authentication succeeds, the user returns the token. Then, if the user accesses the interface, the token access is required because the interface resources are protected, otherwise, error 401 will be displayed
- Check out my friends list:
- Single chat:
- Create group chat and send group messages:
If the number of selectors is greater than 1, a group chat is created. If the number of selectors is 1, a single chat is created. Note: In the group chat pictures here, I don’t know how to combine the group members’ avatars with one picture as the group chat’s avatars, just like how wechat creates group chats, so I randomly find a picture as the group chat’s avatars, so no matter how many groups are created, the group chat’s avatars are the same.
- Right mouse popup delete chat and top chat:
Note: the interaction between the top and the untop is not well written here. If you add a new chat, the chat with the top can not always be in the front, to be optimized. !!!!!
- Emoticons send:
- File sending and downloading:
- Heartbeat and idle detection:
Netty’s IdleStateHandler heartbeat mechanism is used to check whether the remote end is alive. If the remote end is not alive or active, idle Socket connections are processed to avoid wasting resources. There is a timer on the front that sends a heartbeat packet to the Netty every 30 seconds. If the detection mechanism is triggered, the channelIdle method is executed, where you can close the channel connection.
- If there is no idle detection, a connection may be suspended, causing the following problems:
- 1: For the server, because each connection consumes CPU and memory resources (when the server accPT successfully returns a file descriptor, Linux all files), a large number of dead connections will gradually deplete the server’s resources, resulting in a gradual decline in performance.
- 2: For the client: The connection is dead, sending data times out, affecting user experience.
5: Summarize and expand
Because the function of the chat software is very much, it is not possible all functions are implemented (too difficult – _ -), there are also a lot of optimization and didn’t realize the function, such as chatting, can increase the return of function, and the people into the group of functions, on the server is nothing more than to add and remove ChannelGroup front-end interactive effect is a little complicated, Also chat text input box can be implemented with rich text editor, this project is to use the textarea tag (sina blog webpage version of the chat is also used textarea ha ha), and then chat records and lists how to do persistent, scrolling load previous chat records, this project did not do (limited ability – _ -), So when you refresh the screen, you can’t see the previous chat history.
6: Shoulders of giants
- Netty entry and actual combat: imitated writing wechat IM instant messaging system
- Netty core Principle analysis and RPC practice