Sélectionner une révision Git
lkt.js 17,25 Kio
const logger = require.main.require('./common/logger.js'),
net = require('net'),
{ finished } = require('stream'),
{ cli } = require('winston/lib/winston/config');
var config = require.main.require('./common/config.js');
class LktClient {
/***************************************
* Not static methods, do not use them *
***************************************/
/* The constructor
* - host: String
* - port: String
*
* Private members
* - m_socket: net.socket
*
* Node: You should not use that directly, prefere the use of static
* methods */
constructor() {
this.m_online = false;
this.m_socket = new net.Socket();
this.m_closed = true;
const sockopt = {
port: config.content.lektord.port,
host: config.content.lektord.host,
readable: true,
writable: true,
};
logger.debug('lkt', 'Creating the lektor client');
this.m_socket.setTimeout(3000);
this.m_socket.setEncoding('utf8');
this.m_socket.on('timeout', () => {
logger.error('lkt', `Got timeout while connecting to localhost:${config.content.lektord.port}`);
this.m_socket.end();
});
this.m_socket.on('ready', () => {
logger.debug('lkt', `Ready to use socket with localhost:${config.content.lektord.port}`);
this.m_online = true;
});
this.m_socket.on('end', () => {
logger.info('lkt', `Disconnected from server localhost:${config.content.lektord.port}`);
this.m_online = false;
});
this.m_socket.on('error', err => {
logger.error('lkt', `${err}`);
if (this.m_online) {
this.m_socket.destroy();
this.m_online = false;
}
});
this.m_socket.on('close', () => {
logger.info('lkt', `Socket localhost:${config.content.lektord.port} closed`);
this.m_online = false;
});
this.m_socket.connect(sockopt, () => {
logger.info('lkt', `Socket connected to localhost:${config.content.lektord.port}`);
this.m_online = true;
});
return this;
}