Skip to content
Extraits de code Groupes Projets
Vérifiée Valider c0af840b rédigé par Kubat's avatar Kubat
Parcourir les fichiers

A working socket

parent afa31b87
Aucune branche associée trouvée
Aucune étiquette associée trouvée
2 requêtes de fusion!3Dev kubat,!1Create basic lib
const logger = require.main.require('./common/logger.js');
const net = require('net');
class LktClient {
/* The constructor
* - host: String
* - port: String
*
* Private members
* - m_socket: net.socket */
constructor(port = 6600) {
this.m_online = false;
this.m_port = port;
this.m_socket = new net.Socket();
const sockopt = {
port: this.m_port,
host: "localhost",
readable: true,
writable: true
};
logger.log("debug", "Creating the lektor client");
this.m_socket.setTimeout(10);
this.m_socket.setEncoding('utf8');
this.m_socket.on("timeout", () => {
logger.log("error", `Got timeout while connecting to localhost:${this.m_port}`);
this.m_socket.end();
});
this.m_socket.on("ready", () => {
logger.log("debug", `Ready to use socker with localhost:${this.m_port}`);
this.m_online = true;
});
this.m_socket.on('data', (data) => {
logger.log("debug", `Recieved "${data.replace(/(\r\n|\n|\r)/gm, "")}"`);
});
this.m_socket.on("end", () => {
logger.log("info", `Disconnected from server localhost:${this.m_port}`);
this.m_online = false;
});
this.m_socket.on("close", () => {
logger.log("info", `Socket localhost:${this.m_port} closed`);
this.m_online = false;
});
this.m_socket.connect(sockopt, () => {
logger.log("info", `Socket connected to localhost:${this.m_port}`);
this.m_online = true;
});
}
/* Wait for the socket to be connected */
wait() {
logger.log("debug", `Waiting to connect to localhost:${this.m_port}`);
var i = 0;
while (! this.m_online) {
}
}
/* Close the client */
close() {
logger.log("debug", "Requesting socket shutdown");
this.m_socket.destroy();
}
}
module.exports = LktClient;
const logger = require.main.require('./common/logger.js');
const lkt = require.main.require('./common/lkt.js');
var client = new lkt();
// client.wait();
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter