Whole Packet Protocol Communication Library in Node

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












I have built this small class as a small library to be able to talk to a server that uses CapeSoft's NetTalk library as socket library, and which uses the Whole Packet Protocol, which is a simple protocol made by CapeSoft for the communication between NetTalk Apps. However, I need to talk to this "REST" Server to get some Info for my Node App, and seeing that this Server does not work with Regular HTTP Protocol, I had to write this library myself.



I wish to know if there is any way (which I'm sure there is) to better refactor this small class and improve the comprehension of it as well as it's comments in JavaScript.



I am trying to improve my refactoring and clean coding skills. I want to be able to write the shortest code while still being simple to understand.



I would appreciate much your help to improve this. I really wish to become top programmer.



Following is my class module; any critique is welcome and well accepted.



const tls = require('tls');
const fs = require('fs');


/**
*
*
* NetTalk Class:
* Creates a NetTalk Object for making request to a NetTalk Service in WPP (Whole Packet Protocol).
*
*/
class NetTalk
/**
* Creates an instance of NetTalk.
* @param Number port Port through which you wish to connect to NetTalk.
* @param String server Server to which to connect.
* @param String certificate Route where the certificate file (*.pem) is located
* @param String [verify = '0'] verify the certificate? (default="0")
* @memberof NetTalk
*/
constructor (port, server, certificate, verify = "0")
this.port = port;
this.server = server;
this.options = ca:[ fs.readFileSync(certificate) ], rejectUnauthorized: false;
this.msgSize = 0;
this.wholePackage = '';
this.socket = null;
this.verify = verify;


/**
*
* Connects to the NetTalk Service.
* @memberof NetTalk
*/
connect()
this.socket = tls.connect(this.port, this.server, this.options, () =>
process.stdin.pipe(this.socket);
process.stdin.resume();
);


this.socket.setTimeout(12000)
this.socket.setEncoding('ascii');


this.socket.on('end', () =>
console.log('Connection terminated.');
);


this.socket.on('timeout', ()=>
console.log('Connection timed out.');
this.socket.destroy();
);


return new Promise((resolve, reject) =>
this.socket.on('ready', () =>
console.log(`client connected on port $this.port to server $this.server`);
resolve();
);

this.socket.on('error', (err) =>
reject(err:err, func:'connect');
);
);


/**
* sends the request.
*
* @param String reqMsg Request Message to be sent to the service.
* @returns Promise Promise.<resolve(data)
* @memberof NetTalk
*/
request(reqMsg)
return new Promise((resolve, reject) =>
/*Adding 32 bit length Integer to the beginning of stream
for compatibility with WPP (Whole Packet Protocol).*/
var message = new Buffer(reqMsg, 'binary');
var stream = new Buffer(4 + message.length); //declares the stream to be sent.

stream.writeInt32LE(message.length, 0); //Must be in LittleEndian for compatibility.
message.copy(stream, 4);

this.socket.write(stream);

this.socket.on('data', (data) =>
let wholePackageSize = null;

this.wholePackage += data;
wholePackageSize = new Buffer(this.wholePackage, 'binary');
this.msgSize += data.length;

if (wholePackageSize.readInt32LE(0) <= this.msgSize)
this.socket.end();
resolve(this.wholePackage.slice(4, this.wholePackage.length));

);

this.socket.on('timeout', ()=>
this.socket.destroy();
reject(err:'Error!, server do not respond', func:'request')
);

this.socket.on('error', err =>
reject(err:err, func:'request');
)
);



module.exports = NetTalk;


I add the link to the GitHub project as well.







share|improve this question





















  • Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.".
    – Sam Onela
    Jun 27 at 20:03










  • Thank you so much @SamOnela for your answer I will edit it right now
    – Enoc Martinez
    Jun 27 at 20:05
















up vote
2
down vote

favorite
1












I have built this small class as a small library to be able to talk to a server that uses CapeSoft's NetTalk library as socket library, and which uses the Whole Packet Protocol, which is a simple protocol made by CapeSoft for the communication between NetTalk Apps. However, I need to talk to this "REST" Server to get some Info for my Node App, and seeing that this Server does not work with Regular HTTP Protocol, I had to write this library myself.



I wish to know if there is any way (which I'm sure there is) to better refactor this small class and improve the comprehension of it as well as it's comments in JavaScript.



I am trying to improve my refactoring and clean coding skills. I want to be able to write the shortest code while still being simple to understand.



I would appreciate much your help to improve this. I really wish to become top programmer.



Following is my class module; any critique is welcome and well accepted.



const tls = require('tls');
const fs = require('fs');


/**
*
*
* NetTalk Class:
* Creates a NetTalk Object for making request to a NetTalk Service in WPP (Whole Packet Protocol).
*
*/
class NetTalk
/**
* Creates an instance of NetTalk.
* @param Number port Port through which you wish to connect to NetTalk.
* @param String server Server to which to connect.
* @param String certificate Route where the certificate file (*.pem) is located
* @param String [verify = '0'] verify the certificate? (default="0")
* @memberof NetTalk
*/
constructor (port, server, certificate, verify = "0")
this.port = port;
this.server = server;
this.options = ca:[ fs.readFileSync(certificate) ], rejectUnauthorized: false;
this.msgSize = 0;
this.wholePackage = '';
this.socket = null;
this.verify = verify;


/**
*
* Connects to the NetTalk Service.
* @memberof NetTalk
*/
connect()
this.socket = tls.connect(this.port, this.server, this.options, () =>
process.stdin.pipe(this.socket);
process.stdin.resume();
);


this.socket.setTimeout(12000)
this.socket.setEncoding('ascii');


this.socket.on('end', () =>
console.log('Connection terminated.');
);


this.socket.on('timeout', ()=>
console.log('Connection timed out.');
this.socket.destroy();
);


return new Promise((resolve, reject) =>
this.socket.on('ready', () =>
console.log(`client connected on port $this.port to server $this.server`);
resolve();
);

this.socket.on('error', (err) =>
reject(err:err, func:'connect');
);
);


/**
* sends the request.
*
* @param String reqMsg Request Message to be sent to the service.
* @returns Promise Promise.<resolve(data)
* @memberof NetTalk
*/
request(reqMsg)
return new Promise((resolve, reject) =>
/*Adding 32 bit length Integer to the beginning of stream
for compatibility with WPP (Whole Packet Protocol).*/
var message = new Buffer(reqMsg, 'binary');
var stream = new Buffer(4 + message.length); //declares the stream to be sent.

stream.writeInt32LE(message.length, 0); //Must be in LittleEndian for compatibility.
message.copy(stream, 4);

this.socket.write(stream);

this.socket.on('data', (data) =>
let wholePackageSize = null;

this.wholePackage += data;
wholePackageSize = new Buffer(this.wholePackage, 'binary');
this.msgSize += data.length;

if (wholePackageSize.readInt32LE(0) <= this.msgSize)
this.socket.end();
resolve(this.wholePackage.slice(4, this.wholePackage.length));

);

this.socket.on('timeout', ()=>
this.socket.destroy();
reject(err:'Error!, server do not respond', func:'request')
);

this.socket.on('error', err =>
reject(err:err, func:'request');
)
);



module.exports = NetTalk;


I add the link to the GitHub project as well.







share|improve this question





















  • Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.".
    – Sam Onela
    Jun 27 at 20:03










  • Thank you so much @SamOnela for your answer I will edit it right now
    – Enoc Martinez
    Jun 27 at 20:05












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have built this small class as a small library to be able to talk to a server that uses CapeSoft's NetTalk library as socket library, and which uses the Whole Packet Protocol, which is a simple protocol made by CapeSoft for the communication between NetTalk Apps. However, I need to talk to this "REST" Server to get some Info for my Node App, and seeing that this Server does not work with Regular HTTP Protocol, I had to write this library myself.



I wish to know if there is any way (which I'm sure there is) to better refactor this small class and improve the comprehension of it as well as it's comments in JavaScript.



I am trying to improve my refactoring and clean coding skills. I want to be able to write the shortest code while still being simple to understand.



I would appreciate much your help to improve this. I really wish to become top programmer.



Following is my class module; any critique is welcome and well accepted.



const tls = require('tls');
const fs = require('fs');


/**
*
*
* NetTalk Class:
* Creates a NetTalk Object for making request to a NetTalk Service in WPP (Whole Packet Protocol).
*
*/
class NetTalk
/**
* Creates an instance of NetTalk.
* @param Number port Port through which you wish to connect to NetTalk.
* @param String server Server to which to connect.
* @param String certificate Route where the certificate file (*.pem) is located
* @param String [verify = '0'] verify the certificate? (default="0")
* @memberof NetTalk
*/
constructor (port, server, certificate, verify = "0")
this.port = port;
this.server = server;
this.options = ca:[ fs.readFileSync(certificate) ], rejectUnauthorized: false;
this.msgSize = 0;
this.wholePackage = '';
this.socket = null;
this.verify = verify;


/**
*
* Connects to the NetTalk Service.
* @memberof NetTalk
*/
connect()
this.socket = tls.connect(this.port, this.server, this.options, () =>
process.stdin.pipe(this.socket);
process.stdin.resume();
);


this.socket.setTimeout(12000)
this.socket.setEncoding('ascii');


this.socket.on('end', () =>
console.log('Connection terminated.');
);


this.socket.on('timeout', ()=>
console.log('Connection timed out.');
this.socket.destroy();
);


return new Promise((resolve, reject) =>
this.socket.on('ready', () =>
console.log(`client connected on port $this.port to server $this.server`);
resolve();
);

this.socket.on('error', (err) =>
reject(err:err, func:'connect');
);
);


/**
* sends the request.
*
* @param String reqMsg Request Message to be sent to the service.
* @returns Promise Promise.<resolve(data)
* @memberof NetTalk
*/
request(reqMsg)
return new Promise((resolve, reject) =>
/*Adding 32 bit length Integer to the beginning of stream
for compatibility with WPP (Whole Packet Protocol).*/
var message = new Buffer(reqMsg, 'binary');
var stream = new Buffer(4 + message.length); //declares the stream to be sent.

stream.writeInt32LE(message.length, 0); //Must be in LittleEndian for compatibility.
message.copy(stream, 4);

this.socket.write(stream);

this.socket.on('data', (data) =>
let wholePackageSize = null;

this.wholePackage += data;
wholePackageSize = new Buffer(this.wholePackage, 'binary');
this.msgSize += data.length;

if (wholePackageSize.readInt32LE(0) <= this.msgSize)
this.socket.end();
resolve(this.wholePackage.slice(4, this.wholePackage.length));

);

this.socket.on('timeout', ()=>
this.socket.destroy();
reject(err:'Error!, server do not respond', func:'request')
);

this.socket.on('error', err =>
reject(err:err, func:'request');
)
);



module.exports = NetTalk;


I add the link to the GitHub project as well.







share|improve this question













I have built this small class as a small library to be able to talk to a server that uses CapeSoft's NetTalk library as socket library, and which uses the Whole Packet Protocol, which is a simple protocol made by CapeSoft for the communication between NetTalk Apps. However, I need to talk to this "REST" Server to get some Info for my Node App, and seeing that this Server does not work with Regular HTTP Protocol, I had to write this library myself.



I wish to know if there is any way (which I'm sure there is) to better refactor this small class and improve the comprehension of it as well as it's comments in JavaScript.



I am trying to improve my refactoring and clean coding skills. I want to be able to write the shortest code while still being simple to understand.



I would appreciate much your help to improve this. I really wish to become top programmer.



Following is my class module; any critique is welcome and well accepted.



const tls = require('tls');
const fs = require('fs');


/**
*
*
* NetTalk Class:
* Creates a NetTalk Object for making request to a NetTalk Service in WPP (Whole Packet Protocol).
*
*/
class NetTalk
/**
* Creates an instance of NetTalk.
* @param Number port Port through which you wish to connect to NetTalk.
* @param String server Server to which to connect.
* @param String certificate Route where the certificate file (*.pem) is located
* @param String [verify = '0'] verify the certificate? (default="0")
* @memberof NetTalk
*/
constructor (port, server, certificate, verify = "0")
this.port = port;
this.server = server;
this.options = ca:[ fs.readFileSync(certificate) ], rejectUnauthorized: false;
this.msgSize = 0;
this.wholePackage = '';
this.socket = null;
this.verify = verify;


/**
*
* Connects to the NetTalk Service.
* @memberof NetTalk
*/
connect()
this.socket = tls.connect(this.port, this.server, this.options, () =>
process.stdin.pipe(this.socket);
process.stdin.resume();
);


this.socket.setTimeout(12000)
this.socket.setEncoding('ascii');


this.socket.on('end', () =>
console.log('Connection terminated.');
);


this.socket.on('timeout', ()=>
console.log('Connection timed out.');
this.socket.destroy();
);


return new Promise((resolve, reject) =>
this.socket.on('ready', () =>
console.log(`client connected on port $this.port to server $this.server`);
resolve();
);

this.socket.on('error', (err) =>
reject(err:err, func:'connect');
);
);


/**
* sends the request.
*
* @param String reqMsg Request Message to be sent to the service.
* @returns Promise Promise.<resolve(data)
* @memberof NetTalk
*/
request(reqMsg)
return new Promise((resolve, reject) =>
/*Adding 32 bit length Integer to the beginning of stream
for compatibility with WPP (Whole Packet Protocol).*/
var message = new Buffer(reqMsg, 'binary');
var stream = new Buffer(4 + message.length); //declares the stream to be sent.

stream.writeInt32LE(message.length, 0); //Must be in LittleEndian for compatibility.
message.copy(stream, 4);

this.socket.write(stream);

this.socket.on('data', (data) =>
let wholePackageSize = null;

this.wholePackage += data;
wholePackageSize = new Buffer(this.wholePackage, 'binary');
this.msgSize += data.length;

if (wholePackageSize.readInt32LE(0) <= this.msgSize)
this.socket.end();
resolve(this.wholePackage.slice(4, this.wholePackage.length));

);

this.socket.on('timeout', ()=>
this.socket.destroy();
reject(err:'Error!, server do not respond', func:'request')
);

this.socket.on('error', err =>
reject(err:err, func:'request');
)
);



module.exports = NetTalk;


I add the link to the GitHub project as well.









share|improve this question












share|improve this question




share|improve this question








edited Jul 10 at 18:41









200_success

123k14143399




123k14143399









asked Jun 27 at 19:39









Enoc Martinez

165




165











  • Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.".
    – Sam Onela
    Jun 27 at 20:03










  • Thank you so much @SamOnela for your answer I will edit it right now
    – Enoc Martinez
    Jun 27 at 20:05
















  • Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.".
    – Sam Onela
    Jun 27 at 20:03










  • Thank you so much @SamOnela for your answer I will edit it right now
    – Enoc Martinez
    Jun 27 at 20:05















Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.".
– Sam Onela
Jun 27 at 20:03




Welcome to Code Review! What task does this code accomplish? Please tell us, and also make that the title of the question via edit. Maybe you missed the placeholder on the title element: "State the task that your code accomplishes. Make your title distinctive.". Also from How to Ask: "State what your code does in your title, not your main concerns about it.".
– Sam Onela
Jun 27 at 20:03












Thank you so much @SamOnela for your answer I will edit it right now
– Enoc Martinez
Jun 27 at 20:05




Thank you so much @SamOnela for your answer I will edit it right now
– Enoc Martinez
Jun 27 at 20:05















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197373%2fwhole-packet-protocol-communication-library-in-node%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197373%2fwhole-packet-protocol-communication-library-in-node%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods