Thanks for the answers, finally I have figured it out somehow. You can see
the working class below, please take suggestions if it contains bugs or it
isn't nice enough:
function TCPServer(listenAddress, listenPort) {
/** @private */ var HOST = listenAddress;
/** @private */ var PORT = listenPort;
/** @private */ var tcpServer = net.createServer();
/** @private */ var gatewayList = [];
/**
* Searches for the gateway by address and port number.
*
* @param {string} address Remote address of the connected gateway.
* @param {number} port Remote port of the connected gateway.
*/
var findByAddressAndPort = function (address, port) {
var i;
for(i = 0; i < gatewayList.length; i++){
if(gatewayList[i] !== undefined) {
if(gatewayList[i].IP === address && gatewayList[i].Port === port) {
return {'index':i, 'gateway':gatewayList[i]};
}
}
}
};
/**
* Handles incoming data packets.
*
* @param {object} stream The open data stream where the packet came from.
* @param {object} data The byte array of the data buffer.
*/
var dataHandler = function (stream, data) {
var gateway = findByAddressAndPort(stream.remoteAddress,
stream.remotePort).gateway;
gateway.Buffer.Add(data);
};
var onData = function(data) {
dataHandler(this, data);
}
var onClose = function(close){
console.log('CONNECTION CLOSED', close);
};
var onError = function(exception) {
console.log('SOCKET ERROR: ' + exception);
};
var onListen = function onListen() {
tcpServer.on('connection', onConnection);
tcpServer.on('error', onError);
tcpServer.on('close', onClose);
console.log('Server listening on ' + HOST +':'+ PORT);
};
var onConnection = function (stream) {
var gw=new GatewayClass.Gateway(stream);
gatewayList.push(gw);
stream.on('data', onData);
console.log("Clients: ", gatewayList.length);
};
this.Listen = function() {
console.log(onListen);
tcpServer.listen(PORT, HOST, onListen);
}
}
2013. március 25., hétfő 16:44:02 UTC+1 időpontban Perrier a következőt
írta:
>
> Hi,
>
> I cannot reach some of the fields of my class and I don't understand why.
> Someone please take a look at my code. This is the Nth one, I was changing
> this to var, to TCPServer.prototype.sg etc. Nothing helped.
> I've started with a sample tcp server from Anwajler's blog
> here<http://anwajler.com/node.html>.
> It was working well, but I would like to put it into a seperate file, into
> a module. I've created my TCPServer class as follows:
>
> var net = require('net');
> var GatewayClass = require('./Gateway.js');
>
> exports.TCPServer = TCPServer;
>
> function TCPServer(host, port) {
> this.HOST = host;
> this.PORT = port;
> var tcpServer = net.createServer();
> this.gatewayList = [];
> this.findByAddressAndPort = function (address, port) {
> var i;
> for(i = 0; i < gatewayList.length; i++){
> if(gatewayList[i] !== undefined) {
> if(gatewayList[i].IP === address && gatewayList[i].Port === port) {
> return gatewayList[i];
> }
> }
> }
> };
> this.dataHandler = function (stream, data) {
> var gateway = this.findByAddressAndPort(stream.remoteAddress,
> stream.remotePort);
> gateway.Buffer.Add(data);
> };
>
> this.onData = function(data) {
> this.dataHandler(this, data);
> }
>
> this.onClose = function(){
> console.log('CONNECTION CLOSED');
> };
> this.onError = function(exception) {
> console.log('SOCKET ERROR: ' + exception);
> };
>
> this.onListen = function() {
> tcpServer.on('connection', this.onConnection);
> tcpServer.on('error', this.onError);
> tcpServer.on('close', this.onClose);
> console.log('Server listening on ' + this.HOST +':'+ this.PORT);
> };
>
> this.onConnection = function (stream) {
> var gw=new GatewayClass.Gateway(stream);
> gatewayList.push(gw);
> stream.on('data', onData);
> console.log("Clients: ", gatewayList.length);
> };
> this.Open = function() {
> tcpServer.listen(this.PORT, this.HOST, this.onListen);
> }
> }
>
> TCPServer.prototype.Listen = function() {
> this.Open();
> }
>
>
> In my main application I create an instance of TCPServer and start
> listening this way:
>
> var TCPServerClass = require('./tcpserver.js');
>
> var TCPServer = new TCPServerClass.TCPServer(GLOBALS.HOST, GLOBALS.PORT);
> TCPServer.Listen();
>
>
> Thanks for your help. This version says:
>
> events.js:130
> throw TypeError('listener must be a function');
> ^
> TypeError: listener must be a function
> at TypeError (<anonymous>)
> at Server.EventEmitter.addListener (events.js:130:11)
>
> Peter
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.