Fixed handling of messages larger than 1024 bytes

Fixes https://github.com/parnic/node-screenlogic/issues/5
This commit is contained in:
2019-03-25 20:02:58 -05:00
parent 398a687cc6
commit 08c5457f2c
2 changed files with 19 additions and 1 deletions

View File

@ -124,8 +124,22 @@ class UnitConnection extends EventEmitter {
this.password = password;
this.client = new net.Socket();
var _this = this;
var buffer = Buffer.alloc(1024);
var bufferIdx = 0;
this.client.on('data', function(msg) {
_this.onClientMessage(msg);
if (buffer.length < msg.length + bufferIdx) {
buffer = Buffer.alloc(msg.length + buffer.length, buffer);
}
msg.copy(buffer, bufferIdx);
bufferIdx = bufferIdx + msg.length;
var expectedLen = msg.readInt32LE(4) + 8;
if (msg.length === expectedLen) {
_this.onClientMessage(buffer);
bufferIdx = 0;
}
}).on('close', function(had_error) {
// console.log('unit connection closed');
});