mirror of https://github.com/msgbyte/tailchat
feat: add TailchatWsClient and update tailchat-types
parent
22f08c98e4
commit
924f644d49
@ -0,0 +1,3 @@
|
|||||||
|
import { TailchatBaseClient } from './base';
|
||||||
|
|
||||||
|
export class TailchatHTTPClient extends TailchatBaseClient {}
|
@ -0,0 +1,8 @@
|
|||||||
|
export {
|
||||||
|
/**
|
||||||
|
* @deprecated please rename to TailchatHTTPClient
|
||||||
|
*/
|
||||||
|
TailchatHTTPClient as TailchatClient,
|
||||||
|
TailchatHTTPClient,
|
||||||
|
} from './http';
|
||||||
|
export { TailchatWsClient } from './ws';
|
@ -0,0 +1,103 @@
|
|||||||
|
import { TailchatBaseClient } from './base';
|
||||||
|
import io, { Socket } from 'socket.io-client';
|
||||||
|
import * as msgpackParser from 'socket.io-msgpack-parser';
|
||||||
|
import type { ChatMessage } from 'tailchat-types';
|
||||||
|
|
||||||
|
export class TailchatWsClient extends TailchatBaseClient {
|
||||||
|
public socket: Socket | null = null;
|
||||||
|
|
||||||
|
connect(): Promise<Socket> {
|
||||||
|
return new Promise<Socket>(async (resolve, reject) => {
|
||||||
|
await this.waitingForLogin();
|
||||||
|
|
||||||
|
const token = this.jwt;
|
||||||
|
|
||||||
|
const socket = (this.socket = io(this.url, {
|
||||||
|
transports: ['websocket'],
|
||||||
|
auth: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
forceNew: true,
|
||||||
|
parser: msgpackParser,
|
||||||
|
}));
|
||||||
|
|
||||||
|
socket.once('connect', () => {
|
||||||
|
// 连接成功
|
||||||
|
this.emit('chat.converse.findAndJoinRoom')
|
||||||
|
.then((res) => {
|
||||||
|
console.log('Joined rooms', res.data);
|
||||||
|
resolve(socket);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
socket.once('error', () => {
|
||||||
|
reject();
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('disconnect', (reason) => {
|
||||||
|
console.log(`disconnect due to ${reason}`);
|
||||||
|
this.socket = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.onAny((ev) => {
|
||||||
|
console.log('onAny', ev);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect() {
|
||||||
|
if (!this.socket) {
|
||||||
|
console.warn('You should call it after connect');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socket.disconnect();
|
||||||
|
this.socket = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(eventName: string, eventData: any = {}) {
|
||||||
|
if (!this.socket) {
|
||||||
|
console.warn('You should call it after connect');
|
||||||
|
throw new Error('You should call it after connect');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.socket.emitWithAck(eventName, eventData);
|
||||||
|
}
|
||||||
|
|
||||||
|
on(eventName: string, callback: (payload: any) => void) {
|
||||||
|
if (!this.socket) {
|
||||||
|
console.warn('You should call it after connect');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socket.on(eventName, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
once(eventName: string, callback: (payload: any) => void) {
|
||||||
|
if (!this.socket) {
|
||||||
|
console.warn('You should call it after connect');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socket.once(eventName, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
off(eventName: string, callback: (payload: any) => void) {
|
||||||
|
if (!this.socket) {
|
||||||
|
console.warn('You should call it after connect');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socket.off(eventName, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessage(callback: (messagePayload: ChatMessage) => void) {
|
||||||
|
this.on('notify:chat.message.add', callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessageUpdate(callback: (messagePayload: ChatMessage) => void) {
|
||||||
|
this.on('notify:chat.message.update', callback);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
import { TailchatWsClient } from '../src';
|
||||||
|
|
||||||
|
const HOST = process.env.HOST;
|
||||||
|
const APPID = process.env.APPID;
|
||||||
|
const APPSECRET = process.env.APPSECRET;
|
||||||
|
|
||||||
|
if (!HOST || !APPID || !APPSECRET) {
|
||||||
|
console.log('require env: HOST, APPID, APPSECRET');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = new TailchatWsClient(HOST, APPID, APPSECRET);
|
||||||
|
|
||||||
|
client.connect().then(async () => {
|
||||||
|
console.log('Login Success!');
|
||||||
|
|
||||||
|
client.onMessage((message) => {
|
||||||
|
console.log('Receive message', message);
|
||||||
|
});
|
||||||
|
});
|
@ -1,2 +1,3 @@
|
|||||||
export * from './model/inbox';
|
export * from './model/inbox';
|
||||||
export * from './model/user';
|
export * from './model/user';
|
||||||
|
export * from './model/message';
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
export interface ChatMessageReaction {
|
||||||
|
name: string;
|
||||||
|
author: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ChatMessage {
|
||||||
|
_id: string;
|
||||||
|
|
||||||
|
content: string;
|
||||||
|
|
||||||
|
author?: string;
|
||||||
|
|
||||||
|
groupId?: string;
|
||||||
|
|
||||||
|
converseId: string;
|
||||||
|
|
||||||
|
reactions?: ChatMessageReaction[];
|
||||||
|
|
||||||
|
hasRecall?: boolean;
|
||||||
|
|
||||||
|
meta?: Record<string, unknown>;
|
||||||
|
|
||||||
|
createdAt?: string;
|
||||||
|
|
||||||
|
updatedAt?: string;
|
||||||
|
}
|
Loading…
Reference in New Issue