function App(){ var app = this; var socket = io.connect(window.location.hostname); // Initialize all the modules of this app function init(){ app.user = new User (app); app.chat = new Chat (app); app.grid = new Grid (app); bind(); } // Bind events that should be handled by the app generically function bind(){ $(window).on({ blur: function(){ $("#message").blur(); }, focus: function(){ // $message.focus(); }, keydown: function(e) { // console.log(e.keyCode); switch(e.keyCode) { case 32: // space if (! app.chat.focused) { app.grid.toggle(); return false; } break; case 9: // tab $("#message").focus(); return false; } } }); } // Send a JSON object to the other clients app.send = function(ns, message){ socket.emit(ns, JSON.stringify(message)); } // Listen for events from other clients app.receive = function(ns, callback){ // socket.on(ns, callback); socket.on(ns, function(json){ var data; console.log(ns, json); try { console.log(typeof json); data = (typeof json === "string") ? JSON.parse(json) : json; } catch (e) { console.log("ERROR PARSING JSON"); data = {}; } callback(data); }); } init(); } // Load the app when the DOM is ready $(function(){ var app = new App (); });