@balte/emberplus-connection
Version:
Javascript implementation of the Ember+ automation protocol
51 lines (50 loc) • 1.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.S101Server = void 0;
const events_1 = require("events");
const net_1 = require("net");
const S101Socket_1 = __importDefault(require("./S101Socket"));
class S101Server extends events_1.EventEmitter {
constructor(port, address) {
super();
this.port = port;
this.address = address;
this.server = null;
this.status = 'disconnected';
}
addClient(socket) {
// Wrap the tcp socket into an S101Socket.
const client = new S101Socket_1.default(socket);
this.emit('connection', client);
}
listen() {
return new Promise((resolve, reject) => {
if (this.status !== 'disconnected') {
return reject(new Error('Already listening'));
}
this.server = net_1.createServer((socket) => {
this.addClient(socket);
})
.on('error', (e) => {
this.emit('error', e);
if (this.status === 'disconnected') {
return reject(e);
}
})
.on('listening', () => {
this.emit('listening');
this.status = 'listening';
resolve();
});
this.server.listen(this.port, this.address);
});
}
discard() {
var _a;
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close();
}
}
exports.S101Server = S101Server;