WebSocket 伺服器4

來源:互聯網
上載者:User

標籤:

Java Websocket執行個體Websocket   2015-04-11 14:11:54 發布
您的評價:       4.4  
收藏     6收藏

介紹

現很多網站為了實現即時通訊,所用的技術都是輪詢(polling)。輪詢是在特定的的時間間隔(如每1秒),由瀏覽器對伺服器發出HTTP request,然後由伺服器返回最新的資料給客服端的瀏覽器。這種傳統的HTTP request 的模式帶來很明顯的缺點 – 瀏覽器需要不斷的向伺服器發出請求,然而HTTP request 的header是非常長的,裡麵包含的資料可能只是一個很小的值,這樣會佔用很多的頻寬。

而最比較新的技術去做輪詢的效果是Comet – 用了AJAX。但這種技術雖然可達到全雙工系統通訊,但依然需要發出請求。

在 WebSocket API,瀏覽器和伺服器只需要要做一個握手的動作,然後,瀏覽器和伺服器之間就形成了一條快速通道。兩者之間就直接可以資料互相傳送。

 

依賴:

Tomcat 7 或者 J2EE7

<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-websocket-api</artifactId><version>7.0.47</version><scope>provided</scope></dependency><dependency><groupId>javax</groupId><artifactId>javaee-api</artifactId><version>7.0</version><scope>provided</scope></dependency>

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

詳見: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做反向 Proxy的需要注意啦,socket請求需要做特殊配置的,切記!

 

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

 

服務端

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

 

import com.dooioo.websocket.utils.SessionUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import javax.websocket.*;import javax.websocket.server.PathParam;import javax.websocket.server.ServerEndpoint;/** * 功能說明:websocket處理類, 使用J2EE7的標準 * 切忌直接在該串連處理類中加入業務處理代碼 * liuxing(2014-11-14 04:20)*///relationId和userCode是我的業務標識參數,websocket.ws是串連的路徑,可以自行定義@ServerEndpoint("/websocket.ws/{relationId}/{userCode}")public class WebsocketEndPoint { private static Log log = LogFactory.getLog(WebsocketEndPoint.class);/** * 開啟串連時觸發 * @param relationId * @param userCode * @param session*/@OnOpen public void onOpen(@PathParam("relationId") String relationId, @PathParam("userCode") int userCode, Session session){ log.info("Websocket Start Connecting:"+ SessionUtils.getKey(relationId, userCode)); SessionUtils.put(relationId, userCode, session);}/** * 收到用戶端訊息時觸發 * @param relationId * @param userCode * @param message * @return*/@OnMessage public String onMessage(@PathParam("relationId") String relationId, @PathParam("userCode") int userCode, String message) { return"Got your message ("+ message +").Thanks !";}/** * 異常時觸發 * @param relationId * @param userCode * @param session*/@OnError public void onError(@PathParam("relationId") String relationId, @PathParam("userCode") int userCode, Throwable throwable, Session session) { log.info("Websocket Connection Exception:"+ SessionUtils.getKey(relationId, userCode)); log.info(throwable.getMessage(), throwable); SessionUtils.remove(relationId, userCode);}/** * 關閉串連時觸發 * @param relationId * @param userCode * @param session*/@OnClose public void onClose(@PathParam("relationId") String relationId, @PathParam("userCode") int userCode, Session session) { log.info("Websocket Close Connection:"+ SessionUtils.getKey(relationId, userCode)); SessionUtils.remove(relationId, userCode);}}

  

 

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

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

 

