Detailed introduction to Tomcat implementation of websocket
How Tomcat implements websocket
Websocket protocol belongs to the HTML5 standard. More and more browsers have natively supported websocket, which can realize two-way communication between client and server. After a websocket connection is established between the client and the server, the server-side message can be sent directly to the client, which breaks the traditional request response mode and avoids meaningless requests. For example, the traditional method may use ajax to constantly request the server, while websocket can directly send data to the client without the client having to request. At the same time, with the native support of the browser, it is more convenient to write client applications without relying on third-party plug-ins. In addition, websocket protocol discards the cumbersome request header of HTTP protocol, but transmits it in the form of data frame, which is more efficient.
The figure shows the process of websocket protocol communication. First, the client will send a handshake package to tell the server that I want to upgrade to websocket. I don't know whether your server agrees or not. At this time, if the server supports websocket protocol, a handshake package will be returned to tell the client that there is no problem and the upgrade has been confirmed. Then a websocket connection is successfully established. The connection supports two-way communication and sends messages in the data frame format of websocket protocol.
The handshake process needs to be explained. In order to make the websocket protocol compatible with the existing HTTP protocol web architecture, the handshake of the websocket protocol should be based on the HTTP protocol. For example, the client will send the following HTTP message to the server to request upgrading to the websocket protocol. The upgrade: websocket is to tell the server that I want to upgrade the protocol:
At this time, if the server supports the websocket protocol, it will send a message agreeing to the client's upgrade protocol. The specific message is similar to the following. Upgrade: websocket tells the client that I agree to your upgrade protocol:
After the above handshake is completed, the HTTP protocol connection is broken, and then the websocket protocol is used for communication between the two sides. This connection is still the original TCP / IP connection, and the port is also the original 80 or 443.
Here is a simple example of writing WebSockets in Tomcat:
This servlet must inherit websocketservlet, and then create a websocketmessageinbound class that inherits messageinbound. Fill the class with methods such as onclose, onopen, onbinarymessage and ontextmessage to complete the logic of each event. Onopen will be called when a websocket connection is established, and onclose will be called when a websocket is closed, Onbinarymessage is called when the client data is received in binary mode, and ontextmessage is called when the client data is received in text mode. The above code implements a broadcast effect.
According to the above processing logic, Tomcat's integration of websocket will not be too difficult, that is, if a websocket protocol request is encountered when processing the request, special processing will be carried out, the connection will be maintained, and the onclose, onopen, onbinarymessage, ontextmessage and other methods of messageinbound of websocketservlet will be called at an appropriate time. Since websocket is generally recommended to be used in NiO mode, take a look at NiO mode to integrate websocket protocol.
As shown in the figure, after the websocket client connection is received by the receiver and registered in the niochannel queue, the poller component continues to take turns to check whether there is a niochannel to be processed. If so, it enters the servlet that inherits the websocketservlet through the processing pipeline. The doget method of websocketservlet will process the websocket handshake and tell the returned client to agree to the upgrade agreement. In the future, poller will continue to take turns off the relevant niochannel. Once it is found that the pipeline uses the websocket protocol, it will call the relevant methods of messageinbound to complete the processing of different events, so as to support the websocket protocol.
Thank you for reading, hope to help you, thank you for your support to this site!