No Description

WebSocketClient.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. reject(err);
  101. }
  102. return [4, this.connect()];
  103. case 1:
  104. data = _a.sent();
  105. return [4, this.authentication()];
  106. case 2:
  107. _a.sent();
  108. resolve(data.value);
  109. return [2];
  110. }
  111. });
  112. }); },
  113. onError: function () {
  114. resolve(false);
  115. },
  116. onClose: function () {
  117. clearInterval(_this.interval);
  118. }
  119. });
  120. })];
  121. });
  122. });
  123. };
  124. WebSocketClient.prototype.on = function (url, callback) {
  125. if (!url) {
  126. throw new Error('url is required.');
  127. }
  128. this.client.addMessageListener(url, callback);
  129. };
  130. WebSocketClient.prototype.off = function (url) {
  131. this.client.removeMessageListener(url);
  132. };
  133. WebSocketClient.prototype.request = function (url, data) {
  134. return __awaiter(this, void 0, void 0, function () {
  135. return __generator(this, function (_a) {
  136. return [2, this.client.request(url, data)];
  137. });
  138. });
  139. };
  140. WebSocketClient.prototype.connect = function () {
  141. return __awaiter(this, void 0, void 0, function () {
  142. var sid, data, id;
  143. return __generator(this, function (_a) {
  144. switch (_a.label) {
  145. case 0:
  146. sid = this.sid;
  147. if (sid) {
  148. this.client.setRequestProperty('sid', sid);
  149. }
  150. return [4, this.request('/v1/session/start', __assign({}, this.chatToken, meta))];
  151. case 1:
  152. data = _a.sent();
  153. if (data && data.value) {
  154. id = data.value;
  155. this.sid = id;
  156. this.client.setRequestProperty('sid', id);
  157. }
  158. return [2, data || true];
  159. }
  160. });
  161. });
  162. };
  163. WebSocketClient.prototype.ping = function () {
  164. return this.client.ping({});
  165. };
  166. WebSocketClient.prototype.authentication = function () {
  167. return __awaiter(this, void 0, void 0, function () {
  168. var tk, data;
  169. return __generator(this, function (_a) {
  170. switch (_a.label) {
  171. case 0:
  172. tk = this.token;
  173. return [4, this.request('/v1/session/bind/uid/by/token', { token: tk })];
  174. case 1:
  175. data = _a.sent();
  176. return [2, data || true];
  177. }
  178. });
  179. });
  180. };
  181. WebSocketClient.prototype.onMessage = function (callback) {
  182. return __awaiter(this, void 0, void 0, function () {
  183. return __generator(this, function (_a) {
  184. this.on('/v1/message/listener', callback);
  185. return [2];
  186. });
  187. });
  188. };
  189. WebSocketClient.prototype.sendMessage = function (data) {
  190. return __awaiter(this, void 0, void 0, function () {
  191. return __generator(this, function (_a) {
  192. return [2, this.request('/v1/send/message', data)];
  193. });
  194. });
  195. };
  196. WebSocketClient.prototype.markServiced = function (data) {
  197. return __awaiter(this, void 0, void 0, function () {
  198. return __generator(this, function (_a) {
  199. return [2, this.request('/v1/mark/message/serviced', data)];
  200. });
  201. });
  202. };
  203. WebSocketClient.prototype.getHistoryMessage = function (data) {
  204. return __awaiter(this, void 0, void 0, function () {
  205. return __generator(this, function (_a) {
  206. return [2, this.request('/v1/history/message', data)];
  207. });
  208. });
  209. };
  210. return WebSocketClient;
  211. }());
  212. exports.WebSocketClient = WebSocketClient;