function connectSocket()

in src/cloudConsole/cloudConsoleLauncher.ts [260:318]


function connectSocket(ipcHandle: string, url: string) {

	const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || undefined;
	const ws = new WS(url, {
		// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
		agent: proxy && (url.startsWith('ws:') || url.startsWith('http:') ? new HttpProxyAgent(proxy) : new HttpsProxyAgent(proxy))
	});

	ws.on('open', function () {
		process.stdin.on('data', function (data) {
			ws.send(data);
		});
		startKeepAlive();
		sendData(ipcHandle, JSON.stringify([ { type: 'status', status: 'Connected' } ]))
			.catch(err => {
				console.error(err);
			});
	});

	ws.on('message', function (data) {
		process.stdout.write(String(data));
	});

	let error = false;
	ws.on('error', function (event) {
		error = true;
		console.error('Socket error: ' + JSON.stringify(event));
	});

	ws.on('close', function () {
		console.log('Socket closed');
		sendData(ipcHandle, JSON.stringify([ { type: 'status', status: 'Disconnected' } ]))
			.catch(err => {
				console.error(err);
			});
		if (!error) {
			process.exit(0);
		}
	});

	function startKeepAlive() {
		let isAlive = true;
		ws.on('pong', () => {
			isAlive = true;
		});
		const timer = setInterval(() => {
			if (isAlive === false) {
				error = true;
				console.log('Socket timeout');
				ws.terminate();
				clearInterval(timer);
			} else {
				isAlive = false;
				ws.ping();
			}
		}, 60000);
		timer.unref();
	}
}