Java Websocket實例

記錄下自己在用的websocket

 

介紹
現很多網站爲了實現即時通訊,所用的技術都是輪詢(polling)。輪詢是在特定的的時間間隔(如每1秒),由瀏覽器對服務器發出HTTP request,然後由服務器返回最新的數據給客服端的瀏覽器。這種傳統的HTTP request 的模式帶來很明顯的缺點 – 瀏覽器需要不斷的向服務器發出請求,然而HTTP request 的header是非常長的,裏面包含的數據可能只是一個很小的值,這樣會佔用很多的帶寬。
而最比較新的技術去做輪詢的效果是Comet – 用了AJAX。但這種技術雖然可達到全雙工通信,但依然需要發出請求。
在 WebSocket API,瀏覽器和服務器只需要要做一個握手的動作,然後,瀏覽器和服務器之間就形成了一條快速通道。兩者之間就直接可以數據互相傳送。
 

運行環境:

實現了websocket的瀏覽器:

Chrome
Supported in version 4+
Firefox
Supported in version 4+
Internet Explorer
Supported in version 10+
Opera
Supported in version 10+
Safari
Supported in version 5+

 

依賴:

 

Tomcat 7 或者 J2EE7

 

Java代碼  收藏代碼
  1. <dependency>  
  2.     <groupId>org.apache.tomcat</groupId>  
  3.     <artifactId>tomcat-websocket-api</artifactId>  
  4.     <version>7.0.47</version>  
  5.     <scope>provided</scope>  
  6. </dependency>  
  7.   
  8.   
  9. <dependency>  
  10.     <groupId>javax</groupId>  
  11.     <artifactId>javaee-api</artifactId>  
  12.     <version>7.0</version>  
  13.     <scope>provided</scope>  
  14. </dependency>  

 

 

 

注意:早前業界沒有統一的標準,各服務器都有各自的實現,現在J2EE7JSR356已經定義了統一的標準,請儘量使用支持最新通用標準的服務器。

詳見:http://www.oracle.com/technetwork/articles/java/jsr356-1937161.html

           http://jinnianshilongnian.iteye.com/blog/1909962

 

我是用的Tomcat 7.0.57 + Java7

必須是Tomcat 7.0.47以上

詳見:http://www.iteye.com/news/28414

 

ps:最早我們是用的Tomcat 7自帶的實現,後來要升級Tomcat 8,結果原來的實現方式在Tomcat 8不支持了,就只好切換到支持Websocket 1.0版本的Tomcat了。

 

主流的java web服務器都有支持JSR365標準的版本了,請自行Google。 

 

用nginx做反向代理的需要注意啦,socket請求需要做特殊配置的,切記!

 

Tomcat的處理方式建議修改爲NIO的方式,同時修改連接數到合適的參數,請自行Google!

 

服務端

服務端不需要在web.xml中做額外的配置,Tomcat啓動後就可以直接連接了。

 

