Multiple Instances of net Sockets

21 views
Skip to first unread message

Muhammad Shahzad

unread,
Nov 6, 2017, 10:55:34 PM11/6/17
to nodejs
I have used net sockets library, and have made a class, i initialize two objects with different IP's and same PORT number, when i get connect with both objects, both show the IP of last connected object, even i have used this.Socket = new require( 'net' ).Socket(); in the constructor :(

Reason might be the that connect function shows recent IP being passed. see the code below.

Response that i get:
CONNECTED TO: 10.0.20.105:8898
CONNECTED TO: 10.0.20.105:8898
Connecting to SQL...
SQL Connection Successful...

Scales = require( './scales.class.js' );
var one = new Scales( "10.0.20.104", 8899 );
one
.Init().Process();
var one = new Scales( "10.0.20.105", 8898 );
one
.Init().Process();



=================================
scales.class.js

var MSSQL = require( 'mssql/msnodesqlv8' );
var isSQLConnected = '';


function Scales( IP, PORT )
{
 
//console.log( "hey: " + MSSQL );
 
//
 
this.Socket = new require( 'net' ).Socket();
 
this.SQL = MSSQL;
 
//
 
this.liveSQLConfig = {
 
};
 
this.localSQLConfig = {
 
};
 
this.SQLConfig = '';
 
//
 
this.devEnvironment = "live";
 
this.routerIP = IP;
 
this.routerPORT = PORT;
 
this.Weight = "0.0kg";
 
this.Timeout = 10000;
 
// this.connectedToRouter = false;
 
this.cachedWeightValue = 0;
 
this.reconnectOnError = true;
 
this.recursiveTimerRef = null;
 
this.closeConnectionOnFirstDataReceive = false;
 
this.printValueToConsole = true;
 
this.isSQLConnected = false;
}
Scales.prototype = {
 constructor
: Scales,
 
Init: function()
 
{
 
if ( this.devEnvironment == "live" )
 
{
 
this.SQLConfig = this.liveSQLConfig;
 
}
 
else
 
{
 
this.SQLConfig = this.localSQLConfig;
 
}
 
if ( !this.reconnectOnError )
 
{
 console
.log( 'Reconnecting...' );
 
}
 
//
 
this.Socket.on( 'error', function()
 
{
 ME
.onErrorEventHandler();
 
} );
 
return this;
 
},
 
Process: function()
 
{
 ME
= this;
 
//console.log( 'CONNECTED TO: ' + ME.routerIP );
 
this.Socket.connect( this.routerPORT, this.routerIP, function()
 
{
 clearTimeout
( ME.recursiveTimerRef ); // once connected timer should be cleared
 ME
.reconnectOnError = true; //once connected it should be allowed to execute onErrorEventHandler if error occurs and connection breaks
 console
.log( 'CONNECTED TO: ' + ME.routerIP + ':' + ME.routerPORT );
 ME
.Socket.on( 'data', function( data )
 
{
 
if ( ME.isSQLConnected == false )
 
{
 ME
.isSQLConnected = true;
 console
.log( 'Connecting to SQL...' );
 ME
.SQL.connect( ME.SQLConfig, function( err )
 
{
 
if ( err )
 
{
 console
.log( 'SQL:  ' + err );
 
}
 
else
 
{
 console
.log( 'SQL Connection Successful...' );
 ME
.onDataEventHandler( data );
 
};
 
} );
 
} //
 
} );
 
} );
 
},
 connectToSQLServer
: function()
 
{
 
//
 
},
 onDataEventHandler
: function( data )
 
{
 
//console.log( this.routerIP );
 
this.Weight = data.toString().trim().replace( /\s/g, '' ).replace( /US/g, '' ).replace( /ST/g, '' ).replace( /GS/g, '' ).replace( /,/g, '' ).replace( /GS/g, '' ).replace( /-/g, '' );
 
if ( this.printValueToConsole )
 
{
 console
.log( this.routerIP );
 
}
 
if ( this.Weight != this.cachedWeightValue && this.Weight.length <= 10 )
 
{
 
if ( this.printValueToConsole )
 
{
 console
.log( this.Weight );
 
this.printValueToConsole = false;
 
}
 
this.cachedWeightValue = this.Weight;
 
}
 
// Close the client socket completely
 
if ( this.closeConnectionOnFirstDataReceive ) //if want to perform something/check/test ONLY on the very first string we receive
 
{
 
this.Socket.destroy();
 
}
 
}
}
module.exports = Scales;

DaneiL

unread,
Nov 7, 2017, 11:20:12 PM11/7/17
to nod...@googlegroups.com
Yes. According to the code you sent..
I think you are overwriting the variable.
its:
Scales = require( './scales.class.js' );
var one = new Scales( "10.0.20.104", 8899 );
one
.Init().Process();
var one = new Scales( "10.0.20.105", 8898 );
one
.Init().Process();

it should be:
Scales = require( './scales.class.js' );
var one = new Scales( "10.0.20.104", 8899 );
one
.Init().Process();
var two = new Scales( "10.0.20.105", 8898 );
two.Init().Process();



--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/73944f5f-0962-4800-b1fb-d308e0451e12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
[]'s
Reply all
Reply to author
Forward
0 new messages