No Description

WebSocketClient.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. WebSocketClient.prototype.updateStatus = function (data) {
  211. return __awaiter(this, void 0, void 0, function () {
  212. return __generator(this, function (_a) {
  213. return [2, this.request('/v1/session/status', data)];
  214. });
  215. });
  216. };
  217. WebSocketClient.prototype.getSessionLists = function () {
  218. return __awaiter(this, void 0, void 0, function () {
  219. return __generator(this, function (_a) {
  220. return [2, this.request('/v1/session/lists', {})];
  221. });
  222. });
  223. };
  224. WebSocketClient.prototype.getAllConversations = function (data) {
  225. return __awaiter(this, void 0, void 0, function () {
  226. return __generator(this, function (_a) {
  227. return [2, this.request('/v1/get/all/conversation', data)];
  228. });
  229. });
  230. };
  231. WebSocketClient.prototype.getAllContact = function (data) {
  232. return __awaiter(this, void 0, void 0, function () {
  233. return __generator(this, function (_a) {
  234. return [2, this.request('/v1/get/all/contact', data)];
  235. });
  236. });
  237. };
  238. WebSocketClient.prototype.addContact = function (data) {
  239. return __awaiter(this, void 0, void 0, function () {
  240. return __generator(this, function (_a) {
  241. return [2, this.request('/v1/add/contact', data)];
  242. });
  243. });
  244. };
  245. WebSocketClient.prototype.deleteContact = function (data) {
  246. return __awaiter(this, void 0, void 0, function () {
  247. return __generator(this, function (_a) {
  248. return [2, this.request('/v1/delete/contact', data)];
  249. });
  250. });
  251. };
  252. WebSocketClient.prototype.maskingContact = function (data) {
  253. return __awaiter(this, void 0, void 0, function () {
  254. return __generator(this, function (_a) {
  255. return [2, this.request('/v1/add/contact/masking', data)];
  256. });
  257. });
  258. };
  259. WebSocketClient.prototype.removeMaskingContact = function (data) {
  260. return __awaiter(this, void 0, void 0, function () {
  261. return __generator(this, function (_a) {
  262. return [2, this.request('/v1/remove/contact/masking', data)];
  263. });
  264. });
  265. };
  266. WebSocketClient.prototype.stickingContact = function (data) {
  267. return __awaiter(this, void 0, void 0, function () {
  268. return __generator(this, function (_a) {
  269. return [2, this.request('/v1/add/contact/stick', data)];
  270. });
  271. });
  272. };
  273. WebSocketClient.prototype.removeStickContact = function (data) {
  274. return __awaiter(this, void 0, void 0, function () {
  275. return __generator(this, function (_a) {
  276. return [2, this.request('/v1/remove/contact/stick', data)];
  277. });
  278. });
  279. };
  280. WebSocketClient.prototype.setNoDisturbing = function (data) {
  281. return __awaiter(this, void 0, void 0, function () {
  282. return __generator(this, function (_a) {
  283. return [2, this.request('/v1/add/contact/no/disturbing', data)];
  284. });
  285. });
  286. };
  287. WebSocketClient.prototype.removeNoDisturbing = function (data) {
  288. return __awaiter(this, void 0, void 0, function () {
  289. return __generator(this, function (_a) {
  290. return [2, this.request('/v1/remove/contact/no/disturbing', data)];
  291. });
  292. });
  293. };
  294. WebSocketClient.prototype.agreeAddContact = function (data) {
  295. return __awaiter(this, void 0, void 0, function () {
  296. return __generator(this, function (_a) {
  297. return [2, this.request('/v1/add/contact/agree', data)];
  298. });
  299. });
  300. };
  301. WebSocketClient.prototype.rejectAddContact = function (data) {
  302. return __awaiter(this, void 0, void 0, function () {
  303. return __generator(this, function (_a) {
  304. return [2, this.request('/v1/add/contact/reject', data)];
  305. });
  306. });
  307. };
  308. WebSocketClient.prototype.addContactServiced = function (data) {
  309. return __awaiter(this, void 0, void 0, function () {
  310. return __generator(this, function (_a) {
  311. return [2, this.request('/v1/add/contact/serviced', data)];
  312. });
  313. });
  314. };
  315. WebSocketClient.prototype.createChatroom = function (data) {
  316. return __awaiter(this, void 0, void 0, function () {
  317. return __generator(this, function (_a) {
  318. return [2, this.request('/v1/create/chatroom', data)];
  319. });
  320. });
  321. };
  322. WebSocketClient.prototype.destroyChatroom = function (data) {
  323. return __awaiter(this, void 0, void 0, function () {
  324. return __generator(this, function (_a) {
  325. return [2, this.request('/v1/destroy/chatroom', data)];
  326. });
  327. });
  328. };
  329. WebSocketClient.prototype.leaveChatroom = function (data) {
  330. return __awaiter(this, void 0, void 0, function () {
  331. return __generator(this, function (_a) {
  332. return [2, this.request('/v1/leave/chatroom', data)];
  333. });
  334. });
  335. };
  336. WebSocketClient.prototype.getChatroomProfile = function (data) {
  337. return __awaiter(this, void 0, void 0, function () {
  338. return __generator(this, function (_a) {
  339. return [2, this.request('/v1/get/chatroom/profile', data)];
  340. });
  341. });
  342. };
  343. WebSocketClient.prototype.updateChatroomSubject = function (data) {
  344. return __awaiter(this, void 0, void 0, function () {
  345. return __generator(this, function (_a) {
  346. return [2, this.request('/v1/update/chatroom/subject', data)];
  347. });
  348. });
  349. };
  350. WebSocketClient.prototype.updateChatroomDescription = function (data) {
  351. return __awaiter(this, void 0, void 0, function () {
  352. return __generator(this, function (_a) {
  353. return [2, this.request('/v1/update/chatroom/description', data)];
  354. });
  355. });
  356. };
  357. WebSocketClient.prototype.addChatroomAdmin = function (data) {
  358. return __awaiter(this, void 0, void 0, function () {
  359. return __generator(this, function (_a) {
  360. return [2, this.request('/v1/add/chatroom/admin', data)];
  361. });
  362. });
  363. };
  364. WebSocketClient.prototype.removeChatroomAdmin = function (data) {
  365. return __awaiter(this, void 0, void 0, function () {
  366. return __generator(this, function (_a) {
  367. return [2, this.request('/v1/remove/chatroom/admin', data)];
  368. });
  369. });
  370. };
  371. WebSocketClient.prototype.addChatroomAdmins = function (data) {
  372. return __awaiter(this, void 0, void 0, function () {
  373. return __generator(this, function (_a) {
  374. return [2, this.request('/v1/add/chatroom/admins', data)];
  375. });
  376. });
  377. };
  378. WebSocketClient.prototype.removeChatroomAdmins = function (data) {
  379. return __awaiter(this, void 0, void 0, function () {
  380. return __generator(this, function (_a) {
  381. return [2, this.request('/v1/remove/chatroom/admins', data)];
  382. });
  383. });
  384. };
  385. WebSocketClient.prototype.joinChatroom = function (data) {
  386. return __awaiter(this, void 0, void 0, function () {
  387. return __generator(this, function (_a) {
  388. return [2, this.request('/v1/join/chatroom', data)];
  389. });
  390. });
  391. };
  392. WebSocketClient.prototype.removeChatroomMember = function (data) {
  393. return __awaiter(this, void 0, void 0, function () {
  394. return __generator(this, function (_a) {
  395. return [2, this.request('/v1/remove/chatroom/member', data)];
  396. });
  397. });
  398. };
  399. WebSocketClient.prototype.fetchStickChatroom = function (data) {
  400. return __awaiter(this, void 0, void 0, function () {
  401. return __generator(this, function (_a) {
  402. return [2, this.request('/v1/fetch/stick/chatroom', data)];
  403. });
  404. });
  405. };
  406. WebSocketClient.prototype.fetchChatroomMembers = function (data) {
  407. return __awaiter(this, void 0, void 0, function () {
  408. return __generator(this, function (_a) {
  409. return [2, this.request('/v1/fetch/chatroom/members', data)];
  410. });
  411. });
  412. };
  413. WebSocketClient.prototype.searchHistoryMessage = function (data) {
  414. return __awaiter(this, void 0, void 0, function () {
  415. return __generator(this, function (_a) {
  416. return [2, this.request('/v1/search/history/message', data)];
  417. });
  418. });
  419. };
  420. return WebSocketClient;
  421. }());
  422. exports.WebSocketClient = WebSocketClient;