Skip to content

Commit

Permalink
fix: Only disconnect and try reconnect when code isn't running (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carson-Shaar authored Oct 10, 2024
1 parent 13b5856 commit 26eefc3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions zt_frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@
@click="showAllComments"
></v-btn> -->
<ShareComponent v-if="$devMode && !isAppRoute" />
<a v-if="showCreateButton && (!$devMode || isAppRoute)" target="_blank" href="https://www.zero-true.com/contact">Create your own project</a>
<a
v-if="showCreateButton && (!$devMode || isAppRoute)"
target="_blank"
href="https://www.zero-true.com/contact"
>Create your own project</a
>
</div>
</v-col>
</template>
Expand Down Expand Up @@ -429,29 +434,36 @@ export default {
async connectSockets() {
this.notebook_socket = new WebSocketManager(this.ws_url + "ws/notebook", {
onMessage: (message: any) => this.notebookOnMessage(message),
isCodeRunning: () => this.isCodeRunning,
});
this.run_socket = new WebSocketManager(
this.$devMode
? this.ws_url + "ws/run_code"
: this.ws_url + "ws/component_run",
{
onMessage: (message: any) => this.runOnMessage(message),
isCodeRunning: () => this.isCodeRunning,
}
);
this.stop_socket = new WebSocketManager(
this.ws_url + "ws/stop_execution"
this.ws_url + "ws/stop_execution",
{
isCodeRunning: () => this.isCodeRunning,
}
);
await this.notebook_socket.initializeSocket();
await this.run_socket.initializeSocket();
await this.stop_socket.initializeSocket();
if (this.$devMode) {
this.save_socket = new WebSocketManager(this.ws_url + "ws/save_text", {
onMessage: (message: any) => this.saveOnMessage(message),
isCodeRunning: () => this.isCodeRunning,
});
this.dependency_socket = new WebSocketManager(
this.ws_url + "ws/dependency_update",
{
onMessage: (message: any) => this.dependencyOnMessage(message),
isCodeRunning: () => this.isCodeRunning,
}
);
await this.save_socket.initializeSocket();
Expand Down
5 changes: 4 additions & 1 deletion zt_frontend/src/websocket_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ type WebSocketOptions = {
onMessage?: (message: any) => void;
onClose?: (event: CloseEvent) => void;
onError?: (error: Event) => void;
isCodeRunning?: () => boolean;
pingInterval?: number;
reconnectDelay?: number;
};
Expand Down Expand Up @@ -70,7 +71,9 @@ export class WebSocketManager {
this.socket!.send(JSON.stringify({ type: "ping" }));
this.pingTimeout = window.setTimeout(() => {
console.log(`Ping timeout: ${this.url}`);
this.socket!.close();
if (!this.options.isCodeRunning || !this.options.isCodeRunning()) {
this.socket!.close();
}
}, 5000);
}
}, interval);
Expand Down

0 comments on commit 26eefc3

Please sign in to comment.