Shouldn't `stream.on('data', onData);` be `this.onData`?
On Monday, March 25, 2013 8:44:02 AM UTC-7, Perrier wrote:
>
> 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.