import javax.websocket.Session;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * 功能說明:用來儲存業務定義的sessionId和串連的對應關係 * 利用商務邏輯中組裝的sessionId擷取有效串連後進行後續操作 * liuxing(2014-12-26 02:32)*/public class SessionUtils { public static Map<String, Session> clients = new ConcurrentHashMap<>(); public static void put(String relationId, int userCode, Session session){ clients.put(getKey(relationId, userCode), session);} public static Session get(String relationId, int userCode){ return clients.get(getKey(relationId, userCode));} public static void remove(String relationId, int userCode){ clients.remove(getKey(relationId, userCode));}/** * 判斷是否有串連 * @param relationId * @param userCode * @return*/ public static boolean hasConnection(String relationId, int userCode) { return clients.containsKey(getKey(relationId, userCode));}/** * 組裝唯一識別的key * @param relationId * @param userCode * @return*/ public static String getKey(String relationId, int userCode) { return relationId +"_"+ userCode;}}

 

 

推送資料到用戶端

 

在其他業務方法中調用

 

/** * 將資料傳回用戶端 * 非同步方式 * @param relationId * @param userCode * @param message*/ public void broadcast(String relationId, int userCode, String message) { if (TelSocketSessionUtils.hasConnection(relationId, userCode)) { TelSocketSessionUtils.get(relationId, userCode).getAsyncRemote().sendText(message); } else { throw new NullPointerException(TelSocketSessionUtils.getKey(relationId, userCode) +"Connection does not exist");}}

 

我是使用非同步方法推送資料,還有同步的方法

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

 

用戶端
var webSocket = null;var tryTime = 0;$(function () {initSocket(); window.onbeforeunload = function () {//離開頁面時的其他動作};});/** * 初始化websocket,建立串連*/function initSocket() { if (!window.WebSocket) {alert("您的瀏覽器不支援websocket!"); return false;} webSocket = new WebSocket("ws://127.0.0.1:8080/websocket.ws/"+ relationId +"/"+ userCode); // 收到服務端訊息 webSocket.onmessage = function (msg) {console.log(msg);}; // 異常 webSocket.onerror = function (event) {console.log(event);}; // 建立串連 webSocket.onopen = function (event) {console.log(event);}; // 斷線重連 webSocket.onclose = function () { // 重試10次,每次之間間隔10秒 if (tryTime < 10) { setTimeout(function () { webSocket = null;tryTime++;initSocket(); }, 500); } else { tryTime = 0;}};}

 

其他調試工具

Java實現一個websocket的用戶端

 

<dependency><groupId>org.java-websocket</groupId><artifactId>Java-WebSocket</artifactId><version>1.3.0</version></dependency>

 

import java.io.IOException;  import javax.websocket.ClientEndpoint;  import javax.websocket.OnError;  import javax.websocket.OnMessage;  import javax.websocket.OnOpen;  import javax.websocket.Session; @ClientEndpoint public class MyClient { @OnOpen public void onOpen(Session session) {  System.out.println("Connected to endpoint:"+ session.getBasicRemote());  try { session.getBasicRemote().sendText("Hello"); } catch (IOException ex) { }}@OnMessage public void onMessage(String message) { System.out.println(message);}@OnError public void onError(Throwable t) { t.printStackTrace();}}

 

import java.io.BufferedReader;  import java.io.IOException;  import java.io.InputStreamReader;  import java.net.URI;  import javax.websocket.ContainerProvider;  import javax.websocket.DeploymentException;  import javax.websocket.Session;  import javax.websocket.WebSocketContainer;  public class MyClientApp {  public Session session;  protected void start() { WebSocketContainer container = ContainerProvider.getWebSocketContainer();  String uri ="ws://127.0.0.1:8080/websocket.ws/relationId/12345";  System.out.println("Connecting to"+ uri);  try {  session = container.connectToServer(MyClient.class, URI.create(uri));  } catch (DeploymentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();}} public static void main(String args[]){  MyClientApp client = new MyClientApp(); client.start(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  String input ="";  try { do{ input = br.readLine(); if(!input.equals("exit"))client.session.getBasicRemote().sendText(input);}while(!input.equals("exit")); } catch (IOException e) {  // TODO Auto-generated catch block e.printStackTrace();}}}

 

 

Chrome安裝一個websocket類比用戶端

 

 

 

最後

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

WebSocket 伺服器4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.