-
-
Notifications
You must be signed in to change notification settings - Fork 43
Open
Description
Hi, thanks for making this. It looks like you create a new websocket for each call of _send. It also looks like there is a 1 second delay for some reason. What if you reused the websocket like this?
_send(command, done, eventHandle) {
let self = this;
if (self.ws) {
self.ws.send(JSON.stringify(command));
return;
}
const ws = new WebSocket(this.WS_URL, {rejectUnauthorized: false});
ws.on('close', function () {
self.ws = null;
})
ws.on('open', () => {
if (this.PORT === 8001) {
setTimeout(() => ws.send(JSON.stringify(command)), 500);
} else {
ws.send(JSON.stringify(command));
}
});
ws.on('message', (message) => {
const data = JSON.parse(message);
this.LOGGER.log('data: ', JSON.stringify(data, null, 2), 'ws.on message');
if (done && (data.event === command.params.event || data.event === eventHandle)) {
this.LOGGER.log('if correct event', JSON.stringify(data, null, 2), 'ws.on message');
done(null, data);
}
if (data.event !== 'ms.channel.connect') {
this.LOGGER.log('if not correct event', JSON.stringify(data, null, 2), 'ws.on message');
ws.close();
}
});
ws.on('response', (response) => {
this.LOGGER.log('response', response, 'ws.on response');
});
ws.on('error', (err) => {
let errorMsg = '';
if (err.code === 'EHOSTUNREACH' || err.code === 'ECONNREFUSED') {
errorMsg = 'TV is off or unavailable';
}
console.error(errorMsg);
this.LOGGER.error(errorMsg, err, 'ws.on error');
if (done) {
done(err, null);
}
});
self.ws = ws;
}
Metadata
Metadata
Assignees
Labels
No labels