You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.4 KiB
48 lines
1.4 KiB
7 months ago
|
const net = require('net');
|
||
|
|
||
|
|
||
|
const getConnection = (connName, host, port) => {
|
||
|
const client = net.connect({port: port, host: host}, function () {
|
||
|
console.log(connName + ' Connected: ');
|
||
|
console.log(' local = %s:%s', this.localAddress, this.localPort);
|
||
|
console.log(' remote = %s:%s', this.remoteAddress, this.remotePort);
|
||
|
this.setTimeout(1000);
|
||
|
this.setEncoding('utf8');
|
||
|
this.on('data', function (data) {
|
||
|
console.log(connName + " From Server: " + data.toString());
|
||
|
// this.end();
|
||
|
});
|
||
|
this.on('end', function () {
|
||
|
console.log(connName + ' Client disconnected');
|
||
|
});
|
||
|
this.on('error', function (err) {
|
||
|
console.log('Socket Error: ', err, JSON.stringify(err));
|
||
|
});
|
||
|
this.on('timeout', function () {
|
||
|
console.log('Socket Timed Out');
|
||
|
});
|
||
|
this.on('close', function (e) {
|
||
|
console.log('Socket Closed', e);
|
||
|
});
|
||
|
});
|
||
|
return client;
|
||
|
}
|
||
|
|
||
|
const writeData = (socket, data) => {
|
||
|
const success = !!socket.write(data);
|
||
|
// if (success) {
|
||
|
// (function (socket, data) {
|
||
|
// socket.once('drain', function () {
|
||
|
// writeData(socket, data);
|
||
|
// });
|
||
|
// })(socket, data);
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
// const Dwarves = getConnection("Dwarves");
|
||
|
// writeData(Dwarves, JSON.stringify(sampleData));
|
||
|
|
||
|
|
||
|
|
||
|
module.exports = {getConnection, writeData}
|