WebSocketDemo
WebSocket 安卓用戶端的實現方式。 介紹
如果不想瞭解其中的原理可以直接拉到最後面的使用方式章節,按照教程使用即可,或者直接開啟 demo 查看代碼,代碼地址:。
https://github.com/0xZhangKe/WebSocketDemo
本文使用一個後台 Service 來建立 WebSocket 串連,Activity 與 Fragment 需要使用 WebSocket 介面時只需要綁定該服務既可。
WebSocketService 接收到資料之後會通知每一個綁定了 WebSocketService 服務的 Activity 及 Fragment,其自身也可以對返回的資料進行判斷等等,具體如何操作根據業務需求定。
下圖是 WebSocketService 的工作流程圖
WebSocketService 負責建立 WebSocket 串連,接收返回的資料,接收到的資料通過 EventBus 發送出去,串連失敗之後可以自動重連。
下圖是 Activity 的工作流程圖
Activity/Fragment 綁定 WebSocket 服務,綁定成功後可以直接調用 WebSocketService 對象發送資料。 WebSocketService 添加必要的依賴
首先添加 WebSocket 架構依賴:
compile 'com.neovisionaries:nv-websocket-client:2.3'
這個架構也是我在 Github 上找了一圈之後選中的一個,使用的人很多,文檔齊全,還在繼續維護。
另外還要添加一個 EventBus 及阿里的 JSON 架構:
compile 'com.alibaba:fastjson:1.2.33'compile 'org.greenrobot:eventbus:3.0.0'
好了完事大吉,現在開始吧。 定義 WebSocket 提供的介面
先建立一個 WebSocket 的介面,其中定義了 WebSocket 必須提供的幾個公開方法:
public interface IWebSocket { /** * 發送資料 * * @param text 需要發送的資料 */ void sendText(String text); /** * 0-未串連 * 1-正在串連 * 2-已串連 */ int getConnectStatus(); /** * 重新串連 */ void reconnect(); /** * 關閉串連 */ void stop();}
WebSocketService 需要實現這個介面,後面綁定 WebSocketService 時直接通過 IWebSocket 建立對象既可。 AbsWebSocketService
我這裡為了降低代碼的耦合度,將與商務邏輯相關的代碼(介面地址、資料處理及分發等)與 WebSocket 的串連、發送資料等操作剝離開來,所以這裡建立的時一個抽象類別 AbsWebSocketService 來實現與商務邏輯無關的代碼。
在實際使用中只需要建立一個 WebSocketService 並繼承該 AbsWebSocketService 既可,不需要改動其中的代碼。
首先看一下 AbsWebSocketService 的代碼:
public abstract class AbsBaseWebSocketService extends Service implements IWebSocket { private static final String TAG = "AbsBaseWebSocketService"; private static final int TIME_OUT = 15000; private static WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(TIME_OUT); private AbsBaseWebSocketService.WebSocketThread webSocketThread; private WebSocket webSocket; private AbsBaseWebSocketService.ServiceBinder serviceBinder = new AbsBaseWebSocketService.ServiceBinder(); public class ServiceBinder extends Binder { public AbsBaseWebSocketService getService() { return AbsBaseWebSocketService.this; } } private boolean stop = false; /** * 0-未串連 * 1-正在串連 * 2-已串連 */ private int connectStatus = 0;//是否已串連 @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate()"); ProxySettings settings = factory.getProxySettings(); settings.addHeader("Content-Type", "text/json"); connectStatus = 0; webSocketThread = new AbsBaseWebSocketService.WebSocketThread(); webSocketThread.start(); Log.i(TAG, "onCreated"); } @Nullable @Override public IBinder onBind(Intent intent) { if (serviceBinder == null) { serviceBinder = new AbsBaseWebSocketService.ServiceBinder(); } Log.i(TAG, "onBind"); return serviceBinder; } @Override public void onDestroy() { super.onDestroy(); stop = true; webSocket.disconnect(); webSocket.flush(); webSocket = null; connectStatus = 0; Log.i(TAG, "onDestroy"); } /** * 擷取伺服器位址 */ protected abstract String getConnectUrl(); /** * 分發響應資料 */ protected abstract void dispatchResponse(String textResponse); /** * 串連成功發送 WebSocketConnectedEvent 事件, * 請求成功發送 CommonResponse 事件, * 請求失敗發送 WebSocketSendDataErrorEvent 事件。 */ private class WebSocketThread extends Thread { @Override public void run() { Log.i(TAG, "WebSocketThread->run()"); setupWebSocket(); } } private void setupWebSocket() { if (connectStatus != 0) return; connectStatus = 1; try { webSocket = factory.createSocket(getConnectUrl()); webSocket.addListener(new WebSocketAdapter() { @Override public void onTextMessage(WebSocket websocket, String text) throws Exception { super.onTextMessage(websocket, text); if (debug()) { Log.i(TAG, String.format("onTextMessage->%s", text)); } dispatchResponse(text); } @Override public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) throws Exception { super.onTextMessageError(websocket, cause, data); Log.e(TAG, "onTextMessageError()", cause); EventBus.getDefault().post(new WebSocketSendDataErrorEvent("", "", "onTextMessageError():" + cause.toString())); } @Override public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { super.onDisconnected(websocket, serverCloseFrame, clientCloseFrame, closedByServer); EventBus.getDefault().post(new DisconnectedEvent()); Log.e(TAG, "onDisconnected()"); connectStatus = 0; if (!stop) { //斷開之後自動重連 setupWebSocket(); } } @Override public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { super.onConnected(websocket, headers); Log.i(TAG, "onConnected()"); connectStatus = 2; EventBus.getDefault().post(new WebSocketConnectedEvent()); } @Override public void onError(WebSocket websocket, WebSocketException cause) throws Exception { super.onError(websocket, cause); Log.e(TAG, "onError()", cause); EventBus.getDefault().post(new WebSocketConnectionErrorEvent("onError:" + cause.getMessage())); } }); try { webSocket.connect(); } catch (NullPointerException e) { connectStatus = 0; Log.i(TAG, String.format("NullPointerException()->%s", e.getMessage())); Log.e(TAG, "NullPointerException()", e); EventBus.getDefault().post(new WebSocketConnectionErrorEvent("NullPointerException:" + e.getMessage())); } catch (OpeningHandshakeException e) { connectStatus = 0; Log.i(TAG, String.format("OpeningHandshakeException()->%s", e.getMessage())); Log.e(TAG, "OpeningHandshakeException()", e); StatusLine sl = e.getStatusLine(); Log.i(TAG, "=== Status Line ==="); Log.e(TAG, "=== Status Line ==="); Log.i(TAG, String.format("HTTP Version = %s\n", sl.getHttpVersion())); Log.e(TAG, String.format("HTTP Version = %s\n", sl.getHttpVersion())); Log.i(TAG, String.format("Status Code = %s\n", sl.getStatusCode())); Log.e(TAG, String.format("Status Code = %s\n", sl.getStatusCode())); Log.i(TAG, String.format("Reason Phrase = %s\n", sl.getReasonPhrase())); Log.e(TAG, String.format("Reason Phrase = %s\n", sl.getReasonPhrase())); Map<String, List<String>> headers = e.getHeaders(); Log.i(TAG, "=== HTTP Headers ==="); Log.e(TAG, "=== HTTP Headers ==="); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { // Header name. String name = entry.getKey(); // Values of the header. List<String> values = entry.getValue(); if (values == null || values.size() == 0) { // Print the name only. System.out.println(name); continue; } for (String value : values) { // Print the name and the value. Log.e(TAG, String.format("%s: %s\n", name, value)); Log.i(TAG, String.format("%s: %s\n", name, value)); } } EventBus.getDefault().post(new WebSocketConnectionErrorEvent("OpeningHandshakeException:" + e.getMessage())); } catch (HostnameUnverifiedException e) { connectStatus = 0; // The certificate of the peer does not match the expected hostname. Log.i(TAG, String.format("HostnameUnverifiedException()->%s", e.getMessage())); Log.e(TAG, "HostnameUnverifiedException()", e); EventBus.getDefault().post(new WebSocketConnectionErrorEvent("HostnameUnverifiedException:" + e.getMessage())); } catch (WebSocketException e) { connectStatus = 0; // Failed to establish a WebSocket connection. Log.i(TAG, String.format("WebSocketException()->%s", e.getMessage())); Log.e(TAG, "WebSocketException()", e); EventBus.getDefault().post(new WebSocketConnectionErrorEvent("WebSocketException:" + e.getMessage())); } } catch (IOException e) { connectStatus = 0; Log.i(TAG, String.format("IOException()->%s", e.getMessage())); Log.e(TAG, "IOException()", e); EventBus.getDefault().post(new WebSocketConnectionErrorEvent("IOException:" + e.getMessage())); } } @Override public void sendText(String text) { if (TextUtils.isEmpty(text)) return; if (debug()) { Log.i(TAG, String.format("sendText()->%s", text)); } if (webSocket != null && connectStatus == 2) { webSocket.sendText(text); } } @Override public int getConnectStatus() { return connectStatus; } @Override public void reconnect() { Log.i(TAG, "reconnect()"); new Thread(new Runnable() { @Override public void run() { Log.i(TAG, "reconnect()->begin restart..."); try { Thread.sleep(200); }catch(Exception e){ Log.e(TAG, "reconnect()->run: ", e); } if (webSocketThread != null && !webSocketThread.isAlive()) { connectStatus = 0; webSocketThread = new WebSocketThread(); webSocketThread.start(); Log.i(TAG, "reconnect()->start success"); } else { Log.i(TAG, "reconnect()->start failed: webSocketThread==null || webSocketThread.isAlive()"); } } }).start(); } @Override public void stop() { Log.i(TAG, "stop()"); webSocket.disconnect(); stop = true; Log.i(TAG, "stop()->success"); } public boolean debug() { try { ApplicationInfo info = getApplication().getApplicationInfo(); return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; } catch (Exception e) { return false; } }}
其中有兩個抽象方法:
String getConnectUrl()//擷取伺服器串連地址void dispatchResponse(String textResponse)//接收到資料後回調此方法,在此方法中分發資料
建立好上面的 AbsWebSocketService 服務之後,還需要根據業務需求建立一個 WebSocketService 實現該類。 WebSocketService 服務
這個代碼就很簡單了,如下:
public class WebSocketService extends AbsBaseWebSocketService { @Override protected String getConnectUrl() { return "伺服器對應的url"; } @Override protected void dispatchResponse(String textResponse) { //處理資料 try { CommonResponse<String> response = JSON.parseObject(textResponse, new TypeReference<CommonResponse<String>>() { }); if (response == null) { EventBus.getDefault().post(new WebSocketSendDataErrorEvent("", textResponse, "響應資料為空白")); return; } //此處可根據伺服器介面文檔進行調整,判斷 code 值是否合法,如下:// if (response.getCode() >= 1000 && response.getCode() < 2000) {// EventBus.getDefault().post(response);// }else{// EventBus.getDefault().post(new WebSocketSendDataErrorEvent(response.getCommand().getPath(), textResponse, response.getMsg()));// } EventBus.getDefault().post(response); }catch(Exception e){ //一般由於 JSON 解析時出現異常 EventBus.getDefault().post(new WebSocketSendDataErrorEvent("", textResponse, "資料異常:" + e.getMessage())); } }}
dispatchResponse(String) 方法中就是將資料轉換成對應的實體,然後使用 EventBus 將其發送出去,可以再其中做一些資料正確的判斷,比如上面注釋的地方。
其中的 CommonResponse 是我們後台介面的一個標準模板,所有格介面返回的資料都應該按照這個格式來,這個類就按照自家的介面寫就行了,不用按照我的。看一下其中的代碼:
public class CommonResponse<T> { private String msg; private T data; private int code; private String path; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getPath() { return path; } public void setPath(String path) { this.path = path; }}
其中 path 表示介面地址,其本質就是個字串,我們通過這個字串當做一個標識符,標識返回的資料屬於哪個介面,然後我們才能做出對應的操作。
泛型 T 表示資料的實體,一般來說我們會按照不同的介面寫出不同的實體方便使用,當然了,這些都不重要,也只是我的個人習慣,這裡也不涉及核心代碼,所以可以根據個人愛好隨意改動。
別忘了在 AndroidManifest 中註冊該服務,然後在合適的時候啟動該服務,我的是在 Application 中的 onCreate 方法啟動的:
public class GateApplication extends Application { @Override public void onCreate() { super.onCreate(); Intent intent = new Intent(this, WebSocketService.class); startService(intent); }}
到了這裡 WebSocket 服務就已經介紹完了,但是我們如果直接這麼用肯定很麻煩。
比如當調用 WebSocketService.sendText(String) 方法時發現 WebSocket 串連已經斷了、綁定 WebSocket 服務、判斷其串連狀態等等,還有很多事情要做,總不能每個 Activity/Fragment 都要寫這麼多代碼去判斷吧。
為此我又寫了 AbsBaseWebSocketActivity 與 AbsBaseWebSocketFragment 兩個抽象類別,其中屏蔽掉了大部分的串連狀態判斷等等操作。
比如我們調用 AbsBaseWebSocketFragment.sendText(String) 方法時,可以直接判斷出當前時候是串連狀態,如果未串連則重新串連,串連完成後再去發送資料。
先來看一下 ABSBaseWebSocketActivity 的代碼: AbsBaseWebSocketActivity
其中主要包括綁定服務,判斷串連狀態,發送資料等操作,另外暴露出了幾個方法以供使用:
public abstract class AbsBaseWebSocketActivity extends BaseAppCompatActivity { /** * 服務重連次數, * 這裡指的是綁定 WebSocket 服務失敗時使用的重連次數,一般來說不會出現綁定失敗的情況 */ private final int RECONNECT_TIME = 5; private IWebSocket mWebSocketService; protected String networkErrorTips; /** * 串連時機:</br> * 0 - 剛進入介面時,如果 WebSocket 還未串連,會繼續串連,或者由於某些原因 WebSocket 斷開,會自動重連,從而會觸發串連成功/失敗事件;</br> * 1 - onResume() 方法回調時判斷 WebSocket 是否串連,如果未串連,則進行串連,從而觸發串連成功/失敗事件;</br> * 2 - sendText() 方法會判斷 WebSocket 是否已經串連,如果未串連,則進行串連,從而觸發串連成功/失敗事件,此時串連成功後應繼續調用 sendText() 方法發送資料。</br> * <p> * 另外,當 connectType != 0 時,每次使用完之後應該設定為 0。因為 0 的狀態是無法預知的,隨時可能調用。 */ private int connectType = 0; /** * 需要發送的資料,當 connectType == 2 時會使用。 */ private String needSendText; private boolean isConnected = false; private boolean networkReceiverIsRegister = false; private int connectTime = 0; protected ServiceConnection mWebSocketServiceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "onServiceConnected()"); mWebSocketService = (IWebSocket) ((AbsBaseWebSocketService.ServiceBinder) service).getService(); //此處假設要不就已經串連,要不就未串連,未串連就等著接收串連成功/失敗的廣播即可 if (mWebSocketService.getConnectStatus() == 2) { Log.i(TAG, "onServiceConnected()->mWebSocketService.getConnectStatus() == 2; BindSuccess"); onServiceBindSuccess(); } else { Log.i(TAG, String.format("o