UNPKG

3.97 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "tslib", "@theintern/common", "./Base", "../browser/util", "../common/util"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var tslib_1 = require("tslib");
13 var common_1 = require("@theintern/common");
14 var Base_1 = tslib_1.__importDefault(require("./Base"));
15 var util_1 = require("../browser/util");
16 var util_2 = require("../common/util");
17 var WebSocketChannel = (function (_super) {
18 tslib_1.__extends(WebSocketChannel, _super);
19 function WebSocketChannel(options) {
20 var _this = _super.call(this, options) || this;
21 _this.timeout = 10000;
22 if (options.timeout !== undefined) {
23 _this.timeout = options.timeout;
24 }
25 if (!options.port) {
26 throw new Error('A port is required for a WebSocket channel');
27 }
28 var url = util_1.parseUrl(options.url);
29 var host = url.hostname;
30 var protocol = url.protocol === 'https' ? 'wss' : 'ws';
31 _this._socket = new common_1.global.WebSocket(protocol + "://" + host + ":" + options.port);
32 _this._ready = new common_1.Task(function (resolve, reject) {
33 _this._socket.addEventListener('open', resolve);
34 _this._socket.addEventListener('error', reject);
35 });
36 _this._socket.addEventListener('message', function (event) {
37 _this._handleMessage(JSON.parse(event.data));
38 });
39 _this._socket.addEventListener('error', function (_event) {
40 _this._handleError(new Error('WebSocket error'));
41 });
42 _this._sendQueue = Object.create(null);
43 _this._sequence = 1;
44 return _this;
45 }
46 WebSocketChannel.prototype._sendData = function (name, data) {
47 var _this = this;
48 return this._ready.then(function () {
49 return new common_1.Task(function (resolve, reject) {
50 var id = String(_this._sequence++);
51 var sessionId = _this.sessionId;
52 var message = { id: id, sessionId: sessionId, name: name, data: data };
53 _this._socket.send(util_2.stringify(message));
54 var timer = setTimeout(function () {
55 reject(new Error('Send timed out'));
56 }, _this.timeout);
57 _this._sendQueue[id] = {
58 resolve: function (data) {
59 clearTimeout(timer);
60 resolve(data);
61 },
62 reject: function (error) {
63 reject(error);
64 },
65 };
66 });
67 });
68 };
69 WebSocketChannel.prototype._handleMessage = function (message) {
70 var id = message.id;
71 this._sendQueue[id].resolve(message);
72 this._sendQueue[id] = undefined;
73 };
74 WebSocketChannel.prototype._handleError = function (error) {
75 var _this = this;
76 this._ready = common_1.Task.reject(error);
77 Object.keys(this._sendQueue)
78 .filter(function (id) { return _this._sendQueue[id] != null; })
79 .forEach(function (id) {
80 _this._sendQueue[id].reject(error);
81 _this._sendQueue[id] = undefined;
82 });
83 };
84 return WebSocketChannel;
85 }(Base_1.default));
86 exports.default = WebSocketChannel;
87});
88//# sourceMappingURL=WebSocket.js.map
\No newline at end of file