GoEasy.d.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { ImEventType } from "./im/events";
  2. import { ConversationType } from "./im/conversations";
  3. export interface GoEasyOptions {
  4. host: string;
  5. appkey: string;
  6. modules: Array<string>;
  7. forceTLS?: boolean;
  8. supportOldBrowser?: boolean;
  9. allowNotification?: boolean;
  10. }
  11. export interface CallBackOptions {
  12. onSuccess?(result?: any): void;
  13. onFailed?(errInfo?: any): void;
  14. }
  15. export interface ConnectOptions extends CallBackOptions {
  16. id?: string | number;
  17. data: string | any;
  18. otp?: string;
  19. onProgress?(progress: number): void;
  20. }
  21. export interface PresenceEvent<T> {
  22. action: string;
  23. time: number;
  24. data: T;
  25. id: string;
  26. }
  27. export interface PresenceEvents<T> {
  28. channel: string;
  29. clientAmount: number;
  30. events: PresenceEvent<T>[];
  31. userAmount: number;
  32. }
  33. export declare type IMEvent = keyof typeof ImEventType;
  34. export declare type MessageCallback = (message: any) => void;
  35. export interface PublishOptions extends CallBackOptions {
  36. channel: string;
  37. message: string;
  38. notification?: {
  39. title: string;
  40. body: string;
  41. };
  42. }
  43. export interface SubscribeOptions extends CallBackOptions {
  44. channel: string;
  45. onMessage(message: {
  46. channel: string;
  47. content: string;
  48. }): void;
  49. }
  50. export interface UnsubscribeOptions extends CallBackOptions {
  51. channel: string;
  52. }
  53. export interface SubscribePresenceOptions extends CallBackOptions {
  54. channel: string;
  55. onPresence<T = any>(presenceEvents: PresenceEvents<T>): void;
  56. }
  57. export interface UnsubscribePresenceOptions extends CallBackOptions {
  58. channel: string;
  59. }
  60. export interface HistoryOptions extends CallBackOptions {
  61. channel: string;
  62. limit: number;
  63. onSuccess(results: {
  64. code: number;
  65. content: {
  66. messages: Array<{
  67. time: number;
  68. content: string;
  69. }>;
  70. };
  71. }): void;
  72. }
  73. export interface HereNowOptions extends CallBackOptions {
  74. channels: string[];
  75. includeUsers?: boolean;
  76. distinct?: boolean;
  77. onSuccess(results: {
  78. code: number;
  79. content: {
  80. channels: {
  81. [key: string]: {
  82. channel: string;
  83. clientAmount: number;
  84. userAmount: number;
  85. users: any[];
  86. };
  87. };
  88. };
  89. }): void;
  90. }
  91. export interface HereNowByUserIdsOptions extends CallBackOptions {
  92. userIds: string[];
  93. distinct?: boolean;
  94. onSuccess(results: {
  95. code: number;
  96. content: Array<{
  97. id: string;
  98. data: any;
  99. }>;
  100. }): void;
  101. }
  102. export interface MessageTo {
  103. type: string;
  104. id: string;
  105. data: any;
  106. }
  107. export interface MessageOptions {
  108. to: MessageTo;
  109. notification?: {
  110. title: string;
  111. body: string;
  112. };
  113. onProgress?(progress: ProgressEvent<EventTarget>): void;
  114. }
  115. export interface TextMessageOptions extends MessageOptions {
  116. text: string;
  117. }
  118. export interface FileMessageOptions extends MessageOptions {
  119. file: File;
  120. }
  121. export interface CustomMessageOptions<T> extends MessageOptions {
  122. type: string;
  123. payload: T;
  124. }
  125. export interface LatestConversationsOptions extends CallBackOptions {
  126. onSuccess(result: {
  127. code: number;
  128. content: {
  129. unreadTotal: number;
  130. conversations: Conversation[];
  131. };
  132. }): void;
  133. }
  134. export interface RemovePrivateConversationOptions extends CallBackOptions {
  135. userId: string;
  136. }
  137. export interface RemoveGroupConversationOptions extends CallBackOptions {
  138. groupId: string;
  139. }
  140. export interface IMHistoryOptions extends CallBackOptions {
  141. userId?: string;
  142. groupId?: string;
  143. lastTimestamp?: number;
  144. limit?: number;
  145. }
  146. export interface UploadOptions extends CallBackOptions {
  147. file: File;
  148. name: string;
  149. onProgress(progress: ProgressEvent<EventTarget>): void;
  150. }
  151. export interface SendMessageOptions extends CallBackOptions {
  152. message: IMMessage;
  153. }
  154. export interface MarkGroupMessageAsReadOptions extends CallBackOptions {
  155. groupId: string;
  156. timestamp: number;
  157. }
  158. export interface MarkPrivateMessageAsReadOptions extends CallBackOptions {
  159. userId: string;
  160. timestamp: number;
  161. }
  162. export interface TopPrivateConversationOptions extends CallBackOptions {
  163. userId: string;
  164. top: boolean;
  165. }
  166. export interface TopGroupConversationOptions extends CallBackOptions {
  167. groupId: string;
  168. top: boolean;
  169. }
  170. export interface SubscribeUserPresenceOptions extends CallBackOptions {
  171. userIds: string[];
  172. }
  173. export interface UnsubscribeUserPresenceOptions extends CallBackOptions {
  174. userId: string;
  175. }
  176. export interface IMHereNowOptions extends CallBackOptions {
  177. userIds: string[];
  178. onSuccess(result: {
  179. code: number;
  180. content: Array<{
  181. userId: string;
  182. userData: string;
  183. }>;
  184. }): void;
  185. }
  186. export interface SubscribeGroupOptions extends CallBackOptions {
  187. groupIds: string;
  188. }
  189. export interface UnsubscribeGroupOptions extends CallBackOptions {
  190. groupId: string;
  191. }
  192. export interface SubscribeGroupPresenceOptions extends CallBackOptions {
  193. groupIds: string[];
  194. }
  195. export interface UnsubscribeGroupPresenceOptions extends CallBackOptions {
  196. groupId: string;
  197. }
  198. export interface GroupHereNowOptions extends CallBackOptions {
  199. groupId: string;
  200. onSuccess(result: {
  201. code: number;
  202. content: Array<{
  203. groupId: string;
  204. userData: string;
  205. }>;
  206. }): void;
  207. }
  208. export interface GroupOnlineCountOptions extends CallBackOptions {
  209. groupId: string;
  210. onSuccess(result: {
  211. code: number;
  212. content: {
  213. onlineCount: number;
  214. };
  215. }): void;
  216. }
  217. export declare enum MessageStatus {
  218. new = "new",
  219. sending = "sending",
  220. success = "success",
  221. fail = "fail"
  222. }
  223. export interface IMMessage<T = any> {
  224. type: string;
  225. senderData?: string;
  226. to?: MessageTo;
  227. timestamp?: number;
  228. senderId: string;
  229. payload: T;
  230. messageId: string;
  231. status: MessageStatus;
  232. }
  233. export interface Conversation {
  234. type: string;
  235. groupId: string;
  236. userId: string;
  237. data: string;
  238. unread: number;
  239. lastMessage: IMMessage<any>;
  240. }
  241. /**
  242. * @description PubSub 类
  243. */
  244. export declare class PubSub {
  245. static instance: any;
  246. private options;
  247. private goEasySocket;
  248. private publisher;
  249. private subscriber;
  250. private presence;
  251. private histories;
  252. private hereNows;
  253. private neverConnect;
  254. constructor(options: GoEasyOptions);
  255. initialGoEasySocket(goEasySocket: any): void;
  256. initialBeforeConnect(): void;
  257. private validateOptions;
  258. publish(options: PublishOptions): void;
  259. subscribe(options: SubscribeOptions): void;
  260. unsubscribe(options: UnsubscribeOptions): void;
  261. subscribePresence(options: SubscribePresenceOptions): void;
  262. unsubscribePresence(options: UnsubscribePresenceOptions): void;
  263. history(options: HistoryOptions): void;
  264. hereNow(options: HereNowOptions): void;
  265. hereNowByUserIds(options: HereNowByUserIdsOptions): void;
  266. }
  267. /**
  268. * @description IM 聊天类
  269. *
  270. * todo:要和IM。js合并,这边应该保持简单,复杂的应该在那边。
  271. */
  272. export declare class GoEasyIM {
  273. options: GoEasyOptions;
  274. constructor(options: GoEasyOptions);
  275. initialBeforeConnect(user: any): void;
  276. initialAfterConnect(): void;
  277. initialGoEasySocket(goEasySocket: any): void;
  278. private validateOptions;
  279. private validateMessageToData;
  280. on(event: IMEvent, fn: MessageCallback): void;
  281. createTextMessage(options: TextMessageOptions): IMMessage<string>;
  282. createImageMessage(options: FileMessageOptions): IMMessage<File>;
  283. createFileMessage(options: FileMessageOptions): IMMessage<File>;
  284. createAudioMessage(options: FileMessageOptions): IMMessage<File>;
  285. createVideoMessage(options: FileMessageOptions): IMMessage<File>;
  286. createCustomMessage<T = any>(options: CustomMessageOptions<T>): IMMessage<T>;
  287. latestConversations(options: LatestConversationsOptions): void;
  288. removePrivateConversation(options: RemovePrivateConversationOptions): void;
  289. removeGroupConversation(options: RemoveGroupConversationOptions): void;
  290. history(options: IMHistoryOptions): void;
  291. upload(options: UploadOptions): void;
  292. sendMessage(message: SendMessageOptions): void;
  293. markGroupMessageAsRead(options: MarkGroupMessageAsReadOptions): void;
  294. markPrivateMessageAsRead(options: MarkPrivateMessageAsReadOptions): void;
  295. topPrivateConversation(options: TopPrivateConversationOptions): void;
  296. topGroupConversation(options: TopGroupConversationOptions): void;
  297. subscribeUserPresence(options: SubscribeUserPresenceOptions): void;
  298. unsubscribeUserPresence(options: UnsubscribeUserPresenceOptions): void;
  299. hereNow(options: IMHereNowOptions): void;
  300. subscribeGroup(options: SubscribeGroupOptions): void;
  301. unsubscribeGroup(options: UnsubscribeGroupOptions): void;
  302. subscribeGroupPresence(options: SubscribeGroupPresenceOptions): void;
  303. unsubscribeGroupPresence(options: UnsubscribeGroupPresenceOptions): void;
  304. groupHereNow(options: GroupHereNowOptions): void;
  305. groupOnlineCount(options: GroupOnlineCountOptions): void;
  306. }
  307. /**
  308. * @description GoEasy 对外接口核心类
  309. */
  310. export default class GoEasy {
  311. static instance: GoEasy;
  312. static readonly version: string;
  313. static readonly IM_EVENT: typeof ImEventType;
  314. static readonly IM_SCENE: typeof ConversationType;
  315. readonly im: GoEasyIM;
  316. readonly pubsub: PubSub;
  317. private goEasySocket;
  318. private options;
  319. private notification;
  320. private constructor();
  321. static getInstance(options: GoEasyOptions): GoEasy;
  322. connect(options: ConnectOptions): void;
  323. disconnect(options: CallBackOptions): void;
  324. getConnectionStatus(): string;
  325. private validateOptions;
  326. onClickNotification(clickHandler: MessageCallback): void;
  327. private confirmUserId;
  328. }