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