No Description

WebSocketClient.ts 7.6KB

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