| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @constructor | 6 * @constructor |
| 7 * @implements {InspectorBackendClass.Connection} | 7 * @implements {InspectorBackendClass.Connection} |
| 8 * @param {!InspectorBackendClass.Connection.Params} params | 8 * @param {!InspectorBackendClass.Connection.Params} params |
| 9 */ | 9 */ |
| 10 WebInspector.MainConnection = function(params) | 10 WebInspector.MainConnection = function(params) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 fulfill(); | 104 fulfill(); |
| 105 }); | 105 }); |
| 106 return promise; | 106 return promise; |
| 107 }, | 107 }, |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 /** | 110 /** |
| 111 * @constructor | 111 * @constructor |
| 112 * @implements {InspectorBackendClass.Connection} | 112 * @implements {InspectorBackendClass.Connection} |
| 113 * @param {string} url | 113 * @param {string} url |
| 114 * @param {function()} onWebSocketDisconnect |
| 114 * @param {!InspectorBackendClass.Connection.Params} params | 115 * @param {!InspectorBackendClass.Connection.Params} params |
| 115 */ | 116 */ |
| 116 WebInspector.WebSocketConnection = function(url, params) | 117 WebInspector.WebSocketConnection = function(url, onWebSocketDisconnect, params) |
| 117 { | 118 { |
| 118 this._socket = new WebSocket(url); | 119 this._socket = new WebSocket(url); |
| 119 this._socket.onerror = this._onError.bind(this); | 120 this._socket.onerror = this._onError.bind(this); |
| 120 this._socket.onopen = this._onOpen.bind(this); | 121 this._socket.onopen = this._onOpen.bind(this); |
| 121 this._socket.onmessage = (messageEvent) => params.onMessage.call(null, /** @
type {string} */ (messageEvent.data)); | 122 this._socket.onmessage = (messageEvent) => params.onMessage.call(null, /** @
type {string} */ (messageEvent.data)); |
| 123 this._socket.onclose = this._onClose.bind(this); |
| 124 |
| 122 this._onDisconnect = params.onDisconnect; | 125 this._onDisconnect = params.onDisconnect; |
| 123 this._socket.onclose = params.onDisconnect.bind(null, "websocket closed"); | 126 this._onWebSocketDisconnect = onWebSocketDisconnect; |
| 124 | |
| 125 this._connected = false; | 127 this._connected = false; |
| 126 this._messages = []; | 128 this._messages = []; |
| 127 }; | 129 }; |
| 128 | 130 |
| 129 WebInspector.WebSocketConnection.prototype = { | 131 WebInspector.WebSocketConnection.prototype = { |
| 130 _onError: function() | 132 _onError: function() |
| 131 { | 133 { |
| 134 this._onWebSocketDisconnect.call(null); |
| 132 // This is called if error occurred while connecting. | 135 // This is called if error occurred while connecting. |
| 133 this._onDisconnect.call(null, "connection failed"); | 136 this._onDisconnect.call(null, "connection failed"); |
| 134 this._close(); | 137 this._close(); |
| 135 }, | 138 }, |
| 136 | 139 |
| 137 _onOpen: function() | 140 _onOpen: function() |
| 138 { | 141 { |
| 139 this._socket.onerror = console.error; | 142 this._socket.onerror = console.error; |
| 140 this._connected = true; | 143 this._connected = true; |
| 141 for (var message of this._messages) | 144 for (var message of this._messages) |
| 142 this._socket.send(message); | 145 this._socket.send(message); |
| 143 this._messages = []; | 146 this._messages = []; |
| 144 }, | 147 }, |
| 145 | 148 |
| 146 _close: function() | 149 _onClose: function() |
| 150 { |
| 151 this._onWebSocketDisconnect.call(null); |
| 152 this._onDisconnect.call(null, "websocket closed"); |
| 153 this._close(); |
| 154 }, |
| 155 |
| 156 /** |
| 157 * @param {function()=} callback |
| 158 */ |
| 159 _close: function(callback) |
| 147 { | 160 { |
| 148 this._socket.onerror = null; | 161 this._socket.onerror = null; |
| 149 this._socket.onopen = null; | 162 this._socket.onopen = null; |
| 150 this._socket.onclose = null; | 163 this._socket.onclose = callback || null; |
| 151 this._socket.onmessage = null; | 164 this._socket.onmessage = null; |
| 152 this._socket.close(); | 165 this._socket.close(); |
| 153 this._socket = null; | 166 this._socket = null; |
| 167 this._onWebSocketDisconnect = null; |
| 154 }, | 168 }, |
| 155 | 169 |
| 156 /** | 170 /** |
| 157 * @override | 171 * @override |
| 158 * @param {string} message | 172 * @param {string} message |
| 159 */ | 173 */ |
| 160 sendMessage: function(message) | 174 sendMessage: function(message) |
| 161 { | 175 { |
| 162 if (this._connected) | 176 if (this._connected) |
| 163 this._socket.send(message); | 177 this._socket.send(message); |
| 164 else | 178 else |
| 165 this._messages.push(message); | 179 this._messages.push(message); |
| 166 }, | 180 }, |
| 167 | 181 |
| 168 /** | 182 /** |
| 169 * @override | 183 * @override |
| 170 * @return {!Promise} | 184 * @return {!Promise} |
| 171 */ | 185 */ |
| 172 disconnect: function() | 186 disconnect: function() |
| 173 { | 187 { |
| 174 this._onDisconnect.call(null, "force disconnect"); | 188 var fulfill; |
| 175 this._close(); | 189 var promise = new Promise(f => fulfill = f); |
| 176 return Promise.resolve(); | 190 this._close(() => { |
| 191 this._onDisconnect.call(null, "force disconnect"); |
| 192 fulfill(); |
| 193 }); |
| 194 return promise; |
| 177 } | 195 } |
| 178 }; | 196 }; |
| 179 | 197 |
| 180 /** | 198 /** |
| 181 * @constructor | 199 * @constructor |
| 182 * @implements {InspectorBackendClass.Connection} | 200 * @implements {InspectorBackendClass.Connection} |
| 183 * @param {!InspectorBackendClass.Connection.Params} params | 201 * @param {!InspectorBackendClass.Connection.Params} params |
| 184 */ | 202 */ |
| 185 WebInspector.StubConnection = function(params) | 203 WebInspector.StubConnection = function(params) |
| 186 { | 204 { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 213 * @return {!Promise} | 231 * @return {!Promise} |
| 214 */ | 232 */ |
| 215 disconnect: function() | 233 disconnect: function() |
| 216 { | 234 { |
| 217 this._onDisconnect.call(null, "force disconnect"); | 235 this._onDisconnect.call(null, "force disconnect"); |
| 218 this._onDisconnect = null; | 236 this._onDisconnect = null; |
| 219 this._onMessage = null; | 237 this._onMessage = null; |
| 220 return Promise.resolve(); | 238 return Promise.resolve(); |
| 221 }, | 239 }, |
| 222 }; | 240 }; |
| OLD | NEW |