Just started delving into CoffeeScript and facing a challenge. In my code snippet below, I initialize WebSocketServer with a number as the first argument and a function as the second argument. However, when the websocket receives a message, @msgHandler
suddenly becomes undefined. I attempted to resolve this by assigning @msgHandler
to a variable named handler
, but that also turned out to be undefined. Any suggestions or ideas on how to tackle this issue would be greatly appreciated. Thank you!
main.coffee
#Utilized for interaction with browser interface
webSocketServ = new sio.WebSocketServer Sauce.Server.WebSocketPort, (data, socketId) ->
try
json = JSON.parse(data)
msgType = json.msgType
catch error
return
if (this.isAuthed(socketId))
switch msgType
when "auth"
else
io.websocket 'TODO: ' + cmd
else
if msgType is 'auth'
token = json.token
socket.coffee
class WebSocketServer
constructor: (@port, @msgHandler) ->
@webSockets = []
@handlers = {}
@test = []
@authedSockes = []
@listen(@port);
console.log @msgHandler #msgHandler is defined here as [ Function ]
listen: (port) ->
@wsServ = engine.listen port
@wsServ.on 'connection', @onConnect
io.socket "WebServer socket started on port #{port}"
onConnect: (client) ->
io.websocket 'New connection with id of ' + client.id
handler = @msgHandler #@msgHandler is undefined here?
client.on 'message', (data) ->
handler data, client.id
io.websocket '[' + this.id + '] ' + JSON.stringify(data)
client.on 'close', ->
io.websocket '[' + this.id + '] Disconnect'
client.on 'error', (err) ->
io.websocket "IO error: " + err
compiled socket.coffee
WebSocketServer = (function() {
function WebSocketServer(port, msgHandler) {
this.port = port;
this.msgHandler = msgHandler;
this.webSockets = [];
this.handlers = {};
this.test = [];
this.authedSockes = [];
this.listen(this.port);
console.log(this.msgHandler);
}
WebSocketServer.prototype.listen = function(port) {
this.wsServ = engine.listen(port);
this.wsServ.on('connection', this.onConnect);
return io.socket("WebServer socket started on port " + port);
};
WebSocketServer.prototype.onConnect = function(client) {
var handler;
io.websocket('New connection with id of ' + client.id);
handler = this.msgHandler;
client.on('message', function(data) {
handler(data, client.id);
return io.websocket('[' + this.id + '] ' + JSON.stringify(data));
});
client.on('close', function() {
return io.websocket('[' + this.id + '] Disconnect');
});
return client.on('error', function(err) {
return io.websocket("IO error: " + err);
});
};
WebSocketServer.prototype.isAuthed = function(socketId) {
return __indexOf.call(this.authedSockets, user) >= 0;
};
WebSocketServer.prototype.authSocket = function(socketId) {
this.authedSockets.push(socketId);
return io.websocket('Authenticated socket with ID of ' + socketId);
};
WebSocketServer.prototype.deauthSocket = function(socketId) {
return this.authedSockets = this.authedSockets.filter(function(word) {
return word !== socketId;
});
};
return WebSocketServer;
})();