Skip to content

Commit

Permalink
feat(builder): Add jwt support on websockets (#7734)
Browse files Browse the repository at this point in the history
add jwt support on websockets
  • Loading branch information
aarushik93 authored Aug 7, 2024
1 parent 1bad266 commit f8d07a2
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions rnd/autogpt_builder/src/lib/autogpt-server-api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,41 @@ export default class AutoGPTServerAPI {
return response_data;
}

connectWebSocket(): Promise<void> {
return new Promise((resolve, reject) => {
this.socket = new WebSocket(this.wsUrl);

this.socket.onopen = () => {
console.log("WebSocket connection established");
resolve();
};

this.socket.onclose = (event) => {
console.log("WebSocket connection closed", event);
this.socket = null;
};

this.socket.onerror = (error) => {
console.error("WebSocket error:", error);
async connectWebSocket(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
const token =
(await this.supabaseClient?.auth.getSession())?.data.session
?.access_token || "";

const wsUrlWithToken = `${this.wsUrl}?token=${token}`;
this.socket = new WebSocket(wsUrlWithToken);

this.socket.onopen = () => {
console.log("WebSocket connection established");
resolve();
};

this.socket.onclose = (event) => {
console.log("WebSocket connection closed", event);
this.socket = null;
};

this.socket.onerror = (error) => {
console.error("WebSocket error:", error);
reject(error);
};

this.socket.onmessage = (event) => {
const message = JSON.parse(event.data);
if (this.messageHandlers[message.method]) {
this.messageHandlers[message.method](message.data);
}
};
} catch (error) {
console.error("Error connecting to WebSocket:", error);
reject(error);
};

this.socket.onmessage = (event) => {
const message = JSON.parse(event.data);
if (this.messageHandlers[message.method]) {
this.messageHandlers[message.method](message.data);
}
};
}
});
}

Expand Down

0 comments on commit f8d07a2

Please sign in to comment.