1、HandshakeInterceptor 類實現(xiàn)握手,獲取session中的登陸用戶信息,用以區(qū)分WebSocketSession,對指定用戶進(jìn)行推送信息。可使用WebSocketSession對象獲取對應(yīng)的用戶:session.getAttributes().get( "username" ) public class HandshakeInterceptor 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 ) { // 使用userName區(qū)分WebSocketHandler,以便定向發(fā)送消息 String username = (String) session.getAttribute( "username" ); attributes.put( "username", username ); } } return true; } @Override public void afterHandshake( ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex ) { }} 2、MyWebSocketHandler類:消息處理類,需要實現(xiàn)WebSocketHandler接口。定義靜態(tài)變量users集合,記錄當(dāng)前登錄用戶的WebSocketSession信息。 public class WebsocketEndPoint implements WebSocketHandler { private static final ArrayList<WebSocketSession> users; static { users = new ArrayList<WebSocketSession>(); } @Override public void afterConnectionEstablished( WebSocketSession session ) throws Exception { users.add( session ); String username = (String) session.getAttributes().get( "username" ); System.out.println( username + "登陸成功" ); } @Override public void handleMessage( WebSocketSession session, WebSocketMessage<?> message ) throws Exception { // TODO Auto-generated method stub System.out.println( "接收到用戶:" + (String) session.getAttributes().get( "username" ) + " 發(fā)送的信息:" + message.getPayload().toString() ); sendMessageToUser( "dave", new TextMessage( (String) session.getAttributes().get( "username" ) + "發(fā)送給您:" + message.getPayload() ) ); } @Override public void handleTransportError( WebSocketSession session, Throwable exception ) throws Exception { // TODO Auto-generated method stub } @Override public void afterConnectionClosed( WebSocketSession session, CloseStatus closeStatus ) throws Exception { // TODO Auto-generated method stub } @Override public boolean supportsPartialMessages() { // TODO Auto-generated method stub return false; } public void sendMessageToUser( String username, TextMessage message ) throws IOException { for ( WebSocketSession user : users ) { if ( username.equals( user.getAttributes().get( "username" ) ) ) { user.sendMessage( message ); } } } } 3、springmvc配置文件: <bean id="websocket" class="com.websocket.websocket.MyWebSocketHandler"/> <websocket:handlers> <websocket:mapping path="/ws" handler="websocket"/> <websocket:handshake-interceptors> <bean class="com.websocket.websocket.HandshakeInterceptor"/> </websocket:handshake-interceptors> </websocket:handlers> 4、html頁面: //判斷當(dāng)前瀏覽器是否支持websocket if('WebSocket' in window){ websocket = new WebSocket("ws://localhost:8080/wesocket/ws"); }else{ alert("non-support"); } //連接發(fā)生錯誤 websocket.onerror = function(){ alert("error connect"); }; //連接成功 websocket.onopen = function(){ alert("open connnect"); }; //接收到的消息 websocket.onmessage = function(event){ alert(event.data); }; //連接關(guān)閉 websocket.onclose = function(){ alert("close"); websocket.close(); }; }) //發(fā)送消息 $("#sendBtn").click(function(){ websocket.send("我是admin用戶"); }) 資料參考網(wǎng)址: http://www.cnblogs.com/winkey4986/p/5478332.html http://www./posts/view/21 |
|