Spring4 + websocket (compatible with ie6 7 8 9) and spring4websocket
Reprinted Please note:TheViper http://www.cnblogs.com/TheViper
Effect
Browsers that do 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 also relatively simple to simulate using actionscript. This is another article in this article, let ie6 7 8 9 support html5 websocket briefly.
In addition, spring provides APIs for sockjs. with simple configuration, it can be compatible with low-version browsers. The principle is to simulate websocket object using js. The specific book has not been viewed yet.
Download the example in this article. The flash source code is in the text that allows ie6 7 8 9 to support html5 websocket.
Notes:
1. Using spring to encapsulate websocket can be used independently or together with spring mvc. You must note that you still need to configure spring dispatcher in web. xml when using spring separately, and you still need to enable server.
<servlet> <servlet-name>websocket</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>websocket</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
2. If refer cross-origin is used separately, you need to set a whitelist in spring.
<websocket:handlers allowed-origins="*"> ........ </websocket:handlers>
3. Because flash is used, you need to enable port 843 and return the policy file when requesting the policy file in flash. Netty4.
4. Handshakes must be intercepted and listened. In the websocket processing class, the session cannot be obtained from WebSocketSession. in addition, the obtained session is saved to arrtibutes. In the websocket processing class, WebSocketSession calls the getAttributes () method to obtain arrtibutes.
public class ChatIntercepter extends HttpSessionHandshakeInterceptor{ @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; HttpSession session = servletRequest.getServletRequest().getSession(false); if (session != null) { String userName = (String) session.getAttribute("user"); attributes.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 contains cookies, but is manually added through scripts. Therefore, you need to avoid the required cookies. For example, if the session cookie is httponly, you need to set the container.
If it is currently developed in eclipse
You can see that you can add useHttpOnly = 'false' to the context tag, which is automatically added during eclipse deployment.
If it has been packaged, go to the tomcat directory/conf/server. xml and add
<Context docBase="websocket" path="/websocket" reloadable="true" useHttpOnly='false'/>
If you think the content of this article is helpful to you, You can reward me: