暫無描述

WebSocketClient.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ts_linker_sdk_1 = require("ts-linker-sdk");
  50. var meta_1 = require("./meta");
  51. var meta = meta_1.getMetaData();
  52. var PING_INTERVAL = 50 * 1000;
  53. var WebSocketClient = (function () {
  54. function WebSocketClient(wsUrl, token, sid, chatToken) {
  55. this.status = {
  56. created: false,
  57. connected: false,
  58. login: false
  59. };
  60. this.wsUrl = wsUrl;
  61. this.token = token;
  62. this.chatToken = chatToken;
  63. this.sid = sid;
  64. }
  65. WebSocketClient.prototype.init = function () {
  66. return __awaiter(this, void 0, void 0, function () {
  67. return __generator(this, function (_a) {
  68. return [2, this.create()];
  69. });
  70. });
  71. };
  72. WebSocketClient.prototype.getStatus = function () {
  73. return this.status;
  74. };
  75. WebSocketClient.prototype.create = function () {
  76. return __awaiter(this, void 0, void 0, function () {
  77. var url;
  78. var _this = this;
  79. return __generator(this, function (_a) {
  80. url = this.wsUrl;
  81. return [2, new Promise(function (resolve, reject) {
  82. if (!url) {
  83. throw new Error('websocket url is required.');
  84. }
  85. _this.client = ts_linker_sdk_1.Client.getInstance(url, {
  86. onOpen: function () { return __awaiter(_this, void 0, void 0, function () {
  87. var data;
  88. var _this = this;
  89. return __generator(this, function (_a) {
  90. switch (_a.label) {
  91. case 0:
  92. try {
  93. this.ping();
  94. this.interval = setInterval(function () {
  95. _this.ping();
  96. }, PING_INTERVAL);
  97. }
  98. catch (err) {
  99. console.error(err);
  100. }
  101. return [4, this.connect()];
  102. case 1:
  103. data = _a.sent();
  104. return [4, this.authentication()];
  105. case 2:
  106. _a.sent();
  107. resolve(data.value);
  108. return [2];
  109. }
  110. });
  111. }); },
  112. onError: function () {
  113. resolve(false);
  114. },
  115. onClose: function () {
  116. clearInterval(_this.interval);
  117. }
  118. });
  119. })];
  120. });
  121. });
  122. };
  123. WebSocketClient.prototype.on = function (url, callback) {
  124. if (!url) {
  125. throw new Error('url is required.');
  126. }
  127. this.client.addMessageListener(url, callback);
  128. };
  129. WebSocketClient.prototype.off = function (url) {
  130. this.client.removeMessageListener(url);
  131. };
  132. WebSocketClient.prototype.request = function (url, data) {
  133. return __awaiter(this, void 0, void 0, function () {
  134. return __generator(this, function (_a) {
  135. return [2, this.client.request(url, data)];
  136. });
  137. });
  138. };
  139. WebSocketClient.prototype.connect = function () {
  140. return __awaiter(this, void 0, void 0, function () {
  141. var sid, data, id;
  142. return __generator(this, function (_a) {
  143. switch (_a.label) {
  144. case 0:
  145. sid = this.sid;
  146. if (sid) {
  147. this.client.setRequestProperty('sid', sid);
  148. }
  149. return [4, this.request('/v1/session/start', __assign({}, this.chatToken, meta))];
  150. case 1:
  151. data = _a.sent();
  152. if (data && data.value) {
  153. id = data.value;
  154. this.sid = id;
  155. this.client.setRequestProperty('sid', id);
  156. }
  157. return [2, data || true];
  158. }
  159. });
  160. });
  161. };
  162. WebSocketClient.prototype.ping = function () {
  163. return this.client.ping({});
  164. };
  165. WebSocketClient.prototype.authentication = function () {
  166. return __awaiter(this, void 0, void 0, function () {
  167. var tk, data;
  168. return __generator(this, function (_a) {
  169. tk = this.token;
  170. data = this.request('/v1/session/bind/uid/by/token', { token: tk });
  171. return [2, data || true];
  172. });
  173. });
  174. };
  175. WebSocketClient.prototype.onMessage = function (callback) {
  176. this.on('/v1/message/listener', callback);
  177. };
  178. WebSocketClient.prototype.sendMessage = function (data) {
  179. return this.request('/v1/send/message', data);
  180. };
  181. WebSocketClient.prototype.markServiced = function (data) {
  182. return this.request('/v1/mark/message/serviced', data);
  183. };
  184. WebSocketClient.prototype.getHistoryMessage = function (data) {
  185. return this.request('/v1/history/message', data);
  186. };
  187. return WebSocketClient;
  188. }());
  189. exports.WebSocketClient = WebSocketClient;