mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-07-30 16:59:15 +02:00
Merge pull request #10 from kkonradpl/socket-fix
Fix read of the TCP socket
This commit is contained in:
@@ -20,6 +20,7 @@ const { logDebug, logError, logInfo, logWarn } = consoleCmd;
|
|||||||
|
|
||||||
let currentUsers = 0;
|
let currentUsers = 0;
|
||||||
let streamEnabled = false;
|
let streamEnabled = false;
|
||||||
|
let incompleteDataBuffer = '';
|
||||||
|
|
||||||
/* Audio Stream */
|
/* Audio Stream */
|
||||||
commandExists('ffmpeg')
|
commandExists('ffmpeg')
|
||||||
@@ -119,13 +120,29 @@ client.connect(xdrdServerPort, xdrdServerHost, () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
client.on('data', (data) => {
|
client.on('data', (data) => {
|
||||||
const receivedData = data.toString();
|
var receivedData = incompleteDataBuffer + data.toString();
|
||||||
|
const isIncomplete = (receivedData.slice(-1) != '\n');
|
||||||
|
|
||||||
wss.clients.forEach((client) => {
|
if (isIncomplete) {
|
||||||
if (client.readyState === WebSocket.OPEN) {
|
const position = receivedData.lastIndexOf('\n');
|
||||||
dataHandler.handleData(client, receivedData);
|
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);
|
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 */
|
/* Static data are being sent through here on connection - these don't change when the server is running */
|
||||||
app.get('/static_data', (req, res) => {
|
app.get('/static_data', (req, res) => {
|
||||||
res.json({ qthLatitude, qthLongitude, webServerName, audioPort, streamEnabled});
|
res.json({ qthLatitude, qthLongitude, webServerName, audioPort, streamEnabled});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user