Java代碼  收藏代碼
  1. import com.dooioo.websocket.utils.SessionUtils;  
  2. import org.apache.commons.logging.Log;  
  3. import org.apache.commons.logging.LogFactory;  
  4.   
  5. import javax.websocket.*;  
  6. import javax.websocket.server.PathParam;  
  7. import javax.websocket.server.ServerEndpoint;  
  8.   
  9. /** 
  10.  * 功能說明:websocket處理類, 使用J2EE7的標準 
  11.  *         切忌直接在該連接處理類中加入業務處理代碼 
  12.  * 作者:liuxing(2014-11-14 04:20) 
  13.  */  
  14. //relationId和userCode是我的業務標識參數,websocket.ws是連接的路徑,可以自行定義  
  15. @ServerEndpoint("/websocket.ws/{relationId}/{userCode}")  
  16. public class WebsocketEndPoint {  
  17.   
  18.     private static Log log = LogFactory.getLog(WebsocketEndPoint.class);  
  19.   
  20.     /** 
  21.      * 打開連接時觸發 
  22.      * @param relationId 
  23.      * @param userCode 
  24.      * @param session 
  25.      */  
  26.     @OnOpen  
  27.     public void onOpen(@PathParam("relationId") String relationId,  
  28.                        @PathParam("userCode"int userCode,  
  29.                        Session session){  
  30.         log.info("Websocket Start Connecting: " + SessionUtils.getKey(relationId, userCode));  
  31.         SessionUtils.put(relationId, userCode, session);  
  32.     }  
  33.   
  34.     /** 
  35.      * 收到客戶端消息時觸發 
  36.      * @param relationId 
  37.      * @param userCode 
  38.      * @param message 
  39.      * @return 
  40.      */  
  41.     @OnMessage  
  42.     public String onMessage(@PathParam("relationId") String relationId,  
  43.                             @PathParam("userCode"int userCode,  
  44.                             String message) {  
  45.         return "Got your message (" + message + ").Thanks !";  
  46.     }  
  47.   
  48.     /** 
  49.      * 異常時觸發 
  50.      * @param relationId 
  51.      * @param userCode 
  52.      * @param session 
  53.      */  
  54.     @OnError  
  55.     public void onError(@PathParam("relationId") String relationId,  
  56.                         @PathParam("userCode"int userCode,  
  57.                         Throwable throwable,  
  58.                         Session session) {  
  59.         log.info("Websocket Connection Exception: " + SessionUtils.getKey(relationId, userCode));  
  60.         log.info(throwable.getMessage(), throwable);  
  61.         SessionUtils.remove(relationId, userCode);  
  62.     }  
  63.   
  64.     /** 
  65.      * 關閉連接時觸發 
  66.      * @param relationId 
  67.      * @param userCode 
  68.      * @param session 
  69.      */  
  70.     @OnClose  
  71.     public void onClose(@PathParam("relationId") String relationId,  
  72.                         @PathParam("userCode"int userCode,  
  73.                         Session session) {  
  74.         log.info("Websocket Close Connection: " + SessionUtils.getKey(relationId, userCode));  
  75.         SessionUtils.remove(relationId, userCode);  
  76.     }  
  77.   
  78. }  

  

 

工具類用來存儲唯一key和連接

這個是我業務的需要,我的業務是服務器有對應動作觸發時,推送數據到客戶端,沒有接收客戶端數據的操作。

 

Java代碼  收藏代碼
  1. import javax.websocket.Session;  
  2. import java.util.Map;  
  3. import java.util.concurrent.ConcurrentHashMap;  
  4.   
  5. /** 
  6.  * 功能說明:用來存儲業務定義的sessionId和連接的對應關係 
  7.  *          利用業務邏輯中組裝的sessionId獲取有效連接後進行後續操作 
  8.  * 作者:liuxing(2014-12-26 02:32) 
  9.  */  
  10. public class SessionUtils {  
  11.   
  12.     public static Map<String, Session> clients = new ConcurrentHashMap<>();  
  13.   
  14.     public static void put(String relationId, int userCode, Session session){  
  15.         clients.put(getKey(relationId, userCode), session);  
  16.     }  
  17.   
  18.     public static Session get(String relationId, int userCode){  
  19.         return clients.get(getKey(relationId, userCode));  
  20.     }  
  21.   
  22.     public static void remove(String relationId, int userCode){  
  23.         clients.remove(getKey(relationId, userCode));  
  24.     }  
  25.   
  26.     /** 
  27.      * 判斷是否有連接 
  28.      * @param relationId 
  29.      * @param userCode 
  30.      * @return 
  31.      */  
  32.     public static boolean hasConnection(String relationId, int userCode) {  
  33.         return clients.containsKey(getKey(relationId, userCode));  
  34.     }  
  35.   
  36.     /** 
  37.      * 組裝唯一識別的key 
  38.      * @param relationId 
  39.      * @param userCode 
  40.      * @return 
  41.      */  
  42.     public static String getKey(String relationId, int userCode) {  
  43.         return relationId + "_" + userCode;  
  44.     }  
  45.   
  46. }  

 

 

推送數據到客戶端

 

在其他業務方法中調用

 

Java代碼  收藏代碼
  1. /** 
  2.  * 將數據傳回客戶端 
  3.  * 異步的方式 
  4.  * @param relationId 
  5.  * @param userCode 
  6.  * @param message 
  7.  */  
  8. public void broadcast(String relationId, int userCode, String message) {  
  9.     if (TelSocketSessionUtils.hasConnection(relationId, userCode)) {  
  10.         TelSocketSessionUtils.get(relationId, userCode).getAsyncRemote().sendText(message);  
  11.     } else {  
  12.         throw new NullPointerException(TelSocketSessionUtils.getKey(relationId, userCode) + " Connection does not exist");  
  13.     }  
  14.   
  15. }  

 

我是使用異步的方法推送數據,還有同步的方法

詳見:http://docs.oracle.com/javaee/7/api/javax/websocket/Session.html

 

客戶端

Js代碼  收藏代碼
  1. var webSocket = null;  
  2. var tryTime = 0;  
  3. $(function () {  
  4.     initSocket();  
  5.   
  6.     window.onbeforeunload = function () {  
  7.         //離開頁面時的其他操作  
  8.     };  
  9. });  
  10.   
  11. /** 
  12.  * 初始化websocket,建立連接 
  13.  */  
  14. function initSocket() {  
  15.     if (!window.WebSocket) {  
  16.         alert("您的瀏覽器不支持websocket!");  
  17.         return false;  
  18.     }  
  19.   
  20.     webSocket = new WebSocket("ws://127.0.0.1:8080/websocket.ws/" + relationId + "/" + userCode);  
  21.       
  22.     // 收到服務端消息  
  23.     webSocket.onmessage = function (msg) {  
  24.         console.log(msg);  
  25.     };  
  26.       
  27.     // 異常  
  28.     webSocket.onerror = function (event) {  
  29.         console.log(event);  
  30.     };  
  31.       
  32.     // 建立連接  
  33.     webSocket.onopen = function (event) {  
  34.         console.log(event);  
  35.     };  
  36.   
  37.     // 斷線重連  
  38.     webSocket.onclose = function () {  
  39.         // 重試10次,每次之間間隔10秒  
  40.         if (tryTime < 10) {  
  41.             setTimeout(function () {  
  42.                 webSocket = null;  
  43.                 tryTime++;  
  44.                 initSocket();  
  45.             }, 500);  
  46.         } else {  
  47.             tryTime = 0;  
  48.         }  
  49.     };  
  50.   
  51. }  

 

其他調試工具

Java實現一個websocket的客戶端

 

Java代碼  收藏代碼
  1. <dependency>  
  2.             <groupId>org.java-websocket</groupId>  
  3.             <artifactId>Java-WebSocket</artifactId>  
  4.             <version>1.3.0</version>  
  5.         </dependency>  

 

Java代碼  收藏代碼
  1. import java.io.IOException;    
  2.     import javax.websocket.ClientEndpoint;    
  3.     import javax.websocket.OnError;    
  4.     import javax.websocket.OnMessage;    
  5.     import javax.websocket.OnOpen;    
  6.     import javax.websocket.Session;    
  7.          
  8.     @ClientEndpoint    
  9.     public class MyClient {    
  10.         @OnOpen    
  11.         public void onOpen(Session session) {    
  12.             System.out.println("Connected to endpoint: " + session.getBasicRemote());    
  13.             try {    
  14.                 session.getBasicRemote().sendText("Hello");    
  15.             } catch (IOException ex) {    
  16.             }    
  17.         }    
  18.          
  19.         @OnMessage    
  20.         public void onMessage(String message) {    
  21.             System.out.println(message);    
  22.         }    
  23.          
  24.         @OnError    
  25.         public void onError(Throwable t) {    
  26.             t.printStackTrace();    
  27.         }    
  28.     }    

 

Java代碼  收藏代碼
  1. import java.io.BufferedReader;    
  2.     import java.io.IOException;    
  3.     import java.io.InputStreamReader;    
  4.     import java.net.URI;    
  5.     import javax.websocket.ContainerProvider;    
  6.     import javax.websocket.DeploymentException;    
  7.     import javax.websocket.Session;    
  8.     import javax.websocket.WebSocketContainer;    
  9.          
  10.     public class MyClientApp {    
  11.          
  12.         public Session session;    
  13.          
  14.         protected void start()    
  15.                  {    
  16.          
  17.                 WebSocketContainer container = ContainerProvider.getWebSocketContainer();    
  18.          
  19.                 String uri = "ws://127.0.0.1:8080/websocket.ws/relationId/12345";    
  20.                 System.out.println("Connecting to " + uri);    
  21.                 try {    
  22.                     session = container.connectToServer(MyClient.class, URI.create(uri));    
  23.                 } catch (DeploymentException e) {    
  24.                     e.printStackTrace();    
  25.                 } catch (IOException e) {    
  26.                     e.printStackTrace();    
  27.                 }                 
  28.          
  29.         }    
  30.         public static void main(String args[]){    
  31.             MyClientApp client = new MyClientApp();    
  32.             client.start();    
  33.          
  34.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    
  35.             String input = "";    
  36.             try {    
  37.                 do{    
  38.                     input = br.readLine();    
  39.                     if(!input.equals("exit"))    
  40.                         client.session.getBasicRemote().sendText(input);    
  41.          
  42.                 }while(!input.equals("exit"));    
  43.          
  44.             } catch (IOException e) {    
  45.                 // TODO Auto-generated catch block    
  46.                 e.printStackTrace();    
  47.             }    
  48.         }    
  49.     }    

 

 

Chrome安裝一個websocket模擬客戶端

 


 

 

最後

爲了統一的操作體驗,對於一些不支持websocket的瀏覽器,請使用socketjs技術做客戶端開發。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章