38 lines
No EOL
796 B
JavaScript
38 lines
No EOL
796 B
JavaScript
const { EVENT_DELIMITER } = require('../libs/constants.js');
|
|
|
|
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();
|
|
logger.debug('sending data to \'' + socket.remoteAddress + ':' + socket.remotePort + '\': ' + data);
|
|
await new Promise((resolve, reject) => {
|
|
socket.write(data + EVENT_DELIMITER, resolve);
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Message; |