1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00

Merge pull request #10 from kkonradpl/socket-fix

Fix read of the TCP socket
This commit is contained in:
Marek Farkaš
2024-01-27 18:38:27 +01:00
committed by GitHub

View File

@@ -20,6 +20,7 @@ const { logDebug, logError, logInfo, logWarn } = consoleCmd;
let currentUsers = 0;
let streamEnabled = false;
let incompleteDataBuffer = '';
/* Audio Stream */
commandExists('ffmpeg')
@@ -119,13 +120,29 @@ client.connect(xdrdServerPort, xdrdServerHost, () => {
};
client.on('data', (data) => {
const receivedData = data.toString();
var receivedData = incompleteDataBuffer + data.toString();
const isIncomplete = (receivedData.slice(-1) != '\n');
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
dataHandler.handleData(client, receivedData);
if (isIncomplete) {
const position = receivedData.lastIndexOf('\n');
if (position < 0) {
incompleteDataBuffer = receivedData;
receivedData = '';
} else {
incompleteDataBuffer = receivedData.slice(position + 1);
receivedData = receivedData.slice(0, position + 1);
}
});
} else {
incompleteDataBuffer = '';
}
if (receivedData.length) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
dataHandler.handleData(client, receivedData);
}
});
}
});
client.on('data', authDataHandler);
@@ -168,4 +185,4 @@ httpServer.listen(webServerPort, webServerHost, () => {
/* Static data are being sent through here on connection - these don't change when the server is running */
app.get('/static_data', (req, res) => {
res.json({ qthLatitude, qthLongitude, webServerName, audioPort, streamEnabled});
});
});