Detailed explanation of websocket + spring sample demo (sockjs library has been used)

1. Introduction

As the next generation web standard, HTML5 has many remarkable new features, such as canvas, local storage, multimedia programming interface, websocket and so on. Among them, websocket, known as "TCP of the web", has attracted the attention of developers. The emergence of websocket makes it possible for browsers to provide socket support, so as to provide a two-way channel based on TCP connection between browsers and servers. Web developers can easily use websocket to build real-time web applications. Since then, developers have a powerful weapon in their hands.

The information interaction process of web applications is usually that the client sends a request through the browser, the server receives and reviews the request, processes it and returns the results to the client, and then the client browser presents the information. This mechanism can be safe for applications with less frequent information changes, but for applications with high real-time requirements, For example, online games, online securities, device monitoring, news online broadcasting, RSS subscription push, etc. when the client browser is ready to present these information, these information may be outdated on the server side.

Therefore, keeping the information synchronization between client and server is the key element of real-time web applications, and it is also a difficult problem for web developers. Before the websocket specification came out, developers had to adopt some compromise schemes to realize these real-time web applications. Among them, the most commonly used are polling and comet technology. Comet technology is actually an improvement of polling technology, which can be subdivided into two implementation methods, one is long polling mechanism, and the other is called streaming technology.

The purpose of HTML5 websocket is to replace polling and comet technology, so that the client browser has the real-time communication ability of desktop system under C / S architecture. The browser sends a request to the server to establish a websocket connection through JavaScript. After the connection is established, the client and server can exchange data directly through TCP connection. Because websocket connection is essentially a TCP connection, it has great performance advantages over polling and comet technologies in terms of data transmission stability and data transmission volume.

However, in view of the high requirements of Web socket for browser, sockjs is introduced to solve this problem. Sockjs is a JavaScript library that provides cross browser JavaScript API and creates a low latency, full duplex communication channel between browser and web server.

2. Relevant environmental requirements

Spring4. 0.6 (to select 4.0 +), tomcat7 Version 0.55, jdk1 7。

3. Specific code

(the following code is available for pro testing!)

(1)web.xml:

(2) dispatcher-servlet. xml

(3)HandshakeInterceptor. java

(4)InfoSocketEndPoint. Java

(5)SystemWebSocketHandler. java

(6)WebSocketConfig. java

(7)testSocket. jsp

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>