It looks like every time you call Crafty.c
, recieveMessageFromServer()
is getting called too. Every time recieveMessageFromServer
is invoked, it attaches an additional event listener on the socket. That's why the first time data comes back you get one copy, then the second time you get two, the third time you get three, and so on.
You either need to prevent recieveMessageFromServer
from being called multiple times, or use removeListener or removeAllListeners to remove the previously attached listeners.