Skip to content

Commit

Permalink
Merge pull request #32 from erc1337-Coffee/spaces_reactions
Browse files Browse the repository at this point in the history
working reactions
  • Loading branch information
lalalune authored Dec 29, 2024
2 parents d8600df + c1d62b2 commit 33e89c4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/spaces/core/ChatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ export class ChatClient extends EventEmitter {
);
}

reactWithEmoji(emoji: string) {
if (!this.ws) return;
const payload = JSON.stringify({
body: JSON.stringify({ body: emoji, type: 2, v: 2 }),
kind: 1,
/*
// The 'sender' field is not required, it's not even verified by the server
// Instead of passing attributes down here it's easier to ignore it
sender: {
user_id: null,
twitter_id: null,
username: null,
display_name: null,
},
*/
payload: JSON.stringify({
room: this.spaceId,
body: JSON.stringify({ body: emoji, type: 2, v: 2 }),
}),
type: 2,
});
this.ws.send(payload);
}

private handleMessage(raw: string) {
let msg: any;
try {
Expand Down Expand Up @@ -129,6 +153,14 @@ export class ChatClient extends EventEmitter {
muted: false,
});
}
// Example of guest reaction
if (body?.type === 2) {
console.log('[ChatClient] Emiting guest reaction event =>', body);
this.emit('guestReaction', {
displayName: body.displayName,
emoji: body.body,
});
}
}

async disconnect() {
Expand Down
10 changes: 10 additions & 0 deletions src/spaces/core/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
BroadcastCreated,
SpeakerRequest,
OccupancyUpdate,
GuestReaction,
Plugin,
AudioDataWithUser,
PluginRegistration,
Expand Down Expand Up @@ -162,6 +163,11 @@ export class Space extends EventEmitter {
return broadcast;
}

reactWithEmoji(emoji: string) {
if (!this.chatClient) return;
this.chatClient.reactWithEmoji(emoji);
}

private setupChatEvents() {
if (!this.chatClient) return;

Expand All @@ -175,6 +181,10 @@ export class Space extends EventEmitter {
this.chatClient.on('muteStateChanged', (evt) => {
this.emit('muteStateChanged', evt);
});
this.chatClient.on('guestReaction', (reaction: GuestReaction) => {
console.log('[Space] Guest reaction =>', reaction);
this.emit('guestReaction', reaction);
});
}

/**
Expand Down
51 changes: 51 additions & 0 deletions src/spaces/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,61 @@ async function main() {
}, 10_000);
});

// When a user react, reply with some reactions to test the flow
space.on('guestReaction', (evt) => {
// Pick a random emoji from the list
const emojis = ['💯', '✨', '🙏', '🎮'];
const emoji = emojis[Math.floor(Math.random() * emojis.length)];
space.reactWithEmoji(emoji);
});

space.on('error', (err) => {
console.error('[Test] Space Error =>', err);
});

// ==================================================
// BEEP GENERATION (500 ms) @16kHz => 8000 samples
// ==================================================
const beepDurationMs = 500;
const sampleRate = 16000;
const totalSamples = (sampleRate * beepDurationMs) / 1000; // 8000
const beepFull = new Int16Array(totalSamples);

// Sine wave: 440Hz, amplitude ~12000
const freq = 440;
const amplitude = 12000;
for (let i = 0; i < beepFull.length; i++) {
const t = i / sampleRate;
beepFull[i] = amplitude * Math.sin(2 * Math.PI * freq * t);
}

const FRAME_SIZE = 160;
/**
* Send a beep by slicing beepFull into frames of 160 samples
*/
async function sendBeep() {
console.log('[Test] Starting beep...');
for (let offset = 0; offset < beepFull.length; offset += FRAME_SIZE) {
// subarray => simple "view"
const portion = beepFull.subarray(offset, offset + FRAME_SIZE);

// Make a real copy
const frame = new Int16Array(FRAME_SIZE);
frame.set(portion);

// Now frame.length = 160, and frame.byteLength = 320
space.pushAudio(frame, sampleRate);

await new Promise((r) => setTimeout(r, 10));
}
console.log('[Test] Finished beep');
}

// 5) Send beep every 5s
//setInterval(() => {
// sendBeep().catch((err) => console.error('[Test] beep error =>', err));
//}, 5000);

console.log('[Test] Space is running... press Ctrl+C to exit.');

// Graceful shutdown
Expand Down
5 changes: 5 additions & 0 deletions src/spaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export interface OccupancyUpdate {
totalParticipants: number;
}

export interface GuestReaction {
displayName: string;
emoji: string;
}

export interface SpaceConfig {
mode: 'BROADCAST' | 'LISTEN' | 'INTERACTIVE';
title?: string;
Expand Down

0 comments on commit 33e89c4

Please sign in to comment.