javascript - Can't use non-anonymous method with socket.io, "listener" argument must be a function -
i searched answer (simple ?) question, yet, can't find clear.
i'm java developper trying fit in web market, but, oop, many things infurate me.
io.sockets.on('connection', function (socket) { console.log("client has connected! id = [" + socket.id + "]"); socket.on('disconnect', onplayerdisconnected(socket)); }); function onplayerdisconnected(socket) { socket.broadcast.emit('player_disconnected', socket.id); console.log("client has disconnected! id = [" + socket.id + "]"); }
here's stacktrace :
events.js:219 throw new typeerror('"listener" argument must function'); ^ typeerror: "listener" argument must function
if use anonymous function, works charm. problem is, if react (increasing) number of events, code won't maintenable.
so, there way break down code smaller functions ?
bonus question : possible event split code in multiple .js files ?
thanks lot,
node.js beginner.
you're passing in result of function onplayerdisconnected
(which appears undefined
since nothing returned) socket.on('disconnect')
handler , not actual function onplayerdisconnected
looking for.
change below code passes result of onplayerdisconnected(socket)
socket.on('disconnect', onplayerdisconnected(socket));
to code passes actual function onplayerdisconnected
socket.on('disconnect', onplayerdisconnected);
Comments
Post a Comment