Files
clapper/clapper_src/webHelpers.js
Rafostar 8564cc9617 Move WebSocket message parsing to another file
Allows reusing the same code for the client app
2020-12-15 18:19:24 +01:00

31 lines
647 B
JavaScript

const { Soup } = imports.gi;
const ByteArray = imports.byteArray;
const Debug = imports.clapper_src.debug;
let { debug } = Debug;
function parseData(dataType, bytes)
{
if(dataType !== Soup.WebsocketDataType.TEXT) {
debug('ignoring non-text WebSocket message');
return [false];
}
let parsedMsg = null;
const msg = bytes.get_data();
try {
parsedMsg = JSON.parse(ByteArray.toString(msg));
}
catch(err) {
debug(err);
}
if(!parsedMsg || !parsedMsg.action) {
debug('no "action" in parsed WebSocket message');
return [false];
}
return [true, parsedMsg];
}