Files
VoiceAgent/example/ws-server.ts
Bijit Mondal 77bac597e4 init:
2026-02-13 17:33:22 +05:30

44 lines
1.2 KiB
TypeScript

import "dotenv/config";
import { WebSocketServer } from "ws";
const endpoint = process.env.VOICE_WS_ENDPOINT || "ws://localhost:8080";
const url = new URL(endpoint);
const port = Number(url.port || 8080);
const host = url.hostname || "localhost";
const wss = new WebSocketServer({ port, host });
wss.on("listening", () => {
console.log(`[ws-server] listening on ${endpoint}`);
});
wss.on("connection", (socket) => {
console.log("[ws-server] client connected");
// Send a sample transcript to test text pipeline end-to-end.
setTimeout(() => {
socket.send(
JSON.stringify({
type: "transcript",
text: "What is the weather in Berlin?",
}),
);
}, 500);
socket.on("message", (data) => {
try {
const msg = JSON.parse(data.toString()) as {
type?: string;
text?: string;
};
console.log("[ws-server] <-", msg);
} catch {
console.log("[ws-server] <- raw", data.toString());
}
});
socket.on("close", () => {
console.log("[ws-server] client disconnected");
});
});