kannon-client/classes/Message.js

36 lines
744 B
JavaScript
Raw Permalink Normal View History

2022-04-14 14:25:48 +02:00
class Message {
constructor(id, data) {
this.id = id;
this.data = data;
}
getId() {
return this.id;
}
getData() {
return this.data;
}
toString() {
return JSON.stringify(this);
}
async send(socket) {
if (socket === undefined) {
socket = connection.socket;
}
if (socket === undefined) {
return;
}
const data = this.toString();
2022-05-03 16:17:08 +02:00
logger.debug('sending data to \'' + socket.remoteAddress + ':' + socket.remotePort + '\': ' + data);
2022-04-14 14:25:48 +02:00
await new Promise((resolve, reject) => {
2022-04-21 13:40:00 +02:00
socket.write(data + constants.EVENT_DELIMITER, resolve);
2022-04-14 14:25:48 +02:00
});
}
}
module.exports = Message;