No Description

WebSocketClient.ts 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { Client } from 'ts-linker-sdk';
  2. import { ContactMsg, ContactAddMsg, ChatroomMsg } from './messages';
  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. async messageReaded(data: { msg_id: string }){
  116. return this.request('/v1/mark/messages/read', data);
  117. }
  118. /** @start Apis */
  119. /** 获取聊天室历史消息 */
  120. async getHistoryMessage(data: {
  121. contact_id: string;
  122. chat_type: string;
  123. start_time: number;
  124. limit: number;
  125. }) {
  126. return this.request('/v1/history/message', data);
  127. }
  128. /** 修改在线状态 */
  129. async updateStatus(data: { status: 'on' | 'off' | 'busy' }) {
  130. return this.request('/v1/session/status', data);
  131. }
  132. /** 获取当前登陆用户的设备信息 */
  133. async getSessionLists() {
  134. return this.request('/v1/session/lists', {});
  135. }
  136. /** 增量获取所有会话信息 */
  137. async getAllConversations(data: {
  138. last_pull: number; // 最后一次获取的时间 0的话是全量获取
  139. }) {
  140. return this.request('/v1/get/all/conversation', data);
  141. }
  142. /** 获取联系人列表 */
  143. async getAllContact(data: {
  144. last_pull: number; // 最后一次获取的时间 0的话是全量获取
  145. }) {
  146. return this.request('/v1/get/all/contact', data);
  147. }
  148. /** 添加联系人 */
  149. async addContact(data: { to_add_username: string; reason: string }) {
  150. return this.request('/v1/add/contact', data);
  151. }
  152. /** 删除联系人 */
  153. async deleteContact(data: ContactMsg) {
  154. return this.request('/v1/delete/contact', data);
  155. }
  156. /** 屏蔽联系人 */
  157. async maskingContact(data: ContactMsg) {
  158. return this.request('/v1/add/contact/masking', data);
  159. }
  160. /** 取消联系人 */
  161. async removeMaskingContact(data: ContactMsg) {
  162. return this.request('/v1/remove/contact/masking', data);
  163. }
  164. /** 置顶联系人 */
  165. async stickingContact(data: ContactMsg) {
  166. return this.request('/v1/add/contact/stick', data);
  167. }
  168. /** 解除置顶联系人 */
  169. async removeStickContact(data: ContactMsg) {
  170. return this.request('/v1/remove/contact/stick', data);
  171. }
  172. /** 消息免打扰 */
  173. async setNoDisturbing(data: ContactMsg) {
  174. return this.request('/v1/add/contact/no/disturbing', data);
  175. }
  176. /** 取消消息免打扰 */
  177. async removeNoDisturbing(data: ContactMsg) {
  178. return this.request('/v1/remove/contact/no/disturbing', data);
  179. }
  180. /** 同意添加好友 */
  181. async agreeAddContact(data: ContactAddMsg) {
  182. return this.request('/v1/add/contact/agree', data);
  183. }
  184. /** 拒绝添加好友 */
  185. async rejectAddContact(data: ContactAddMsg) {
  186. return this.request('/v1/add/contact/reject', data);
  187. }
  188. /** 标记好友申请已发送 */
  189. async addContactServiced(data: ContactAddMsg) {
  190. return this.request('/v1/add/contact/serviced', data);
  191. }
  192. /** 创建聊天室 */
  193. async createChatroom(data: {
  194. subject: string;
  195. description: string;
  196. welcome_message: string;
  197. max: number;
  198. }) {
  199. return this.request('/v1/create/chatroom', data);
  200. }
  201. /** 销毁聊天室 */
  202. async destroyChatroom(data: ChatroomMsg) {
  203. return this.request('/v1/destroy/chatroom', data);
  204. }
  205. /** 离开聊天室 */
  206. async leaveChatroom(data: ChatroomMsg) {
  207. return this.request('/v1/leave/chatroom', data);
  208. }
  209. /** 获取聊天室详情 */
  210. async getChatroomProfile(data: ChatroomMsg) {
  211. return this.request('/v1/get/chatroom/profile', data);
  212. }
  213. /** 修改聊天室名称 */
  214. async updateChatroomSubject(data: ChatroomMsg) {
  215. return this.request('/v1/update/chatroom/subject', data);
  216. }
  217. /** 修改聊天室描述信息 */
  218. async updateChatroomDescription(data: ChatroomMsg) {
  219. return this.request('/v1/update/chatroom/description', data);
  220. }
  221. /** 添加管理员 */
  222. async addChatroomAdmin(data: ChatroomMsg) {
  223. return this.request('/v1/add/chatroom/admin', data);
  224. }
  225. /** 移除管理员 */
  226. async removeChatroomAdmin(data: ChatroomMsg) {
  227. return this.request('/v1/remove/chatroom/admin', data);
  228. }
  229. /** 批量添加管理员 */
  230. async addChatroomAdmins(data: ChatroomMsg) {
  231. return this.request('/v1/add/chatroom/admins', data);
  232. }
  233. /** 批量移除管理员 */
  234. async removeChatroomAdmins(data: ChatroomMsg) {
  235. return this.request('/v1/remove/chatroom/admins', data);
  236. }
  237. /** 加入聊天室 */
  238. async joinChatroom(data: {}) {
  239. return this.request('/v1/join/chatroom', data);
  240. }
  241. /** 删除聊天室成员 */
  242. async removeChatroomMember(data: ChatroomMsg) {
  243. return this.request('/v1/remove/chatroom/member', data);
  244. }
  245. /** 获取置顶聊天室列表 */
  246. async fetchStickChatroom(data: { cache_time: number }) {
  247. return this.request('/v1/fetch/stick/chatroom', data);
  248. }
  249. /** 获取聊天室成员 */
  250. async fetchChatroomMembers(data: ChatroomMsg) {
  251. return this.request('/v1/fetch/chatroom/members', data);
  252. }
  253. /** 搜索聊天室历史消息 */
  254. async searchHistoryMessage(data: {
  255. contact_id: string;
  256. chat_type: string;
  257. keyword: string;
  258. limit: number;
  259. }) {
  260. return this.request('/v1/search/history/message', data);
  261. }
  262. }