import { io } from "socket.io-client";
class MeetingClient {
private socket: Socket;
connect(displayName: string, token?: string, userId?: string) {
this.socket = io("https://api.neuronmeet.com", {
auth: {
token,
userId,
displayName,
},
transports: ["websocket", "polling"],
reconnection: true,
});
this.setupEventHandlers();
return this.socket;
}
private setupEventHandlers() {
// Connection events
this.socket.on("connect", () => {
console.log("Connected with socket ID:", this.socket.id);
});
this.socket.on("disconnect", (reason) => {
console.log("Disconnected:", reason);
});
}
joinRoom(roomCode: string, userId?: string, displayName: string) {
return new Promise((resolve, reject) => {
this.socket.emit("join-room", {
roomCode,
userId,
displayName,
});
// Wait for confirmation
this.socket.once("room-joined", (data) => {
resolve(data);
});
this.socket.once("join-error", (error) => {
reject(new Error(error.message));
});
// Timeout
setTimeout(() => reject(new Error("Join timeout")), 15000);
});
}
disconnect() {
this.socket.disconnect();
}
}
// Usage
const client = new MeetingClient();
client.connect("John Doe");
const roomData = await client.joinRoom("ABC123", undefined, "John Doe");
console.log("Joined room:", roomData);