I'm currently playing around with coffeescript
and websockets using nodejs
.
I created a class SocketHandler
for managing websocket clients:
class SocketHandler
constructor: (@server) ->
@server.on "connection", @onClientConnect
onClientConnect: (socket) ->
console.log "client connected"
socket.on "close", @onClientDisconnect
onClientDisconnect: (code, message) ->
console.log "client disconnected"
ws = require "ws"
server = ws.Server { port: 8080 }
sockethandler = new SocketHandler server
When I run the script and a client connects I get the following error message:
client connected
events.js:210
throw new TypeError('listener must be a function');
^
TypeError: listener must be a function
[...]
I have no clue why this happens. In my view I'm passing a function reference to socket.on
as the second parameter.
I tried to investigate further and tried to output onClientDisconnect
to see what type it has.
So I changed
onClientConnect: (socket) ->
console.log "client connected"
socket.on "close", @onClientDisconnect
to
onClientConnect: (socket) ->
console.log "client connected"
console.log @onClientDisconnect
socket.on "close", @onClientDisconnect
resulting in getting an undefined
value as output.
Now I'm really confused and I think I'm missing something fundamental at how the language works.
Can anyone of you help me out?