Skip to content

Instantly share code, notes, and snippets.

@Loschcode
Created August 14, 2014 10:38
Show Gist options
  • Save Loschcode/40b2e3c9fd94c72ad4a5 to your computer and use it in GitHub Desktop.
Save Loschcode/40b2e3c9fd94c72ad4a5 to your computer and use it in GitHub Desktop.
/**
* Sockets
*
* @module :: Sockets
* @description :: Here all events for sockets
*
*/
require(['main', 'datas', 'queries', 'app/controllers/server_controller', 'app/controllers/player_controller'], function (Main, Datas, Queries, Server, Player) {
var Sockets = {
/**
* We initialize the socket and get the user session, etc.
*/
init: function() {
/**
* UNICORN SOCKETS GUIDELINE
* -------------------
*
* SENDING/GETTING {
*
* kind: private|public|any|...
* area: server|any|...
* user: user|self|any|...
* role: referent|owner|admin|any|...
* type: pusher|watcher|any|...
*
* reference: (object)|any|...
*
* action: (string)
* params: (object)
*
* }
*
* -> Front-end and back-end sockets works with these formats everytime
* -> Everything is checked and ignored if it doesn't respect clearly this format
*
* -------------------
*/
// Connect to the Socket IO
var Socket = Main.socket();
/**
* We signin the user (get the session, get the session from cookie, signin as anonymous, etc.)
*/
datas = Queries.prepare_socket('self', 'automatic_signin');
Socket.emit('user', datas, function(feedback) {
// If everything went good
if (feedback.success) {
// We filter the datas we received and populate the header
Datas.reset('user', feedback.datas.user);
/**
* We access the private server of the user
*/
user_id = Datas.get('user', '_id');
datas = Queries.prepare_socket('private.server', {user: user_id} , 'join_my_private_server');
Socket.emit('server', datas, function(feedback) {
if (feedback.success) {
Server.init_server(feedback.datas.server, true);
}
});
}
// If there's a cookie to save
Queries.save_cookies_from_socket(feedback);
});
},
/**
* We handle the different datas in different formats and
* Transmit it to the correct front-end socket controllers
*/
handle: {
any: function() {
/**
* ANY
* We receive general sent for every user on the application
*/
Socket.on('any', function(datas) {
console.log('I RECEIVED SOMETHING ANY YO');
});
},
}
}
return Sockets;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment