My code looks as followed:
I have a socketAPI module which handles all the socket-traffic of the app:
var io = new socketIoServer();
var socketApi = {};
socketApi.io = io;
io.on('connection', function (client) {
const SetupHandler = require ('./socket_handlers/setup-handler.js');
setupHandler = new SetupHandler(client, arg1, arg2);
setupHandler.bind();
}
then in the setup handler i got following code:
function SetupHandler(client,arg1,arg2) {
this.client = client;
socketApi.setups[lobby.id] = new Setup(lobby, scenario_id);
this.setup = socketApi.setups[lobby.id];
this.client.emit("test"); //this works
}
SetupHandler.prototype.requestGameSetup = function(){
// do stuff
this.client.emit("test"); //doesnt work
};
SetupHandler.prototype.bind = function(){
this.client.on('request_game_setup', this.requestGameSetup);
}
module.exports = SetupHandler;
it gives me following error:
/home/markus/WebstormProjects/penquest/socket_handlers/setup-handler.js:32
this.client.emit("test");
^
TypeError: this.client.emit is not a function
at Socket.SetupHandler.requestGameSetup (/home/markus/WebstormProjects/penquest/socket_handlers/setup-handler.js:32:17)
at Socket.emit (events.js:180:13)
at /home/markus/WebstormProjects/penquest/node_modules/socket.io/lib/socket.js:513:12
The idea here is to have one SetupHandler per client connection which handles all the events , do you have any ideas how i can accomplish this?