| 252 | } |
| 253 | |
| 254 | attach(rawChannel) { |
| 255 | this.init(); |
| 256 | |
| 257 | // Must get object and class methods to be compatible with the tests. |
| 258 | const channelProps = [...Object.keys(rawChannel), ...Object.getOwnPropertyNames(Object.getPrototypeOf(rawChannel))]; |
| 259 | for (let i = 0; i < rawChannelProps.length; i++) { |
| 260 | const prop = rawChannelProps[i]; |
| 261 | if (channelProps.indexOf(prop) < 0) { |
| 262 | throw new Error('Raw channel missing property: ' + prop); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | this._websocket = rawChannel; |
| 267 | this._websocket.binaryType = "arraybuffer"; |
| 268 | this._websocket.onmessage = this._recvMessage.bind(this); |
| 269 | |
| 270 | this._websocket.onopen = () => { |
| 271 | Log.Debug('>> WebSock.onopen'); |
| 272 | if (this._websocket.protocol) { |
| 273 | Log.Info("Server choose sub-protocol: " + this._websocket.protocol); |
| 274 | } |
| 275 | |
| 276 | this._eventHandlers.open(); |
| 277 | Log.Debug("<< WebSock.onopen"); |
| 278 | }; |
| 279 | |
| 280 | this._websocket.onclose = (e) => { |
| 281 | Log.Debug(">> WebSock.onclose"); |
| 282 | this._eventHandlers.close(e); |
| 283 | Log.Debug("<< WebSock.onclose"); |
| 284 | }; |
| 285 | |
| 286 | this._websocket.onerror = (e) => { |
| 287 | Log.Debug(">> WebSock.onerror: " + e); |
| 288 | this._eventHandlers.error(e); |
| 289 | Log.Debug("<< WebSock.onerror: " + e); |
| 290 | }; |
| 291 | } |
| 292 | |
| 293 | close() { |
| 294 | if (this._websocket) { |