ozserver
Version:
API for OZ
86 lines (81 loc) • 2.5 kB
JavaScript
var colors, moment, sockjs;
sockjs = require('sockjs');
colors = require('colors');
moment = require('moment');
module.exports = function(server, streak) {
var sockets;
sockets = sockjs.createServer({
log: function(severity, line) {
var comment, method, parse;
parse = line.match(/^([\w]+)[\s](.*?)$/);
method = parse[1] != null ? parse[1].toString() : '';
comment = parse[2] != null ? parse[2].toString() : '';
return console.log("" + (moment().format('HH:mm:ss.SSS').blue) + " " + (moment().format('DD/MM/YYYY').magenta) + " " + method.green + " " + comment.grey);
}
});
sockets.on('connection', function(socket) {
var client, _ref;
client = {
id: ((_ref = socket._session) != null ? _ref.session_id : void 0) != null ? socket._session.session_id : null,
token: null,
sessid: null,
region: {},
user_id: 0
};
socket.on('data', function(s) {
var data;
data = JSON.parse(s);
if ((data != null ? data.message : void 0) != null) {
switch (data.message) {
case 'connect':
if (data.region != null) {
client.region = data.region;
}
if (data.user_id != null) {
client.user_id = data.user_id;
}
if (data.token != null) {
client.token = data.token;
}
if (data.sessid != null) {
return client.sessid = data.sessid;
}
}
}
});
streak.on('feed.post', function(data) {
if (this.isSocketReady(socket)) {
if (this.isRegionRight(client, data)) {
if (this.isPostedSuccess(data)) {
return socket.write(JSON.stringify({
message: 'feed.update'
}));
}
}
}
});
streak.on('feed.edit', function(data) {
if (this.isSocketReady(socket)) {
if (this.isRegionRight(client, data)) {
if (this.isPostedSuccess(data)) {
return socket.write(JSON.stringify({
message: 'feed.update'
}));
}
}
}
});
return streak.on('feed.delete', function(data) {
if (this.isSocketReady(socket)) {
if (this.isRegionRight(client, data)) {
return socket.write(JSON.stringify({
message: 'feed.update'
}));
}
}
});
});
return sockets.installHandlers(server, {
prefix: '/sockets'
});
};