Skip to content

Commit 00c522f

Browse files
committed
updated socker
1 parent f4d8b2d commit 00c522f

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

frame.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var request = require('request-json');
3636
var client = request.createClient('http://'+root_domain);
3737
var conf;
3838

39+
console.log(client);
3940

4041
// If reset flag is present, remove the conf
4142
// TODO: delete existing frame from server on reset.
@@ -80,15 +81,18 @@ try {
8081
});
8182
} else {
8283
conf.root_domain = root_domain;
83-
if (conf) startServer();
84+
if (conf) startServer();
8485
}
8586
} catch (e) {
8687
// no conf file, create new frame and save resulting conf
8788
var frame = {
8889
owner: username,
8990
users: [username],
9091
name: framename,
91-
active: false
92+
active: false,
93+
settings: {
94+
visible: true
95+
}
9296
};
9397

9498
client.post('/frames', frame, function(err, res, body) {
@@ -113,9 +117,9 @@ function createConfigFile(conf) {
113117
}
114118

115119

116-
//create node.js http server and listen on port
120+
//create node.js http server and listen on port
117121
function startServer() {
118-
http.createServer(app).listen(7000);
122+
http.createServer(app).listen(7000);
119123
launchFrame();
120124
}
121125

@@ -137,7 +141,7 @@ function launchFrame() {
137141
} else {
138142
open("http://localhost:7000");
139143
}
140-
}
144+
}
141145
}
142146

143147

static/frame.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* {
88
cursor: none
99
}
10-
10+
1111
html,
1212
body {
1313
width: 100%;
@@ -16,7 +16,7 @@
1616
margin: 0;
1717
background-color: #000;
1818
}
19-
19+
2020
body {
2121
background-repeat: no-repeat;
2222
background-position: center center;
@@ -42,7 +42,7 @@
4242
_domain = resp.root_domain;
4343
_username = resp.owner;
4444
_framename = resp.name;
45-
_frame_id = resp._id.$oid;
45+
_frame_id = resp._id;
4646

4747
Socker.connect("ws://" + _domain + "/ws/" + _frame_id, {
4848
onOpen: function() {
@@ -53,10 +53,10 @@
5353
Socker.on('frame:update_content', _handleUpdateContent);
5454
});
5555

56-
56+
5757
function _handleUpdateContent(content) {
5858
_id = content._id.$oid || content._id;
59-
59+
6060
_$body.animate({
6161
'opacity': 0
6262
}, 1000, function() {

static/js/socker.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ Socker = (function() {
77
checkInterval: 10000
88
},
99
_url,
10-
_ws;
10+
_ws,
11+
_timer;
1112

13+
/**
14+
* Create a websocket connection.
15+
* @param {string} url The server URL.
16+
* @param {object} opts Optional settings
17+
*/
1218
function _connect(url, opts) {
1319
_url = url;
1420
if (opts) _extend(_opts, opts);
@@ -44,7 +50,8 @@ Socker = (function() {
4450
};
4551

4652
if (_opts.keepAlive) {
47-
setInterval(_checkConnection, _opts.checkInterval);
53+
clearInterval(_timer);
54+
_timer = setInterval(_checkConnection, _opts.checkInterval);
4855
}
4956
}
5057

@@ -79,21 +86,36 @@ Socker = (function() {
7986
}
8087
}
8188

89+
/**
90+
* Send an event.
91+
* @param {[type]} name [description]
92+
* @param {[type]} data [description]
93+
* @return {[type]} [description]
94+
*/
8295
function _send(name, data) {
83-
message = {
96+
var message = {
8497
name: name,
8598
data: data
8699
};
87100

88101
_ws.send(JSON.stringify(message));
89102
}
90103

104+
/**
105+
* Check if the connection is established. If not, try to reconnect.
106+
* @return {[type]} [description]
107+
*/
91108
function _checkConnection() {
92109
if (!_connected) {
93110
_connect(_url, _opts);
94111
}
95112
}
96113

114+
/**
115+
* Utility function for extending an object.
116+
* @param {[type]} obj [description]
117+
* @return {[type]} [description]
118+
*/
97119
function _extend(obj) {
98120
Array.prototype.slice.call(arguments, 1).forEach(function(source) {
99121
if (source) {
@@ -112,3 +134,6 @@ Socker = (function() {
112134
_self.connect = _connect;
113135
return _self;
114136
})();
137+
138+
// COMMON.JS
139+
if (typeof module != 'undefined' && module.exports) module.exports = Socker;

0 commit comments

Comments
 (0)