暂无描述

WebSocketClient.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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("@bilingo.com/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, extendOpts) {
  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. this.extendOpts = extendOpts;
  65. }
  66. WebSocketClient.prototype.init = function () {
  67. return __awaiter(this, void 0, void 0, function () {
  68. return __generator(this, function (_a) {
  69. return [2, this.create()];
  70. });
  71. });
  72. };
  73. WebSocketClient.prototype.getStatus = function () {
  74. return this.status;
  75. };
  76. WebSocketClient.prototype.create = function () {
  77. return __awaiter(this, void 0, void 0, function () {
  78. var url;
  79. var _this = this;
  80. return __generator(this, function (_a) {
  81. url = this.wsUrl;
  82. return [2, new Promise(function (resolve, reject) {
  83. if (!url) {
  84. throw new Error('websocket url is required.');
  85. }
  86. var binaryType = 'blob';
  87. if (_this.extendOpts && _this.extendOpts.binaryType) {
  88. binaryType = _this.extendOpts.binaryType;
  89. }
  90. _this.client = ts_linker_sdk_1.Client.getInstance(url, {
  91. onOpen: function () { return __awaiter(_this, void 0, void 0, function () {
  92. var data;
  93. var _this = this;
  94. return __generator(this, function (_a) {
  95. switch (_a.label) {
  96. case 0:
  97. try {
  98. this.ping();
  99. this.interval = setInterval(function () {
  100. _this.ping();
  101. }, PING_INTERVAL);
  102. }
  103. catch (err) {
  104. console.error(err);
  105. reject(err);
  106. }
  107. return [4, this.connect()];
  108. case 1:
  109. data = _a.sent();
  110. return [4, this.authentication()];
  111. case 2:
  112. _a.sent();
  113. resolve(data.value);
  114. return [2];
  115. }
  116. });
  117. }); },
  118. onError: function () {
  119. resolve(false);
  120. },
  121. onClose: function () {
  122. clearInterval(_this.interval);
  123. }
  124. }, binaryType, true);
  125. })];
  126. });
  127. });
  128. };
  129. WebSocketClient.prototype.on = function (url, callback) {
  130. if (!url) {
  131. throw new Error('url is required.');
  132. }
  133. this.client.addMessageListener(url, callback);
  134. };
  135. WebSocketClient.prototype.off = function (url) {
  136. this.client.removeMessageListener(url);
  137. };
  138. WebSocketClient.prototype.request = function (url, data) {
  139. return __awaiter(this, void 0, void 0, function () {
  140. return __generator(this, function (_a) {
  141. return [2, this.client.request(url, data)];
  142. });
  143. });
  144. };
  145. WebSocketClient.prototype.connect = function () {
  146. return __awaiter(this, void 0, void 0, function () {
  147. var sid, param, data, id;
  148. return __generator(this, function (_a) {
  149. switch (_a.label) {
  150. case 0:
  151. sid = this.sid;
  152. if (sid) {
  153. this.client.setRequestProperty('sid', sid);
  154. }
  155. param = __assign({}, this.chatToken, meta);
  156. if (this.extendOpts && this.extendOpts.connectOptions) {
  157. param = __assign({}, param, this.extendOpts.connectOptions);
  158. }
  159. return [4, this.request('/v1/session/start', __assign({}, param))];
  160. case 1:
  161. data = _a.sent();
  162. if (data && data.value) {
  163. id = data.value;
  164. this.sid = id;
  165. this.client.setRequestProperty('sid', id);
  166. }
  167. return [2, data || true];
  168. }
  169. });
  170. });
  171. };
  172. WebSocketClient.prototype.ping = function () {
  173. return this.client.ping({});
  174. };
  175. WebSocketClient.prototype.authentication = function () {
  176. return __awaiter(this, void 0, void 0, function () {
  177. var tk, data;
  178. return __generator(this, function (_a) {
  179. switch (_a.label) {
  180. case 0:
  181. tk = this.token;
  182. return [4, this.request('/v1/session/bind/uid/by/token', { token: tk })];
  183. case 1:
  184. data = _a.sent();
  185. return [2, data || true];
  186. }
  187. });
  188. });
  189. };
  190. WebSocketClient.prototype.onMessage = function (callback) {
  191. return __awaiter(this, void 0, void 0, function () {
  192. return __generator(this, function (_a) {
  193. this.on('/v1/message/listener', callback);
  194. return [2];
  195. });
  196. });
  197. };
  198. WebSocketClient.prototype.sendMessage = function (data) {
  199. return __awaiter(this, void 0, void 0, function () {
  200. return __generator(this, function (_a) {
  201. return [2, this.request('/v1/send/message', data)];
  202. });
  203. });
  204. };
  205. WebSocketClient.prototype.markServiced = function (data) {
  206. return __awaiter(this, void 0, void 0, function () {
  207. return __generator(this, function (_a) {
  208. return [2, this.request('/v1/mark/message/serviced', data)];
  209. });
  210. });
  211. };
  212. WebSocketClient.prototype.messageReaded = function (data) {
  213. return __awaiter(this, void 0, void 0, function () {
  214. return __generator(this, function (_a) {
  215. return [2, this.request('/v1/mark/messages/read', data)];
  216. });
  217. });
  218. };
  219. WebSocketClient.prototype.getHistoryMessage = function (data) {
  220. return __awaiter(this, void 0, void 0, function () {
  221. return __generator(this, function (_a) {
  222. return [2, this.request('/v1/history/message', data)];
  223. });
  224. });
  225. };
  226. WebSocketClient.prototype.updateStatus = function (data) {
  227. return __awaiter(this, void 0, void 0, function () {
  228. return __generator(this, function (_a) {
  229. return [2, this.request('/v1/session/status', data)];
  230. });
  231. });
  232. };
  233. WebSocketClient.prototype.getSessionLists = function () {
  234. return __awaiter(this, void 0, void 0, function () {
  235. return __generator(this, function (_a) {
  236. return [2, this.request('/v1/session/lists', {})];
  237. });
  238. });
  239. };
  240. WebSocketClient.prototype.getAllConversations = function (data) {
  241. return __awaiter(this, void 0, void 0, function () {
  242. return __generator(this, function (_a) {
  243. return [2, this.request('/v1/get/all/conversation', data)];
  244. });
  245. });
  246. };
  247. WebSocketClient.prototype.getAllContact = function (data) {
  248. return __awaiter(this, void 0, void 0, function () {
  249. return __generator(this, function (_a) {
  250. return [2, this.request('/v1/users/friends', data)];
  251. });
  252. });
  253. };
  254. WebSocketClient.prototype.addContact = function (data) {
  255. return __awaiter(this, void 0, void 0, function () {
  256. return __generator(this, function (_a) {
  257. return [2, this.request('/v1/add/contact', data)];
  258. });
  259. });
  260. };
  261. WebSocketClient.prototype.deleteContact = function (data) {
  262. return __awaiter(this, void 0, void 0, function () {
  263. return __generator(this, function (_a) {
  264. return [2, this.request('/v1/delete/contact', data)];
  265. });
  266. });
  267. };
  268. WebSocketClient.prototype.maskingContact = function (data) {
  269. return __awaiter(this, void 0, void 0, function () {
  270. return __generator(this, function (_a) {
  271. return [2, this.request('/v1/add/contact/masking', data)];
  272. });
  273. });
  274. };
  275. WebSocketClient.prototype.removeMaskingContact = function (data) {
  276. return __awaiter(this, void 0, void 0, function () {
  277. return __generator(this, function (_a) {
  278. return [2, this.request('/v1/remove/contact/masking', data)];
  279. });
  280. });
  281. };
  282. WebSocketClient.prototype.stickingContact = function (data) {
  283. return __awaiter(this, void 0, void 0, function () {
  284. return __generator(this, function (_a) {
  285. return [2, this.request('/v1/add/contact/stick', data)];
  286. });
  287. });
  288. };
  289. WebSocketClient.prototype.removeStickContact = function (data) {
  290. return __awaiter(this, void 0, void 0, function () {
  291. return __generator(this, function (_a) {
  292. return [2, this.request('/v1/remove/contact/stick', data)];
  293. });
  294. });
  295. };
  296. WebSocketClient.prototype.setNoDisturbing = function (data) {
  297. return __awaiter(this, void 0, void 0, function () {
  298. return __generator(this, function (_a) {
  299. return [2, this.request('/v1/add/contact/no/disturbing', data)];
  300. });
  301. });
  302. };
  303. WebSocketClient.prototype.removeNoDisturbing = function (data) {
  304. return __awaiter(this, void 0, void 0, function () {
  305. return __generator(this, function (_a) {
  306. return [2, this.request('/v1/remove/contact/no/disturbing', data)];
  307. });
  308. });
  309. };
  310. WebSocketClient.prototype.agreeAddContact = function (data) {
  311. return __awaiter(this, void 0, void 0, function () {
  312. return __generator(this, function (_a) {
  313. return [2, this.request('/v1/add/contact/agree', data)];
  314. });
  315. });
  316. };
  317. WebSocketClient.prototype.rejectAddContact = function (data) {
  318. return __awaiter(this, void 0, void 0, function () {
  319. return __generator(this, function (_a) {
  320. return [2, this.request('/v1/add/contact/reject', data)];
  321. });
  322. });
  323. };
  324. WebSocketClient.prototype.addContactServiced = function (data) {
  325. return __awaiter(this, void 0, void 0, function () {
  326. return __generator(this, function (_a) {
  327. return [2, this.request('/v1/add/contact/serviced', data)];
  328. });
  329. });
  330. };
  331. WebSocketClient.prototype.createChatroom = function (data) {
  332. return __awaiter(this, void 0, void 0, function () {
  333. return __generator(this, function (_a) {
  334. return [2, this.request('/v1/create/chatroom', data)];
  335. });
  336. });
  337. };
  338. WebSocketClient.prototype.destroyChatroom = function (data) {
  339. return __awaiter(this, void 0, void 0, function () {
  340. return __generator(this, function (_a) {
  341. return [2, this.request('/v1/destroy/chatroom', data)];
  342. });
  343. });
  344. };
  345. WebSocketClient.prototype.leaveChatroom = function (data) {
  346. return __awaiter(this, void 0, void 0, function () {
  347. return __generator(this, function (_a) {
  348. return [2, this.request('/v1/leave/chatroom', data)];
  349. });
  350. });
  351. };
  352. WebSocketClient.prototype.getChatroomProfile = function (data) {
  353. return __awaiter(this, void 0, void 0, function () {
  354. return __generator(this, function (_a) {
  355. return [2, this.request('/v1/get/chatroom/profile', data)];
  356. });
  357. });
  358. };
  359. WebSocketClient.prototype.updateChatroomSubject = function (data) {
  360. return __awaiter(this, void 0, void 0, function () {
  361. return __generator(this, function (_a) {
  362. return [2, this.request('/v1/update/chatroom/subject', data)];
  363. });
  364. });
  365. };
  366. WebSocketClient.prototype.updateChatroomDescription = function (data) {
  367. return __awaiter(this, void 0, void 0, function () {
  368. return __generator(this, function (_a) {
  369. return [2, this.request('/v1/update/chatroom/description', data)];
  370. });
  371. });
  372. };
  373. WebSocketClient.prototype.addChatroomAdmin = function (data) {
  374. return __awaiter(this, void 0, void 0, function () {
  375. return __generator(this, function (_a) {
  376. return [2, this.request('/v1/add/chatroom/admin', data)];
  377. });
  378. });
  379. };
  380. WebSocketClient.prototype.removeChatroomAdmin = function (data) {
  381. return __awaiter(this, void 0, void 0, function () {
  382. return __generator(this, function (_a) {
  383. return [2, this.request('/v1/remove/chatroom/admin', data)];
  384. });
  385. });
  386. };
  387. WebSocketClient.prototype.addChatroomAdmins = function (data) {
  388. return __awaiter(this, void 0, void 0, function () {
  389. return __generator(this, function (_a) {
  390. return [2, this.request('/v1/add/chatroom/admins', data)];
  391. });
  392. });
  393. };
  394. WebSocketClient.prototype.removeChatroomAdmins = function (data) {
  395. return __awaiter(this, void 0, void 0, function () {
  396. return __generator(this, function (_a) {
  397. return [2, this.request('/v1/remove/chatroom/admins', data)];
  398. });
  399. });
  400. };
  401. WebSocketClient.prototype.joinChatroom = function (data) {
  402. return __awaiter(this, void 0, void 0, function () {
  403. return __generator(this, function (_a) {
  404. return [2, this.request('/v1/join/chatroom', data)];
  405. });
  406. });
  407. };
  408. WebSocketClient.prototype.removeChatroomMember = function (data) {
  409. return __awaiter(this, void 0, void 0, function () {
  410. return __generator(this, function (_a) {
  411. return [2, this.request('/v1/remove/chatroom/member', data)];
  412. });
  413. });
  414. };
  415. WebSocketClient.prototype.fetchStickChatroom = function (data) {
  416. return __awaiter(this, void 0, void 0, function () {
  417. return __generator(this, function (_a) {
  418. return [2, this.request('/v1/fetch/stick/chatroom', data)];
  419. });
  420. });
  421. };
  422. WebSocketClient.prototype.fetchChatroomMembers = function (data) {
  423. return __awaiter(this, void 0, void 0, function () {
  424. return __generator(this, function (_a) {
  425. return [2, this.request('/v1/chatroom/members', data)];
  426. });
  427. });
  428. };
  429. WebSocketClient.prototype.setConversationIsTop = function (data) {
  430. return __awaiter(this, void 0, void 0, function () {
  431. return __generator(this, function (_a) {
  432. return [2, this.request('/v1/set/conversation/is/top/request', data)];
  433. });
  434. });
  435. };
  436. WebSocketClient.prototype.searchHistoryMessage = function (data) {
  437. return __awaiter(this, void 0, void 0, function () {
  438. return __generator(this, function (_a) {
  439. return [2, this.request('/v1/search/history/message', data)];
  440. });
  441. });
  442. };
  443. WebSocketClient.prototype.startGroupChat = function (data) {
  444. return __awaiter(this, void 0, void 0, function () {
  445. return __generator(this, function (_a) {
  446. return [2, this.request('/v1/create/group/request', data)];
  447. });
  448. });
  449. };
  450. return WebSocketClient;
  451. }());
  452. exports.WebSocketClient = WebSocketClient;