效果
不支持websocket的瀏覽器,用flash模擬websocket.當然,也可以用flash socket直接與服務端socket連接。
事實上,websocket協議比較簡單,用actionscript模擬也比較簡單,這個在本屌的另外一篇文章讓ie6 7 8 9支持html5 websocket簡單說了下。
另外,spring為sockjs 提供api,只需簡單配置下,就可以兼容低版本瀏覽器,原理是用js模擬websocket object。具體的本屌還沒有去看。
幾點說明:
1.使用spring對websocket的封裝既可以單獨使用,也可以和spring mvc一起使用。需要注意的是,單獨使用時,仍然要在web.xml中配置spring的dispatcher,仍然要打開server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<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.單獨使用時,若refer跨域,需要在spring中設置白名單
1
2
3
|
<websocket:handlers allowed-origins= "*" > ........ </websocket:handlers> |
3.由于用到了flash,所以需要開啟843端口,并在flash請求policy文件時,返回policy文件。例子中用的是netty4.
4.需要對握手進行攔截,監聽。因為在后面的websocket處理類中,無法從WebSocketSession獲得session.另外,這里獲得session要保存到arrtibutes中,在websocket處理類中,WebSocketSession調用getAttributes()方法就可以獲得arrtibutes了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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.在web-socket-js 中,flash模擬的websocket頭信息中會包含cookie,不過是人工通過腳本添加的。所以要避免需要的cookie,如session cookie是httponly.這就需要設置容器。
如果當前是在eclipse中開發
可以看到在context標簽上添加useHttpOnly='false'就可以了,而context標簽是eclipse部署時自動添加的。
如果已經打包了,就到tomcat目錄/conf/server.xml,在最后的</Host>前面添加
<Context docBase="websocket" path="/websocket" reloadable="true" useHttpOnly='false'/>
意思所述就是本文的全部內容了,希望大家能夠喜歡。