diff --git a/src/lib/cloud-provider.js b/src/lib/cloud-provider.js index 8116813ebd17c98549b3fc55565944747f19054d..ce189e60d44c3eec400df1dbd870a7c3e41623eb 100644 --- a/src/lib/cloud-provider.js +++ b/src/lib/cloud-provider.js @@ -51,8 +51,10 @@ class CloudProvider { log.info(`Received websocket message: ${messageString}`); // Multiple commands can be received, newline separated messageString.split('\n').forEach(message => { - const parsedData = this.parseMessage(JSON.parse(message)); - this.vm.postIOData('cloud', parsedData); + if (message) { // .split can also contain '' in the array it returns + const parsedData = this.parseMessage(JSON.parse(message)); + this.vm.postIOData('cloud', parsedData); + } }); } diff --git a/test/unit/util/cloud-provider.test.js b/test/unit/util/cloud-provider.test.js index c81588ddeccad21e2e7e9116f9ae9d5ce7f081a2..4d247d3fd4c8fee1cb7f54c56c346f887e9d1fa6 100644 --- a/test/unit/util/cloud-provider.test.js +++ b/test/unit/util/cloud-provider.test.js @@ -70,6 +70,16 @@ describe('CloudProvider', () => { expect(vmIOData[0].varUpdate.value).toEqual('value'); }); + test('onMessage with newline at the end', () => { + const msg1 = JSON.stringify({ + method: 'set', + name: 'name1', + value: 'value' + }); + cloudProvider.onMessage({data: `${msg1}\n`}); + expect(vmIOData[0].varUpdate.name).toEqual('name1'); + }); + test('onMessage with multiple commands', () => { const msg1 = JSON.stringify({ method: 'set',