Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 1aa44c6

Browse files
author
ItsNik
committed
Fix: Websocket logic adjustments
1 parent ad79836 commit 1aa44c6

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

src/handlers/graph.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ async function renderGraphToImage(
7777
}
7878
}
7979

80-
throw new Error(`Graph rendering failed: ${errorMessage}`);
80+
throw new Error(`Graph rendering failed - ${errorMessage}`);
8181
} finally {
8282
if (browser) {
8383
await browser.close().catch(() => { });
8484
}
8585
}
8686

87-
logger.info(`Graph rendered and image saved to: ${outputImagePath}`);
87+
logger.info(`Graph rendered and image saved to - ${outputImagePath}`);
8888
}
8989

9090
async function generateGraphFiles(

src/utils/webSocket.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function setupWebSocket(server: Server) {
1212
logger.debug(`Received upgrade request for URL: ${req.url}`);
1313
const baseURL = `http://${req.headers.host}/`;
1414
const requestURL = new URL(req.url || '', baseURL);
15-
const {pathname} = requestURL;
15+
const { pathname } = requestURL;
1616
logger.debug(`Parsed pathname: ${pathname}`);
1717

1818
// Debug log to verify path handling
@@ -38,7 +38,7 @@ export function setupWebSocket(server: Server) {
3838
wss.on('connection', (ws: WebSocket, req) => {
3939
const baseURL = `http://${req.headers.host}/`;
4040
const requestURL = new URL(req.url || '', baseURL);
41-
const {pathname} = requestURL;
41+
const { pathname } = requestURL;
4242

4343
logger.info(`WebSocket connection established to ${pathname}`);
4444

@@ -83,27 +83,22 @@ export function setupWebSocket(server: Server) {
8383
ws.send(JSON.stringify({ type: 'log-history', data: history }));
8484

8585
// Watch the log file for changes
86-
const watcher = fs.watch(logPath, (eventType) => {
87-
if (eventType === 'change') {
88-
const newSize = fs.statSync(logPath).size;
89-
if (newSize > lastSize) {
90-
const stream = fs.createReadStream(logPath, {
91-
start: lastSize,
92-
end: newSize - 1,
93-
encoding: 'utf-8'
94-
});
95-
96-
stream.on('data', (chunk) => {
97-
ws.send(JSON.stringify({ type: 'log-update', data: chunk }));
98-
});
99-
100-
lastSize = newSize;
101-
}
86+
const watcher = fs.watchFile(logPath, { interval: 1000 }, (curr, prev) => {
87+
if (curr.size > prev.size) {
88+
const stream = fs.createReadStream(logPath, {
89+
start: prev.size,
90+
end: curr.size - 1,
91+
encoding: 'utf-8'
92+
});
93+
94+
stream.on('data', (chunk) => {
95+
ws.send(JSON.stringify({ type: 'log-update', data: chunk }));
96+
});
10297
}
10398
});
10499

105100
ws.on('close', () => {
106-
watcher.close();
101+
watcher.removeAllListeners();
107102
logger.info('Closed WebSocket connection for logs');
108103
});
109104
} else {

0 commit comments

Comments
 (0)