435628 (2) [Avatar] Offline
#1
code fails with.

this.clients[id] = client;
                     

TypeError: Cannot set property '::ffff:127.0.0.1:53230' of undefined


Copied the code from the github repo, so maybe just buggy?

'use strict';
const events = require('events');
const net = require('net');
const channel = new events.EventEmitter();

channel.clients = {};
channel.subscriptions = {};
channel.on('join', (id, client) => {
    this.clients[id] = client;
    this.subscriptions[id] = (senderId, message) => {
        if (id != senderId) {
            this.clients[id].write(message);
        }
    };
    this.on('broadcast', this.subscriptions[id]);
});

const server = net.createServer(client => {
    const id = `${client.remoteAddress}:${client.remotePort}`;
    channel.emit('join', id, client);
    client.on('data', data => {
        data = data.toString();
        channel.emit('broadcast', id, data);
    });
});
server.listen(8888);
288532 (1) [Avatar] Offline
#2
It looks like the error was to use "this" in the code listing. It should instead be using "channel". Here is code that should work:

const events = require('events');
const net = require('net');
var channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join', (id, client) => {
    console.log(channel.clients);
    channel.clients[id] = client;
    channel.subscriptions[id] = (senderId, message) => {
        if (id != senderId) {
            channel.clients[id].write(message);
        }
    };
    channel.on('broadcast', channel.subscriptions[id]);
});
const server = net.createServer(client => {
    const id = `${client.remoteAddress}:${client.remotePort}`;
    channel.emit('join', id, client);
    client.on('data', data => {
        data = data.toString();
        channel.emit('broadcast', id, data);
    });
});
server.listen(8888);
245295 (2) [Avatar] Offline
#3
According to node.js documentation: (https://nodejs.org/api/events.html#events_passing_arguments_and_this_to_listeners)

"It is possible to use ES6 Arrow Functions as listeners, however, when doing so, the this keyword will no longer reference the EventEmitter instance"

- Yannis

435628 (2) [Avatar] Offline
#4
Thanks! That worked.
plusplusdylan (6) [Avatar] Offline
#5
2.12 code just in case someone wants it smilie

const events = require('events');
const net = require('net');
const channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join', (id, client) => {
    channel.clients[id] = client;
    channel.subscriptions[id] = (senderId, message) => {
        if (id != senderId) {
            channel.clients[id].write(message);
        }
    };
    channel.on('broadcast', channel.subscriptions[id]);
    channel.on('leave', id => {
        channel.removeListener(
            'broadcast', channel.subscriptions[id]
        );
        channel.emit('broadcast', id, `${id} has left the chatroom.\n`);
    });
});
const server = net.createServer(client => {
    const id = `${client.remoteAddress}:${client.remotePort}`;
    channel.emit('join', id, client);
    client.on('data', data => {
        data = data.toString();
        channel.emit('broadcast', id, data);
    });
    client.on('close', () => {
        channel.emit('leave', id);
    });
});
server.listen(8888);
270922 (3) [Avatar] Offline
#6
Thank you