Skip to content

Commit

Permalink
Merge pull request #34 from slkzgm/feature/debugMode
Browse files Browse the repository at this point in the history
feat: Add HlsRecordPlugin, Improve SttTtsPlugin, Logger for core components
  • Loading branch information
Freytes authored Dec 30, 2024
2 parents 28e0bdf + 89cb1f4 commit 8c07898
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 446 deletions.
104 changes: 3 additions & 101 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
"@sinclair/typebox": "^0.32.20",
"headers-polyfill": "^3.1.2",
"json-stable-stringify": "^1.0.2",
"node-fetch": "^3.3.2",
"otpauth": "^9.2.2",
"set-cookie-parser": "^2.6.0",
"tough-cookie": "^4.1.2",
"tslib": "^2.5.2",
"twitter-api-v2": "^1.18.2",
"undici": "^7.1.1",
"ws": "^8.18.0"
Expand All @@ -45,7 +43,7 @@
"@tsconfig/node16": "^16.1.3",
"@types/jest": "^29.5.1",
"@types/json-stable-stringify": "^1.0.34",
"@types/node": "^22.9.1",
"@types/node": "^22.10.2",
"@types/set-cookie-parser": "^2.4.2",
"@types/tough-cookie": "^4.0.2",
"@types/ws": "^8.5.13",
Expand Down
1 change: 1 addition & 0 deletions src/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export { SttTtsPlugin } from './spaces/plugins/SttTtsPlugin'
export { RecordToDiskPlugin } from './spaces/plugins/RecordToDiskPlugin'
export { MonitorAudioPlugin } from './spaces/plugins/MonitorAudioPlugin'
export { IdleMonitorPlugin } from './spaces/plugins/IdleMonitorPlugin'
export { HlsRecordPlugin } from './spaces/plugins/HlsRecordPlugin'

export * from './types/spaces'
46 changes: 29 additions & 17 deletions src/spaces/core/ChatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@
import WebSocket from 'ws';
import { EventEmitter } from 'events';
import type { SpeakerRequest, OccupancyUpdate } from '../types';
import { Logger } from '../logger';

interface ChatClientConfig {
spaceId: string;
accessToken: string;
endpoint: string;
logger: Logger;
}

export class ChatClient extends EventEmitter {
private ws?: WebSocket;
private connected = false;
private logger: Logger;
private readonly spaceId: string;
private readonly accessToken: string;
private endpoint: string;

constructor(
private readonly spaceId: string,
private readonly accessToken: string,
private readonly endpoint: string,
) {
constructor(config: ChatClientConfig) {
super();
this.spaceId = config.spaceId;
this.accessToken = config.accessToken;
this.endpoint = config.endpoint;
this.logger = config.logger;
}

async connect() {
const wsUrl = `${this.endpoint}/chatapi/v1/chatnow`.replace(
'https://',
'wss://',
);
console.log('[ChatClient] Connecting =>', wsUrl);
this.logger.info('[ChatClient] Connecting =>', wsUrl);

this.ws = new WebSocket(wsUrl, {
headers: {
Expand All @@ -34,43 +46,45 @@ export class ChatClient extends EventEmitter {
}

private setupHandlers(): Promise<void> {
if (!this.ws) throw new Error('No WebSocket instance');
if (!this.ws) {
throw new Error('No WebSocket instance');
}

return new Promise((resolve, reject) => {
this.ws!.on('open', () => {
console.log('[ChatClient] Connected');
this.logger.info('[ChatClient] Connected');
this.connected = true;
this.sendAuthAndJoin();
resolve();
});

this.ws!.on('message', (data: { toString: () => string; }) => {
this.ws!.on('message', (data: { toString: () => string }) => {
this.handleMessage(data.toString());
});

this.ws!.on('close', () => {
console.log('[ChatClient] Closed');
this.logger.info('[ChatClient] Closed');
this.connected = false;
this.emit('disconnected');
});

this.ws!.on('error', (err) => {
console.error('[ChatClient] Error =>', err);
this.logger.error('[ChatClient] Error =>', err);
reject(err);
});
});
}

private sendAuthAndJoin() {
if (!this.ws) return;
// Auth

this.ws.send(
JSON.stringify({
payload: JSON.stringify({ access_token: this.accessToken }),
kind: 3,
}),
);
// Join

this.ws.send(
JSON.stringify({
payload: JSON.stringify({
Expand Down Expand Up @@ -120,7 +134,6 @@ export class ChatClient extends EventEmitter {

const body = safeJson(payload.body);

// Example of speaker request detection
if (body.guestBroadcastingEvent === 1) {
const req: SpeakerRequest = {
userId: body.guestRemoteID,
Expand All @@ -131,7 +144,6 @@ export class ChatClient extends EventEmitter {
this.emit('speakerRequest', req);
}

// Example of occupancy update
if (typeof body.occupancy === 'number') {
const update: OccupancyUpdate = {
occupancy: body.occupancy,
Expand All @@ -140,7 +152,6 @@ export class ChatClient extends EventEmitter {
this.emit('occupancyUpdate', update);
}

// Example of mute state
if (body.guestBroadcastingEvent === 16) {
this.emit('muteStateChanged', {
userId: body.guestRemoteID,
Expand All @@ -155,7 +166,7 @@ export class ChatClient extends EventEmitter {
}
// Example of guest reaction
if (body?.type === 2) {
console.log('[ChatClient] Emiting guest reaction event =>', body);
this.logger.info('[ChatClient] Emitting guest reaction event =>', body);
this.emit('guestReaction', {
displayName: body.displayName,
emoji: body.body,
Expand All @@ -165,6 +176,7 @@ export class ChatClient extends EventEmitter {

async disconnect() {
if (this.ws) {
this.logger.info('[ChatClient] Disconnecting...');
this.ws.close();
this.ws = undefined;
this.connected = false;
Expand Down
Loading

0 comments on commit 8c07898

Please sign in to comment.