Нема описа

WebSocketClient.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. Object.defineProperty(exports, "__esModule", { value: true });
  49. var client_1 = require("./client");
  50. var meta_1 = require("./meta");
  51. var meta = meta_1.getMetaData();
  52. var WebSocketClient = (function () {
  53. function WebSocketClient(wsUrl, token, sid, chatToken) {
  54. this.status = {
  55. created: false,
  56. connected: false,
  57. login: false
  58. };
  59. this.wsUrl = wsUrl;
  60. this.token = token;
  61. this.chatToken = chatToken;
  62. this.sid = sid;
  63. }
  64. WebSocketClient.prototype.init = function () {
  65. return __awaiter(this, void 0, void 0, function () {
  66. return __generator(this, function (_a) {
  67. return [2, this.create()];
  68. });
  69. });
  70. };
  71. WebSocketClient.prototype.getStatus = function () {
  72. return this.status;
  73. };
  74. WebSocketClient.prototype.create = function () {
  75. return __awaiter(this, void 0, void 0, function () {
  76. var url;
  77. var _this = this;
  78. return __generator(this, function (_a) {
  79. url = this.wsUrl;
  80. return [2, new Promise(function (resolve, reject) {
  81. if (!url) {
  82. throw new Error('websocket url is required.');
  83. }
  84. _this.client = client_1.getClient(url, {
  85. onOpen: function () { return __awaiter(_this, void 0, void 0, function () {
  86. var data;
  87. var _this = this;
  88. return __generator(this, function (_a) {
  89. switch (_a.label) {
  90. case 0:
  91. try {
  92. this.ping();
  93. this.interval = setInterval(function () {
  94. _this.ping();
  95. }, PING_INTERVAL);
  96. }
  97. catch (err) {
  98. this.client.reconnect();
  99. }
  100. return [4, this.connect()];
  101. case 1:
  102. data = _a.sent();
  103. return [4, this.authentication()];
  104. case 2:
  105. _a.sent();
  106. resolve(data.value);
  107. return [2];
  108. }
  109. });
  110. }); },
  111. onError: function (err) {
  112. resolve(false);
  113. },
  114. onClose: function (CloseEvent) {
  115. clearInterval(_this.interval);
  116. }
  117. });
  118. })];
  119. });
  120. });
  121. };
  122. WebSocketClient.prototype.on = function (url, callback) {
  123. if (!url) {
  124. throw new Error('url is required.');
  125. }
  126. this.client.addMessageListener(url, callback);
  127. };
  128. WebSocketClient.prototype.off = function (url) {
  129. this.client.removeMessageListener(url);
  130. };
  131. WebSocketClient.prototype.request = function (url, data) {
  132. return __awaiter(this, void 0, void 0, function () {
  133. var error;
  134. return __generator(this, function (_a) {
  135. if (this.client.readyState !== 1) {
  136. error = 'websocket connection not ready!';
  137. throw new Error(error);
  138. }
  139. try {
  140. return [2, this.client.asyncSend(url, data)];
  141. }
  142. catch (e) {
  143. console.error(e);
  144. throw e;
  145. }
  146. return [2];
  147. });
  148. });
  149. };
  150. WebSocketClient.prototype.connect = function () {
  151. return __awaiter(this, void 0, void 0, function () {
  152. var sid, data, id;
  153. return __generator(this, function (_a) {
  154. switch (_a.label) {
  155. case 0:
  156. sid = this.sid;
  157. if (sid) {
  158. this.client.setRequestProperty('sid', sid);
  159. }
  160. return [4, this.request('/v1/session/start', __assign({}, this.chatToken, meta))];
  161. case 1:
  162. data = _a.sent();
  163. if (data && data.value) {
  164. id = data.value;
  165. this.sid = id;
  166. this.client.setRequestProperty('sid', id);
  167. }
  168. return [2, data || true];
  169. }
  170. });
  171. });
  172. };
  173. WebSocketClient.prototype.ping = function () {
  174. return this.client.ping({});
  175. };
  176. WebSocketClient.prototype.authentication = function () {
  177. return __awaiter(this, void 0, void 0, function () {
  178. var tk, data;
  179. return __generator(this, function (_a) {
  180. tk = this.token;
  181. data = this.request('/v1/session/bind/uid/by/token', { token: tk });
  182. return [2, data || true];
  183. });
  184. });
  185. };
  186. WebSocketClient.prototype.onMessage = function (callback) {
  187. this.on('/v1/message/listener', callback);
  188. };
  189. WebSocketClient.prototype.sendMessage = function (data) {
  190. return this.request('/v1/send/message', data);
  191. };
  192. WebSocketClient.prototype.markServiced = function (data) {
  193. return this.request('/v1/mark/message/serviced', data);
  194. };
  195. WebSocketClient.prototype.getHistoryMessage = function (data) {
  196. return this.request('/v1/history/message', data);
  197. };
  198. return WebSocketClient;
  199. }());
  200. exports.WebSocketClient = WebSocketClient;