Skip to content

Commit

Permalink
test message button
Browse files Browse the repository at this point in the history
  • Loading branch information
zoan37 committed Dec 12, 2024
1 parent c023d5b commit bdc5ac5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
41 changes: 40 additions & 1 deletion src/components/restreamTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ type Props = {
onTokensUpdate: (tokens: RestreamTokens | null) => void;
};

// Add this helper function at the top level, outside the component
const generateTestMessage = (): ChatMessage => {
const usernames = ['tester1', 'bot_user', 'mock_viewer', 'test_chat'];
const messages = [
'Hello there!',
'Testing 123',
'Great stream!',
'How are you today?',
'This is a test message',
'Just checking things out'
];

return {
username: usernames[Math.floor(Math.random() * usernames.length)],
displayName: 'Test User',
timestamp: Math.floor(Date.now() / 1000),
text: messages[Math.floor(Math.random() * messages.length)]
};
};

export const RestreamTokens: React.FC<Props> = ({ onTokensUpdate }) => {
const [jsonInput, setJsonInput] = useState('');
const [error, setError] = useState<string | null>(null);
Expand Down Expand Up @@ -122,6 +142,20 @@ export const RestreamTokens: React.FC<Props> = ({ onTokensUpdate }) => {
websocketService.disconnect();
};

// Modify sendTestMessage to match websocketService's handler format
const sendTestMessage = () => {
const testMessage = {
author: {
username: 'tester1',
displayName: 'Test User'
},
timestamp: Math.floor(Date.now() / 1000),
text: 'Test message ' + Math.random().toString(36).substring(7)
};

websocketService.handleChatMessage(testMessage);
};

return (
<div className="my-40">
<div className="my-16 typography-20 font-bold">Restream Integration</div>
Expand All @@ -145,9 +179,14 @@ export const RestreamTokens: React.FC<Props> = ({ onTokensUpdate }) => {
{isConnected ? 'Stop Listening' : 'Start Listening'}
</TextButton>
</div>
<div className="">
<div className="pr-8">
<TextButton onClick={handleClearTokens}>Clear Tokens</TextButton>
</div>
{isConnected && (
<div>
<TextButton onClick={sendTestMessage}>Send Test Message</TextButton>
</div>
)}
</div>

{/* Connection Status */}
Expand Down
4 changes: 2 additions & 2 deletions src/services/websocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ChatMessage {
text: string;
}

class WebSocketService extends EventEmitter {
export class WebSocketService extends EventEmitter {
private ws: WebSocket | null = null;
private accessToken: string | null = null;
private llmCallback: ((messages: string) => Promise<void>) | null = null;
Expand Down Expand Up @@ -75,7 +75,7 @@ class WebSocketService extends EventEmitter {
return this.ws?.readyState === WebSocket.OPEN;
}

private handleChatMessage(messageData: any) {
public handleChatMessage(messageData: any) {
const chatMessage: ChatMessage = {
username: messageData.author.username,
displayName: messageData.author.displayName,
Expand Down

0 comments on commit bdc5ac5

Please sign in to comment.