Skip to content

Commit

Permalink
updated socker
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwohl committed Jun 20, 2015
1 parent f4d8b2d commit 00c522f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
14 changes: 9 additions & 5 deletions frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -80,15 +81,18 @@ 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
var frame = {
owner: username,
users: [username],
name: framename,
active: false
active: false,
settings: {
visible: true
}
};

client.post('/frames', frame, function(err, res, body) {
Expand All @@ -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();
}

Expand All @@ -137,7 +141,7 @@ function launchFrame() {
} else {
open("http://localhost:7000");
}
}
}
}


Expand Down
10 changes: 5 additions & 5 deletions static/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* {
cursor: none
}

html,
body {
width: 100%;
Expand All @@ -16,7 +16,7 @@
margin: 0;
background-color: #000;
}

body {
background-repeat: no-repeat;
background-position: center center;
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
31 changes: 28 additions & 3 deletions static/js/socker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -44,7 +50,8 @@ Socker = (function() {
};

if (_opts.keepAlive) {
setInterval(_checkConnection, _opts.checkInterval);
clearInterval(_timer);
_timer = setInterval(_checkConnection, _opts.checkInterval);
}
}

Expand Down Expand Up @@ -79,21 +86,36 @@ 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
};

_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) {
Expand All @@ -112,3 +134,6 @@ Socker = (function() {
_self.connect = _connect;
return _self;
})();

// COMMON.JS
if (typeof module != 'undefined' && module.exports) module.exports = Socker;

0 comments on commit 00c522f

Please sign in to comment.