Effect
The browser that does not support WebSocket, use Flash to simulate WebSocket. Of course, you can also use Flash Socket to directly connect to the server socket.
In fact, the WebSocket protocol is relatively simple, and it is simpler to simulate with ActionScript. This another article in this book allows IE6 7 9 9 to support HTML5 WebSocket simply.
In addition, Spring provides APIs for SockJS. As long as it is simply configured, you can also comply with the low version of the browser. The principle is to use JS to simulate WebSocket Object. I haven't seen it yet.
Some of the descriptions:
1. Using Spring's encapsulation of WebSocket can be used alone or with Spring MVC. It should be noted that when used alone, you still need to configure the Dispatcher of Spring in the web.xml, and you still have to open the server.
<Servlet> <Servlet-name> WebSocket </Servlet-Name> <Servlet-Class> Org.springFramework.web.Servlet.dispatcherVlet </Servlet-Class> <PARA m-name> ContextConfigLocation </param- name> <Param-Value> /Web-inf/applicationContext.xml </Param-Value> </Init-Param> <Load-On-Startup> 1 </Load-Startup> </Servlet- MAPPING> <Servlet-Name> WebSocket </Servlet-Name> <URL-PATTERN>/</Url-Pattern> </Servlet-MAPPING>>
2. When used alone, if Refer cross -domain, you need to set a whitelist in Spring
<websocket: handlers allowed-origins = "*"> ........ </websocket: handlers>
3. Because Flash is used, it is necessary to turn on port 843 and return the Policy file when Flash requested the Policy file. The example is netty4.
4. You need to intercept and monitor the handshake. Because in the WebSocket processing class behind, it is impossible to get session from WebSocketSession. In addition, the session is saved into Arrtibutes here. In the WebSocket processing class, the gettattributes () method can be obtained by calling the websocketSession. ARRTIBUTES.
Public Class ChatinterCepter Extends HttpSessionhandShaandshakeinterCepTor {@Override Public Boolean BeforehandShake (ServerHttprequest Request, Response, WebSocketHandler Wshandler, Map <string, Object> Attributes) Throws Exception {if (Request Instanceof Servlettprequest) { Prequest ServletRequest = (ServletServerHttprequest) Request; httpsession session = ServletRequest.getServletRequest (). GetSession (FALSE); if (session! = Null) {string username = (string) session.gettattribute ("user"); .put ("user", username);}} System.out .println ("" Before Handshake "+Request.Getheaders ()); // Return Super.beForehandshake (Request, Response, WSHandler, ATTRIBUTES); Return True; .............
5. In Web-Socket-JS, the WebSocket header information simulated by Flash will contain cookie, but it is added by the script. So to avoid the required cookies, such as Session Cookie is httponly. This requires a container.
If it is currently developed in Eclipse
It can be seen that adding usehttponly = 'false' is added to the Context tag, and the Context tag is automatically added when Eclipse is deployed.
If you have already packed it, go to the Tomcat directory /conf/server.xml, add it before the last </host>
Copy code code as follows:
<Context docBase = "websocket" path = "/websocket" reloadable = "true" usehttponly = 'false'/>
The meaning of this is all the contents of this article, I hope everyone can like it.