123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- import { ContactMsg, ContactAddMsg, ChatroomMsg } from './messages';
- import { Client } from 'ts-linker-sdk';
- import { getMetaData } from './meta';
-
- export interface WebSocketClientStatus {
- created: boolean;
- connected: boolean;
- login: boolean;
- }
-
- export interface WebSocketResp {
- value: object | string;
- }
-
- export type ListenerCallback = (data: WebSocketResp) => void;
-
- const meta = getMetaData();
- const PING_INTERVAL = 50 * 1000;
-
- export class WebSocketClient {
- client: Client;
- interval: number;
-
- wsUrl: string;
- token: string;
- sid: string;
- chatToken: object;
-
- status: WebSocketClientStatus = {
- created: false,
- connected: false,
- login: false
- };
-
- /** 默认构造函数 */
- constructor(wsUrl: string, token: string, sid: string, chatToken: object) {
- this.wsUrl = wsUrl;
- this.token = token;
- this.chatToken = chatToken;
- this.sid = sid;
- }
-
- async init() {
- return this.create();
- }
-
- getStatus() {
- return this.status;
- }
-
- async create() {
- const url = this.wsUrl;
-
- return new Promise((resolve, reject) => {
- if (!url) {
- throw new Error('websocket url is required.');
- }
- this.client = Client.getInstance(url, {
- onOpen: async () => {
- try {
- this.ping();
- this.interval = setInterval(() => {
- this.ping();
- }, PING_INTERVAL) as any;
- } catch (err) {
- console.error(err);
- reject(err);
- }
-
- const data = await this.connect();
- await this.authentication();
- resolve(data.value);
- },
- onError: () => {
- resolve(false);
- },
- onClose: () => {
- clearInterval(this.interval);
- }
- });
- });
- }
-
- on(url: string, callback: (data: WebSocketResp) => void) {
- if (!url) {
- throw new Error('url is required.');
- }
-
- this.client.addMessageListener(url, callback);
- }
-
- off(url: string) {
- this.client.removeMessageListener(url);
- }
-
- async request(url: string, data: object) {
- return this.client.request(url, data);
- }
-
- async connect() {
- const { sid } = this;
- if (sid) {
- this.client.setRequestProperty('sid', sid);
- }
-
- const data = await this.request('/v1/session/start', {
- ...this.chatToken,
- ...meta
- });
-
- if (data && data.value) {
- const id = data.value as string;
- this.sid = id;
- this.client.setRequestProperty('sid', id);
- }
- return data || true;
- }
-
- ping() {
- return this.client.ping({});
- }
-
- async authentication() {
- const tk = this.token;
- const data = await this.request('/v1/session/bind/uid/by/token', { token: tk });
- return data || true;
- }
-
- async onMessage(callback: ListenerCallback) {
- this.on('/v1/message/listener', callback);
- }
-
- async sendMessage(data: object) {
- return this.request('/v1/send/message', data);
- }
-
- async markServiced(data: { msg_id: string }) {
- return this.request('/v1/mark/message/serviced', data);
- }
-
- /** @start Apis */
-
- /** 获取聊天室历史消息 */
- async getHistoryMessage(data: {
- contact_id: string;
- chat_type: string;
- start_time: number;
- limit: number;
- }) {
- return this.request('/v1/history/message', data);
- }
-
- /** 修改在线状态 */
- async updateStatus(data: { status: 'on' | 'off' | 'busy' }) {
- return this.request('/v1/session/status', data);
- }
-
- /** 获取当前登陆用户的设备信息 */
- async getSessionLists() {
- return this.request('/v1/session/lists', {});
- }
-
- /** 增量获取所有会话信息 */
- async getAllConversations(data: {
- last_pull: number; // 最后一次获取的时间 0的话是全量获取
- }) {
- return this.request('/v1/get/all/conversation', data);
- }
-
- /** 获取联系人列表 */
- async getAllContact(data: {
- last_pull: number; // 最后一次获取的时间 0的话是全量获取
- }) {
- return this.request('/v1/get/all/contact', data);
- }
-
- /** 添加联系人 */
- async addContact(data: { to_add_username: string; reason: string }) {
- return this.request('/v1/add/contact', data);
- }
-
- /** 删除联系人 */
- async deleteContact(data: ContactMsg) {
- return this.request('/v1/delete/contact', data);
- }
-
- /** 屏蔽联系人 */
- async maskingContact(data: ContactMsg) {
- return this.request('/v1/add/contact/masking', data);
- }
-
- /** 取消联系人 */
- async removeMaskingContact(data: ContactMsg) {
- return this.request('/v1/remove/contact/masking', data);
- }
-
- /** 置顶联系人 */
- async stickingContact(data: ContactMsg) {
- return this.request('/v1/add/contact/stick', data);
- }
-
- /** 解除置顶联系人 */
- async removeStickContact(data: ContactMsg) {
- return this.request('/v1/remove/contact/stick', data);
- }
-
- /** 消息免打扰 */
- async setNoDisturbing(data: ContactMsg) {
- return this.request('/v1/add/contact/no/disturbing', data);
- }
-
- /** 取消消息免打扰 */
- async removeNoDisturbing(data: ContactMsg) {
- return this.request('/v1/remove/contact/no/disturbing', data);
- }
-
- /** 同意添加好友 */
- async agreeAddContact(data: ContactAddMsg) {
- return this.request('/v1/add/contact/agree', data);
- }
-
- /** 拒绝添加好友 */
- async rejectAddContact(data: ContactAddMsg) {
- return this.request('/v1/add/contact/reject', data);
- }
-
- /** 标记好友申请已发送 */
- async addContactServiced(data: ContactAddMsg) {
- return this.request('/v1/add/contact/serviced', data);
- }
-
- /** 创建聊天室 */
- async createChatroom(data: {
- subject: string;
- description: string;
- welcome_message: string;
- max: number;
- }) {
- return this.request('/v1/create/chatroom', data);
- }
-
- /** 销毁聊天室 */
- async destroyChatroom(data: ChatroomMsg) {
- return this.request('/v1/destroy/chatroom', data);
- }
-
- /** 离开聊天室 */
- async leaveChatroom(data: ChatroomMsg) {
- return this.request('/v1/leave/chatroom', data);
- }
-
- /** 获取聊天室详情 */
- async getChatroomProfile(data: ChatroomMsg) {
- return this.request('/v1/get/chatroom/profile', data);
- }
-
- /** 修改聊天室名称 */
- async updateChatroomSubject(data: ChatroomMsg) {
- return this.request('/v1/update/chatroom/subject', data);
- }
-
- /** 修改聊天室描述信息 */
- async updateChatroomDescription(data: ChatroomMsg) {
- return this.request('/v1/update/chatroom/description', data);
- }
-
- /** 添加管理员 */
- async addChatroomAdmin(data: ChatroomMsg) {
- return this.request('/v1/add/chatroom/admin', data);
- }
-
- /** 移除管理员 */
- async removeChatroomAdmin(data: ChatroomMsg) {
- return this.request('/v1/remove/chatroom/admin', data);
- }
-
- /** 批量添加管理员 */
- async addChatroomAdmins(data: ChatroomMsg) {
- return this.request('/v1/add/chatroom/admins', data);
- }
-
- /** 批量移除管理员 */
- async removeChatroomAdmins(data: ChatroomMsg) {
- return this.request('/v1/remove/chatroom/admins', data);
- }
-
- /** 加入聊天室 */
- async joinChatroom(data: {}) {
- return this.request('/v1/join/chatroom', data);
- }
-
- /** 删除聊天室成员 */
- async removeChatroomMember(data: ChatroomMsg) {
- return this.request('/v1/remove/chatroom/member', data);
- }
-
- /** 获取置顶聊天室列表 */
- async fetchStickChatroom(data: { cache_time: number }) {
- return this.request('/v1/fetch/stick/chatroom', data);
- }
-
- /** 获取聊天室成员 */
- async fetchChatroomMembers(data: ChatroomMsg) {
- return this.request('/v1/fetch/chatroom/members', data);
- }
-
- /** 搜索聊天室历史消息 */
- async searchHistoryMessage(data: {
- contact_id: string;
- chat_type: string;
- keyword: string;
- limit: number;
- }) {
- return this.request('/v1/search/history/message', data);
- }
- }
|