Spring4+websocket_java compatible with IE6 7 8 9 in Java

Source: Internet
Author: User

Effect

WebSocket Browser is not supported, use Flash to simulate websocket. Of course, you can also use the flash socket to connect directly with the service socket.

In fact, the WebSocket protocol is relatively simple, using ActionScript simulation is also relatively simple, this in another article of this cock let IE6 7 8 9 support HTML5 WebSocket simply said.

In addition, Spring provides APIs for SOCKJS, simply configured to be compatible with the lower version of the browser, the principle is to use JS simulation WebSocket object. The concrete cock hasn't gone to see it yet.

A few notes:

1. The use of spring for websocket encapsulation can be used either separately or with spring MVC. Note that when used alone, you still have to configure spring's dispatcher in Web.xml and still open the 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. When used alone, if refer cross domain, you need to set up whitelist in spring

  <websocket:handlers allowed-origins= "*" > ...
  </websocket:handlers>

3. Due to the use of flash, you need to open 843 ports, and in Flash request policy file, return policy file. The example is Netty4.

4. The handshake needs to be intercepted and monitored. Because in the back WebSocket processing class, Unable to get session from Websocketsession. In addition, get session here to save to Arrtibutes, in the WebSocket processing class, websocketsession invoke GetAttributes () method to get the arrtibutes.

 public class Chatintercepter extends httpsessionhandshakeinterceptor{@Override p
      Ublic 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 in Flash simulation contains cookies, but is manually added by the script. Therefore, to avoid the need for cookies, such as the session cookie is httponly. This requires setting up the container.

If you are currently developing in eclipse

You can see Adding usehttponly= ' false ' on the Context tab, which is added automatically when eclipse is deployed.

If it's already packaged, go to the Tomcat directory/conf/server.xml, add the last </Host> before

Copy Code code as follows:

<context docbase= "WebSocket" path= "/websocket" reloadable= "true" usehttponly= ' false '/>

The meaning is the whole content of this article, I hope you can enjoy it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.