spincycle
Version:
A reactive message router and object manager that lets clients subscribe to object property changes on the server
72 lines (59 loc) • 2.09 kB
JavaScript
// Generated by CoffeeScript 1.12.6
(function() {
var ClientEndpoints, IO, WsMethod, debug, uuid;
IO = require("socket.io");
uuid = require('node-uuid');
ClientEndpoints = require('./ClientEndpoints');
debug = process.env["DEBUG"];
WsMethod = (function() {
WsMethod.wsroutes = [];
function WsMethod(messageRouter, server) {
var io;
this.messageRouter = messageRouter;
io = IO(server);
io.set('origins', '*:*');
io.on("connection", function(socket) {
var adr, ip, port;
ip = socket.handshake.address;
port = socket.request.connection.remotePort;
adr = ip + ':' + port;
console.log('new ws connection from ' + adr);
ClientEndpoints.registerEndpoint(adr, function(msg) {
return socket.emit('message', msg);
});
socket.on("message", function(datastring) {
var data, fn;
if (typeof datastring === "string") {
data = JSON.parse(datastring);
} else {
data = datastring;
}
data.client = ip + ':' + port;
data.messageId = data.messageId || uuid.v4();
data.replyFunc = function(replydata) {
replydata.messageId = data.messageId;
return socket.emit('message', replydata);
};
fn = WsMethod.wsroutes[data.target];
if (fn) {
return fn(data);
} else {
return console.log('*********** Could not find registered target for ' + data.target);
}
});
return socket.on("disconnect", function() {
adr = ip + ':' + port;
console.log('client at ' + adr + ' disconnected');
return ClientEndpoints.removeEndpoint(adr);
});
});
this.messageRouter.addMethod('ws', this);
}
WsMethod.prototype.registrationFunc = function(targetName, targetFunc) {
return WsMethod.wsroutes[targetName] = targetFunc;
};
return WsMethod;
})();
module.exports = WsMethod;
}).call(this);
//# sourceMappingURL=WsMethod.js.map