| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 */ | 58 */ |
| 59 public enum WebSocketConnectionState { | 59 public enum WebSocketConnectionState { |
| 60 NEW, CONNECTED, REGISTERED, CLOSED, ERROR | 60 NEW, CONNECTED, REGISTERED, CLOSED, ERROR |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 /** | 63 /** |
| 64 * Callback interface for messages delivered on WebSocket. | 64 * Callback interface for messages delivered on WebSocket. |
| 65 * All events are dispatched from a looper executor thread. | 65 * All events are dispatched from a looper executor thread. |
| 66 */ | 66 */ |
| 67 public interface WebSocketChannelEvents { | 67 public interface WebSocketChannelEvents { |
| 68 public void onWebSocketMessage(final String message); | 68 void onWebSocketMessage(final String message); |
| 69 public void onWebSocketClose(); | 69 void onWebSocketClose(); |
| 70 public void onWebSocketError(final String description); | 70 void onWebSocketError(final String description); |
| 71 } | 71 } |
| 72 | 72 |
| 73 public WebSocketChannelClient(LooperExecutor executor, WebSocketChannelEvents
events) { | 73 public WebSocketChannelClient(LooperExecutor executor, WebSocketChannelEvents
events) { |
| 74 this.executor = executor; | 74 this.executor = executor; |
| 75 this.events = events; | 75 this.events = events; |
| 76 roomID = null; | 76 roomID = null; |
| 77 clientID = null; | 77 clientID = null; |
| 78 wsSendQueue = new LinkedList<String>(); | 78 wsSendQueue = new LinkedList<String>(); |
| 79 state = WebSocketConnectionState.NEW; | 79 state = WebSocketConnectionState.NEW; |
| 80 } | 80 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 106 } | 106 } |
| 107 | 107 |
| 108 public void register(final String roomID, final String clientID) { | 108 public void register(final String roomID, final String clientID) { |
| 109 checkIfCalledOnValidThread(); | 109 checkIfCalledOnValidThread(); |
| 110 this.roomID = roomID; | 110 this.roomID = roomID; |
| 111 this.clientID = clientID; | 111 this.clientID = clientID; |
| 112 if (state != WebSocketConnectionState.CONNECTED) { | 112 if (state != WebSocketConnectionState.CONNECTED) { |
| 113 Log.w(TAG, "WebSocket register() in state " + state); | 113 Log.w(TAG, "WebSocket register() in state " + state); |
| 114 return; | 114 return; |
| 115 } | 115 } |
| 116 Log.d(TAG, "Registering WebSocket for room " + roomID + ". CLientID: " + cli
entID); | 116 Log.d(TAG, "Registering WebSocket for room " + roomID + ". ClientID: " + cli
entID); |
| 117 JSONObject json = new JSONObject(); | 117 JSONObject json = new JSONObject(); |
| 118 try { | 118 try { |
| 119 json.put("cmd", "register"); | 119 json.put("cmd", "register"); |
| 120 json.put("roomid", roomID); | 120 json.put("roomid", roomID); |
| 121 json.put("clientid", clientID); | 121 json.put("clientid", clientID); |
| 122 Log.d(TAG, "C->WSS: " + json.toString()); | 122 Log.d(TAG, "C->WSS: " + json.toString()); |
| 123 ws.sendTextMessage(json.toString()); | 123 ws.sendTextMessage(json.toString()); |
| 124 state = WebSocketConnectionState.REGISTERED; | 124 state = WebSocketConnectionState.REGISTERED; |
| 125 // Send any previously accumulated messages. | 125 // Send any previously accumulated messages. |
| 126 for (String sendMessage : wsSendQueue) { | 126 for (String sendMessage : wsSendQueue) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 152 json.put("cmd", "send"); | 152 json.put("cmd", "send"); |
| 153 json.put("msg", message); | 153 json.put("msg", message); |
| 154 message = json.toString(); | 154 message = json.toString(); |
| 155 Log.d(TAG, "C->WSS: " + message); | 155 Log.d(TAG, "C->WSS: " + message); |
| 156 ws.sendTextMessage(message); | 156 ws.sendTextMessage(message); |
| 157 } catch (JSONException e) { | 157 } catch (JSONException e) { |
| 158 reportError("WebSocket send JSON error: " + e.getMessage()); | 158 reportError("WebSocket send JSON error: " + e.getMessage()); |
| 159 } | 159 } |
| 160 break; | 160 break; |
| 161 } | 161 } |
| 162 return; | |
| 163 } | 162 } |
| 164 | 163 |
| 165 // This call can be used to send WebSocket messages before WebSocket | 164 // This call can be used to send WebSocket messages before WebSocket |
| 166 // connection is opened. | 165 // connection is opened. |
| 167 public void post(String message) { | 166 public void post(String message) { |
| 168 checkIfCalledOnValidThread(); | 167 checkIfCalledOnValidThread(); |
| 169 sendWSSMessage("POST", message); | 168 sendWSSMessage("POST", message); |
| 170 } | 169 } |
| 171 | 170 |
| 172 public void disconnect(boolean waitForComplete) { | 171 public void disconnect(boolean waitForComplete) { |
| 173 checkIfCalledOnValidThread(); | 172 checkIfCalledOnValidThread(); |
| 174 Log.d(TAG, "Disonnect WebSocket. State: " + state); | 173 Log.d(TAG, "Disconnect WebSocket. State: " + state); |
| 175 if (state == WebSocketConnectionState.REGISTERED) { | 174 if (state == WebSocketConnectionState.REGISTERED) { |
| 176 // Send "bye" to WebSocket server. | 175 // Send "bye" to WebSocket server. |
| 177 send("{\"type\": \"bye\"}"); | 176 send("{\"type\": \"bye\"}"); |
| 178 state = WebSocketConnectionState.CONNECTED; | 177 state = WebSocketConnectionState.CONNECTED; |
| 179 // Send http DELETE to http WebSocket server. | 178 // Send http DELETE to http WebSocket server. |
| 180 sendWSSMessage("DELETE", ""); | 179 sendWSSMessage("DELETE", ""); |
| 181 } | 180 } |
| 182 // Close WebSocket in CONNECTED or ERROR states only. | 181 // Close WebSocket in CONNECTED or ERROR states only. |
| 183 if (state == WebSocketConnectionState.CONNECTED | 182 if (state == WebSocketConnectionState.CONNECTED |
| 184 || state == WebSocketConnectionState.ERROR) { | 183 || state == WebSocketConnectionState.ERROR) { |
| 185 ws.disconnect(); | 184 ws.disconnect(); |
| 186 state = WebSocketConnectionState.CLOSED; | 185 state = WebSocketConnectionState.CLOSED; |
| 187 | 186 |
| 188 // Wait for websocket close event to prevent websocket library from | 187 // Wait for websocket close event to prevent websocket library from |
| 189 // sending any pending messages to deleted looper thread. | 188 // sending any pending messages to deleted looper thread. |
| 190 if (waitForComplete) { | 189 if (waitForComplete) { |
| 191 synchronized (closeEventLock) { | 190 synchronized (closeEventLock) { |
| 192 while (!closeEvent) { | 191 while (!closeEvent) { |
| 193 try { | 192 try { |
| 194 closeEventLock.wait(CLOSE_TIMEOUT); | 193 closeEventLock.wait(CLOSE_TIMEOUT); |
| 195 break; | 194 break; |
| 196 } catch (InterruptedException e) { | 195 } catch (InterruptedException e) { |
| 197 Log.e(TAG, "Wait error: " + e.toString()); | 196 Log.e(TAG, "Wait error: " + e.toString()); |
| 198 } | 197 } |
| 199 } | 198 } |
| 200 } | 199 } |
| 201 } | 200 } |
| 202 } | 201 } |
| 203 Log.d(TAG, "Disonnecting WebSocket done."); | 202 Log.d(TAG, "Disconnecting WebSocket done."); |
| 204 } | 203 } |
| 205 | 204 |
| 206 private void reportError(final String errorMessage) { | 205 private void reportError(final String errorMessage) { |
| 207 Log.e(TAG, errorMessage); | 206 Log.e(TAG, errorMessage); |
| 208 executor.execute(new Runnable() { | 207 executor.execute(new Runnable() { |
| 209 @Override | 208 @Override |
| 210 public void run() { | 209 public void run() { |
| 211 if (state != WebSocketConnectionState.ERROR) { | 210 if (state != WebSocketConnectionState.ERROR) { |
| 212 state = WebSocketConnectionState.ERROR; | 211 state = WebSocketConnectionState.ERROR; |
| 213 events.onWebSocketError(errorMessage); | 212 events.onWebSocketError(errorMessage); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 @Override | 295 @Override |
| 297 public void onRawTextMessage(byte[] payload) { | 296 public void onRawTextMessage(byte[] payload) { |
| 298 } | 297 } |
| 299 | 298 |
| 300 @Override | 299 @Override |
| 301 public void onBinaryMessage(byte[] payload) { | 300 public void onBinaryMessage(byte[] payload) { |
| 302 } | 301 } |
| 303 } | 302 } |
| 304 | 303 |
| 305 } | 304 } |
| OLD | NEW |