Server push chat room instance based on tomcat7, Java and websocket

preface

HTML5 websocket realizes the two-way communication between the server and the browser. The two-way communication makes the development of server message push easier. The most common applications are instant messaging and applications that require high real-time information. Most of the previous server message push technologies used "polling" and "long connection" technologies, both of which will cause considerable overhead to the server, and the real-time performance is not particularly high. Websocket technology will only produce very little overhead for users, and the real-time performance is particularly high. Let's begin to explain how to use websocket technology to develop chat rooms. In this example, tomcat7 server is used. Each server has different implementation of websocket, so this example can only run in Tomcat server. However, spring has launched websocket API, which is compatible with the implementation of each server. You can refer to relevant materials for understanding. It will not be introduced here, The following figure shows the effect of the chat room:

In this example, the real-time push of messages and the online and offline notification of chat users are realized. Let's start to explain how to implement it.

Background processing

Tomcat mainly relies on org. Com to implement websocket apache. catalina. websocket. Messageinbound is a class whose name is in {tomcat_home} / lib / Catalina Jar, so when you develop, you need to use Catalina Jar and Tomcat coyote Jar is introduced. The following code is the servlet exposed to the client connection address:

This servlet is different from ordinary servlets. It inherits the websocketservlet class and overrides the createwebsocketinbound method. In this class, the user attribute in session is the user entering index JSP, which records the nickname of the current user. The following is the code of the websocket connection object class websocketmessageinbound class implemented by ourselves:

The in the code mainly implements onopen, onclose and ontextmessage methods to process users' online, offline and sending messages respectively. There is a websocketmessageinboundpool connection pool class in this class. This class is used to manage the connections of current online users. The following is the code of this class:

Front desk display

The above code is the code of the background of the chat room, which is mainly composed of three objects: servlet, connection object and connection pool. The following is the code of the front desk. The code of the front desk is mainly to connect with the server and display the user list and information list. The front desk display uses the EXT framework. Students who are not familiar with ext can have a preliminary understanding of ext. the following is index Jsp code:

The display of the page is mainly in websocket JS. The following is websocket JSD Code:

This code mainly implements the container for displaying messages. The following is the code to be executed after the page is loaded:

The above code is the code that automatically connects to the server after the page is loaded and creates the display interface.

be careful

Two points to note: after the deployment is completed, you need to add Catalina. Net to the Lib directory in the Tomcat application directory Jar and Tomcat coyote Delete the jar. For example, the Lib directory of the project is D: \ workspace \ websocket \ Webroot \ WEB-INF \ lib, while the Lib directory of the deployed application is D: \ tools \ apache-tomcat-7.0 32 \ webapps \ websocket \ WEB-INF \ lib, delete the Lib directory of the deployment directory and connect two jars, otherwise the package could not initialize class com ibcio. Websocketmessageservlet error, remember.

If the connection still cannot be established, please download the latest Tomcat. Forget that the Tomcat createwebsocketinbound of that version does not have the request parameter. Now this code has this parameter, 7.0 3xx versions take this parameter. Remember.

summary

Using websocket to develop server push is very convenient. This is a simple application. In fact, it can also be combined with webrtc to realize video chat and voice chat.

Instance Download

Download address: Demo

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>