From 00c522f09df423e32f593430db5fe7a0732157df Mon Sep 17 00:00:00 2001 From: Jonathan Wohl Date: Sat, 20 Jun 2015 12:55:35 -0400 Subject: [PATCH] updated socker --- frame.js | 14 +++++++++----- static/frame.html | 10 +++++----- static/js/socker.js | 31 ++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/frame.js b/frame.js index ac89d07..f11104a 100755 --- a/frame.js +++ b/frame.js @@ -36,6 +36,7 @@ var request = require('request-json'); var client = request.createClient('http://'+root_domain); var conf; +console.log(client); // If reset flag is present, remove the conf // TODO: delete existing frame from server on reset. @@ -80,7 +81,7 @@ try { }); } else { conf.root_domain = root_domain; - if (conf) startServer(); + if (conf) startServer(); } } catch (e) { // no conf file, create new frame and save resulting conf @@ -88,7 +89,10 @@ try { owner: username, users: [username], name: framename, - active: false + active: false, + settings: { + visible: true + } }; client.post('/frames', frame, function(err, res, body) { @@ -113,9 +117,9 @@ function createConfigFile(conf) { } -//create node.js http server and listen on port +//create node.js http server and listen on port function startServer() { - http.createServer(app).listen(7000); + http.createServer(app).listen(7000); launchFrame(); } @@ -137,7 +141,7 @@ function launchFrame() { } else { open("http://localhost:7000"); } - } + } } diff --git a/static/frame.html b/static/frame.html index 4a5ad55..c0dae6c 100644 --- a/static/frame.html +++ b/static/frame.html @@ -7,7 +7,7 @@ * { cursor: none } - + html, body { width: 100%; @@ -16,7 +16,7 @@ margin: 0; background-color: #000; } - + body { background-repeat: no-repeat; background-position: center center; @@ -42,7 +42,7 @@ _domain = resp.root_domain; _username = resp.owner; _framename = resp.name; - _frame_id = resp._id.$oid; + _frame_id = resp._id; Socker.connect("ws://" + _domain + "/ws/" + _frame_id, { onOpen: function() { @@ -53,10 +53,10 @@ Socker.on('frame:update_content', _handleUpdateContent); }); - + function _handleUpdateContent(content) { _id = content._id.$oid || content._id; - + _$body.animate({ 'opacity': 0 }, 1000, function() { diff --git a/static/js/socker.js b/static/js/socker.js index 66e2605..8c716b0 100644 --- a/static/js/socker.js +++ b/static/js/socker.js @@ -7,8 +7,14 @@ Socker = (function() { checkInterval: 10000 }, _url, - _ws; + _ws, + _timer; + /** + * Create a websocket connection. + * @param {string} url The server URL. + * @param {object} opts Optional settings + */ function _connect(url, opts) { _url = url; if (opts) _extend(_opts, opts); @@ -44,7 +50,8 @@ Socker = (function() { }; if (_opts.keepAlive) { - setInterval(_checkConnection, _opts.checkInterval); + clearInterval(_timer); + _timer = setInterval(_checkConnection, _opts.checkInterval); } } @@ -79,8 +86,14 @@ Socker = (function() { } } + /** + * Send an event. + * @param {[type]} name [description] + * @param {[type]} data [description] + * @return {[type]} [description] + */ function _send(name, data) { - message = { + var message = { name: name, data: data }; @@ -88,12 +101,21 @@ Socker = (function() { _ws.send(JSON.stringify(message)); } + /** + * Check if the connection is established. If not, try to reconnect. + * @return {[type]} [description] + */ function _checkConnection() { if (!_connected) { _connect(_url, _opts); } } + /** + * Utility function for extending an object. + * @param {[type]} obj [description] + * @return {[type]} [description] + */ function _extend(obj) { Array.prototype.slice.call(arguments, 1).forEach(function(source) { if (source) { @@ -112,3 +134,6 @@ Socker = (function() { _self.connect = _connect; return _self; })(); + +// COMMON.JS +if (typeof module != 'undefined' && module.exports) module.exports = Socker; \ No newline at end of file