+20
-11
@@ -69,10 +69,7 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
||||
return connecting;
|
||||
}
|
||||
|
||||
if (socket && currentOrigin !== origin) {
|
||||
socket.close();
|
||||
socket = null;
|
||||
}
|
||||
if (socket && currentOrigin !== origin) closeClient();
|
||||
|
||||
connecting = new Promise<WebSocket>((resolve, reject) => {
|
||||
const ws = new WebSocket(origin);
|
||||
@@ -88,7 +85,7 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
||||
currentOrigin = origin;
|
||||
connecting = null;
|
||||
|
||||
await sendUnsafe(useLocalhost, {
|
||||
await sendUnsafe({
|
||||
type: "auth",
|
||||
cookies: authCookies,
|
||||
});
|
||||
@@ -133,6 +130,10 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
||||
currentOrigin = null;
|
||||
connecting = null;
|
||||
};
|
||||
|
||||
ws.on("ping", () => {
|
||||
ws.pong();
|
||||
});
|
||||
});
|
||||
|
||||
return connecting;
|
||||
@@ -142,10 +143,7 @@ async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
||||
RPC helpers
|
||||
────────────────────────── */
|
||||
|
||||
async function sendUnsafe<T>(
|
||||
useLocalhost: boolean,
|
||||
payload: Record<string, any>,
|
||||
): Promise<T> {
|
||||
async function sendUnsafe<T>(payload: Record<string, any>): Promise<T> {
|
||||
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||
throw new Error("WebSocket is not open");
|
||||
}
|
||||
@@ -160,7 +158,11 @@ async function sendUnsafe<T>(
|
||||
|
||||
pending.set(requestId, { resolve, reject, timeout });
|
||||
|
||||
socket?.send(JSON.stringify({ ...payload, requestId }));
|
||||
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||
throw new Error("WebSocket closed while sending");
|
||||
}
|
||||
|
||||
socket.send(JSON.stringify({ ...payload, requestId }));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -169,13 +171,20 @@ async function send<T>(
|
||||
payload: Record<string, any>,
|
||||
): Promise<T> {
|
||||
await ensureSocket(useLocalhost);
|
||||
return sendUnsafe(useLocalhost, payload);
|
||||
return sendUnsafe(payload);
|
||||
}
|
||||
|
||||
/* ──────────────────────────
|
||||
Public API
|
||||
────────────────────────── */
|
||||
|
||||
export async function closeClient() {
|
||||
if (!socket) return;
|
||||
socket.close();
|
||||
socket = null;
|
||||
connecting = null;
|
||||
}
|
||||
|
||||
export async function addRun(
|
||||
useLocalhost: boolean,
|
||||
problem: string,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { startServer, stopServer } from "./server";
|
||||
import os from "os";
|
||||
import { exec } from "child_process";
|
||||
import net from "net";
|
||||
import { closeClient } from "./client";
|
||||
|
||||
var globalStatusBarItem: vscode.StatusBarItem | undefined;
|
||||
|
||||
@@ -105,6 +106,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
export function deactivate() {
|
||||
stopServer(globalStatusBarItem as any);
|
||||
closeClient();
|
||||
}
|
||||
|
||||
export function canConnect(
|
||||
|
||||
+1
-1
@@ -129,7 +129,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
|
||||
moveId: moveId++,
|
||||
changes: {
|
||||
from: 0,
|
||||
to: serverText.length,
|
||||
to: serverText?.length || 0,
|
||||
insert: editorText,
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user