No Description

WebSocketClient.ts 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import { Client } from 'ts-linker-sdk';
  2. import { getMetaData } from './meta';
  3. export interface WebSocketClientStatus {
  4. created: boolean;
  5. connected: boolean;
  6. login: boolean;
  7. }
  8. export interface WebSocketResp {
  9. value: object | string;
  10. }
  11. export type ListenerCallback = (data: WebSocketResp) => void;
  12. const meta = getMetaData();
  13. const PING_INTERVAL = 50 * 1000;
  14. export class WebSocketClient {
  15. client: Client;
  16. interval: number;
  17. wsUrl: string;
  18. token: string;
  19. sid: string;
  20. chatToken: object;
  21. status: WebSocketClientStatus = {
  22. created: false,
  23. connected: false,
  24. login: false
  25. };
  26. /** 默认构造函数 */
  27. constructor(wsUrl: string, token: string, sid: string, chatToken: object) {
  28. this.wsUrl = wsUrl;
  29. this.token = token;
  30. this.chatToken = chatToken;
  31. this.sid = sid;
  32. }
  33. async init() {
  34. return this.create();
  35. }
  36. getStatus() {
  37. return this.status;
  38. }
  39. async create() {
  40. const url = this.wsUrl;
  41. return new Promise((resolve, reject) => {
  42. if (!url) {
  43. throw new Error('websocket url is required.');
  44. }
  45. this.client = Client.getInstance(url, {
  46. onOpen: async () => {
  47. try {
  48. this.ping();
  49. this.interval = setInterval(() => {
  50. this.ping();
  51. }, PING_INTERVAL) as any;
  52. } catch (err) {
  53. console.error(err);
  54. reject(err);
  55. }
  56. const data = await this.connect();
  57. await this.authentication();
  58. resolve(data.value);
  59. },
  60. onError: () => {
  61. resolve(false);
  62. },
  63. onClose: () => {
  64. clearInterval(this.interval);
  65. }
  66. });
  67. });
  68. }
  69. on(url: string, callback: (data: WebSocketResp) => void) {
  70. if (!url) {
  71. throw new Error('url is required.');
  72. }
  73. this.client.addMessageListener(url, callback);
  74. }
  75. off(url: string) {
  76. this.client.removeMessageListener(url);
  77. }
  78. async request(url: string, data: object) {
  79. return this.client.request(url, data);
  80. }
  81. async connect() {
  82. const { sid } = this;
  83. if (sid) {
  84. this.client.setRequestProperty('sid', sid);
  85. }
  86. const data = await this.request('/v1/session/start', {
  87. ...this.chatToken,
  88. ...meta
  89. });
  90. if (data && data.value) {
  91. const id = data.value as string;
  92. this.sid = id;
  93. this.client.setRequestProperty('sid', id);
  94. }
  95. return data || true;
  96. }
  97. ping() {
  98. return this.client.ping({});
  99. }
  100. async authentication() {
  101. const tk = this.token;
  102. const data = await this.request('/v1/session/bind/uid/by/token', { token: tk });
  103. return data || true;
  104. }
  105. async onMessage(callback: ListenerCallback) {
  106. this.on('/v1/message/listener', callback);
  107. }
  108. async sendMessage(data: object) {
  109. return this.request('/v1/send/message', data);
  110. }
  111. async markServiced(data: { msg_id: string }) {
  112. return this.request('/v1/mark/message/serviced', data);
  113. }
  114. /** @start Apis */
  115. /** 获取聊天室历史消息 */
  116. async getHistoryMessage(data: {
  117. contact_id: string;
  118. chat_type: string;
  119. start_time: number;
  120. limit: number;
  121. }) {
  122. return this.request('/v1/history/message', data);
  123. }
  124. /** 修改在线状态 */
  125. async updateStatus(data: { status: 'on' | 'off' | 'busy' }) {
  126. return this.request('/v1/session/status', data);
  127. }
  128. /** 获取当前登陆用户的设备信息 */
  129. async getSessionLists() {
  130. return this.request('/v1/session/lists', {});
  131. }
  132. /** 增量获取所有会话信息 */
  133. async getAllConversations(data: {
  134. last_pull: number; // 最后一次获取的时间 0的话是全量获取
  135. }) {
  136. return this.request('/v1/get/all/conversation', data);
  137. }
  138. /** 获取联系人列表 */
  139. async getAllContact(data: {
  140. last_pull: number; // 最后一次获取的时间 0的话是全量获取
  141. }) {
  142. return this.request('/v1/get/all/contact', data);
  143. }
  144. /** 添加联系人 */
  145. async addContact(data: { to_add_username: string; reason: string }) {
  146. return this.request('/v1/add/contact', data);
  147. }
  148. /** 删除联系人 */
  149. async deleteContact(data: { fid: string }) {
  150. return this.request('/v1/delete/contact', data);
  151. }
  152. /** 屏蔽联系人 */
  153. async maskingContact(data: { fid: string }) {
  154. return this.request('/v1/add/contact/masking', data);
  155. }
  156. /** 取消联系人 */
  157. async removeMaskingContact(data: { fid: string }) {
  158. return this.request('/v1/remove/contact/masking', data);
  159. }
  160. /** 置顶联系人 */
  161. async stickingContact(data: { fid: string }) {
  162. return this.request('/v1/add/contact/stick', data);
  163. }
  164. /** 解除置顶联系人 */
  165. async removeStickContact(data: { fid: string }) {
  166. return this.request('/v1/remove/contact/stick', data);
  167. }
  168. /** 消息免打扰 */
  169. async setNoDisturbing(data: { fid: string }) {
  170. return this.request('/v1/add/contact/no/disturbing', data);
  171. }
  172. /** 取消消息免打扰 */
  173. async removeNoDisturbing(data: { fid: string }) {
  174. return this.request('/v1/remove/contact/no/disturbing', data);
  175. }
  176. /** 同意添加好友 */
  177. async agreeAddContact(data: { id: string; to: string }) {
  178. return this.request('/v1/add/contact/agree', data);
  179. }
  180. /** 拒绝添加好友 */
  181. async rejectAddContact(data: { id: string; to: string }) {
  182. return this.request('/v1/add/contact/reject', data);
  183. }
  184. /** 标记好友申请已发送 */
  185. async addContactServiced(data: { id: string; to: string }) {
  186. return this.request('/v1/add/contact/serviced', data);
  187. }
  188. /** 创建聊天室 */
  189. async createChatroom(data: {
  190. subject: string;
  191. description: string;
  192. welcome_message: string;
  193. max: number;
  194. }) {
  195. return this.request('/v1/create/chatroom', data);
  196. }
  197. /** 销毁聊天室 */
  198. async destroyChatroom(data: { room_id: string }) {
  199. return this.request('/v1/destroy/chatroom', data);
  200. }
  201. /** 离开聊天室 */
  202. async leaveChatroom(data: { room_id: string }) {
  203. return this.request('/v1/leave/chatroom', data);
  204. }
  205. /** 获取聊天室详情 */
  206. async getChatroomProfile(data: { room_id: string }) {
  207. return this.request('/v1/get/chatroom/profile', data);
  208. }
  209. /** 修改聊天室名称 */
  210. async updateChatroomSubject(data: { room_id: string }) {
  211. return this.request('/v1/update/chatroom/subject', data);
  212. }
  213. /** 修改聊天室描述信息 */
  214. async updateChatroomDescription(data: { room_id: string }) {
  215. return this.request('/v1/update/chatroom/description', data);
  216. }
  217. /** 添加管理员 */
  218. async addChatroomAdmin(data: { room_id: string; admin_id: string }) {
  219. return this.request('/v1/add/chatroom/admin', data);
  220. }
  221. /** 移除管理员 */
  222. async removeChatroomAdmin(data: { room_id: string; admin_id: string }) {
  223. return this.request('/v1/remove/chatroom/admin', data);
  224. }
  225. /** 批量添加管理员 */
  226. async addChatroomAdmins(data: { room_id: string; admins_id: string[] }) {
  227. return this.request('/v1/add/chatroom/admins', data);
  228. }
  229. /** 批量移除管理员 */
  230. async removeChatroomAdmins(data: { room_id: string; admins_id: string[] }) {
  231. return this.request('/v1/remove/chatroom/admins', data);
  232. }
  233. /** 加入聊天室 */
  234. async joinChatroom(data: {}) {
  235. return this.request('/v1/join/chatroom', data);
  236. }
  237. /** 删除聊天室成员 */
  238. async removeChatroomMember(data: { room_id: string; member: string }) {
  239. return this.request('/v1/remove/chatroom/member', data);
  240. }
  241. /** 获取置顶聊天室列表 */
  242. async fetchStickChatroom(data: { cache_time: number }) {
  243. return this.request('/v1/fetch/stick/chatroom', data);
  244. }
  245. /** 获取聊天室成员 */
  246. async fetchChatroomMembers(data: { room_id: string; authority: number }) {
  247. return this.request('/v1/fetch/chatroom/members', data);
  248. }
  249. /** 搜索聊天室历史消息 */
  250. async searchHistoryMessage(data: {
  251. contact_id: string;
  252. chat_type: string;
  253. keyword: string;
  254. limit: number;
  255. }) {
  256. return this.request('/v1/search/history/message', data);
  257. }
  258. }