Skip to content

Commit

Permalink
disconnect method for WebRTC
Browse files Browse the repository at this point in the history
  • Loading branch information
davorinrusevljan committed Feb 5, 2025
1 parent d070a72 commit 86bae56
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/webRTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ interface AG2Message {
export class WebRTC {
private ag2SocketUrl: string;
private microphone?: MediaStreamTrack;
private ws: WebSocket | null;
private pc: RTCPeerConnection | undefined;

constructor(ag2SocketUrl: string, microphone?: MediaStreamTrack) {
this.ag2SocketUrl = ag2SocketUrl;
this.microphone = microphone;
this.ws = null;
}

async disconnect(): Promise<void> {
if (this.ws) {
this.ws.close();
}
if (this.pc) {
this.pc.close();
}
}

async connect(): Promise<void> {
let ws: WebSocket;
const pc = new RTCPeerConnection();
let dc: RTCDataChannel | null = null; // data connection
const quedMessages: string[] = []; // queue messages from the server before the data connection is open
let resolve: () => void, reject: (reason?: any) => void;
Expand All @@ -32,8 +42,12 @@ export class WebRTC {
reject = _reject;
});

this.pc = new RTCPeerConnection();

async function openRTC(
init_message: AG2InitMessage,
pc: RTCPeerConnection,
ws: WebSocket,
mic: MediaStreamTrack,
resolve: () => void,
reject: (reason?: any) => void,
Expand Down Expand Up @@ -118,20 +132,22 @@ export class WebRTC {
microphone.enabled = false;
}

ws = new WebSocket(this.ag2SocketUrl);
this.ws = new WebSocket(this.ag2SocketUrl);

ws.onopen = (event) => {
this.ws.onopen = (event) => {
console.log('web socket opened');
};

ws.onmessage = async (event) => {
this.ws.onmessage = async (event) => {
try {
const message: AG2Message = JSON.parse(event.data);
console.info('Received Message from AG2 backend', message);
const type = message.type;
if (type === 'ag2.init') {
await openRTC(
message as AG2InitMessage,
this.pc as RTCPeerConnection,
this.ws as WebSocket,
this.microphone as MediaStreamTrack,
resolve,
reject,
Expand Down

0 comments on commit 86bae56

Please sign in to comment.