Skip to content

Commit 012518d

Browse files
committed
first pass at integrating socketio
1 parent b178507 commit 012518d

File tree

10 files changed

+380
-8
lines changed

10 files changed

+380
-8
lines changed

coder-apps/common/coderlib/app/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"created": "2013-03-05",
3-
"modified": "2013-07-17",
3+
"modified": "2013-12-01",
44
"color": "#3e3e3e",
55
"author": "Jason Striegel",
66
"name": "CoderLib",

coder-apps/common/coderlib/static/js/index.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ if (typeof console === "undefined" || typeof console.log === "undefined") {
2323
console.log = function() {};
2424
}
2525

26+
var socketReady = false;
27+
var onSocketReady = null;
28+
29+
2630
var Coder = {
2731
coderlib_url: "/app/coderlib",
2832
appname: '',
2933
appurl: '',
34+
socket: null,
35+
socketListeners: {},
3036

3137
listApps: function( callback ) {
3238
$.get( Coder.coderlib_url + "/api/app/list", function(data){
@@ -50,6 +56,72 @@ var Coder = {
5056
});
5157
});
5258
},
59+
60+
connectSocket: function( callback ) {
61+
var doConnect = function() {
62+
Coder.socket = io.connect(window.location.protocol + window.location.hostname + ':' + window.location.port +'/' );
63+
Coder.socket.on('connect', function() {
64+
console.log('socket connected');
65+
66+
if ( callback ) {
67+
callback();
68+
}
69+
});
70+
Coder.socket.on('SERVERLOG', function(d) {
71+
console.log('SERVERLOG: ' + JSON.stringify(d));
72+
});
73+
Coder.socket.on('appdata', function(data) {
74+
console.log('appdata received');
75+
console.log( data );
76+
if ( data && data.key !== undefined ) {
77+
if ( Coder.socketListeners[data.key] !== undefined ) {
78+
for ( var x=0; x<Coder.socketListeners[data.key].length; x++ ) {
79+
var handler = Coder.socketListeners[data.key][x];
80+
var appdata = data.data;
81+
handler( appdata );
82+
}
83+
}
84+
}
85+
});
86+
};
87+
88+
if ( socketReady ) {
89+
doConnect();
90+
} else {
91+
onSocketReady = doConnect;
92+
}
93+
},
94+
95+
sendData: function( appid, key, data ) {
96+
if ( Coder.socket !== null ) {
97+
Coder.socket.emit( 'appdata', {
98+
appid: appid,
99+
key: key,
100+
data: data
101+
});
102+
}
103+
},
104+
105+
addSocketListener: function( key, handler ) {
106+
if ( Coder.socketListeners[key] === undefined ) {
107+
Coder.socketListeners[key] = [];
108+
}
109+
Coder.socketListeners[key].push( handler );
110+
},
111+
112+
removeSocketListener: function( key, handler ) {
113+
if ( Coder.socketListeners[key] !== undefined && handler !== null ) {
114+
var updated = [];
115+
for ( var x=0; x<Coder.socketListeners[key].length; x++ ) {
116+
if ( Coder.socketListeners[key][x] !== handler ) {
117+
updated.push( Coder.socketListeners[key][x] );
118+
}
119+
Coder.socketListeners[key] = updated;
120+
}
121+
} else if ( Coder.socketListeners[key] !== undefined ) {
122+
Coder.socketListeners[key] = [];
123+
}
124+
}
53125
};
54126

55127
if ( typeof appname != 'nothing' ) {
@@ -59,6 +131,29 @@ if ( typeof appurl != 'nothing' ) {
59131
Coder.appurl = appurl;
60132
}
61133

134+
var loadSocketIO = function() {
135+
var head = document.getElementsByTagName('head')[0];
136+
var script = document.createElement('script');
137+
script.type = 'text/javascript';
138+
script.src = '/socket.io/socket.io.js';
139+
140+
script.onreadystatechange = socketIOReady;
141+
script.onload = socketIOReady;
142+
head.appendChild(script);
143+
};
144+
var socketIOReady = function() {
145+
socketReady = true;
146+
if ( onSocketReady ) {
147+
onSocketReady();
148+
}
149+
};
150+
loadSocketIO();
151+
152+
153+
154+
155+
156+
62157
var getParams = (function(qs){
63158
var params = {};
64159
if ( typeof qs !== 'undefined' ) {
@@ -73,3 +168,6 @@ var getParams = (function(qs){
73168
}
74169
return params;
75170
})(window.location.search.substr(1).split('&'));
171+
172+
173+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
exports.settings={};
2+
//These are dynamically updated by the runtime
3+
//settings.appname - the app id (folder) where your app is installed
4+
//settings.viewpath - prefix to where your view html files are located
5+
//settings.staticurl - base url path to static assets /static/apps/appname
6+
//settings.appurl - base url path to this app /app/appname
7+
//settings.device_name - name given to this coder by the user, Ie."Billy's Coder"
8+
//settings.coder_owner - name of the user, Ie. "Suzie Q."
9+
//settings.coder_color - hex css color given to this coder.
10+
11+
exports.get_routes = [
12+
{ path:'/', handler:'index_handler' },
13+
];
14+
15+
exports.post_routes = [
16+
];
17+
18+
exports.socketio_routes = [
19+
{ key:'connect', handler:'on_socket_connect' },
20+
{ key:'message', handler:'on_socket_message' }
21+
];
22+
23+
24+
var connections = {};
25+
26+
exports.index_handler = function( req, res ) {
27+
var tmplvars = {};
28+
tmplvars['static_url'] = exports.settings.staticurl;
29+
tmplvars['app_name'] = exports.settings.appname;
30+
tmplvars['app_url'] = exports.settings.appurl;
31+
tmplvars['device_name'] = exports.settings.device_name;
32+
33+
res.render( exports.settings.viewpath + '/index', tmplvars );
34+
};
35+
36+
exports.on_socket_connect = function( socket, data ) {
37+
console.log( 'socket connect: ' + socket.handshake.sessionID );
38+
console.log( data );
39+
connections[data.id] = {
40+
socket: socket,
41+
name: data.name,
42+
id: data.id
43+
};
44+
45+
};
46+
47+
exports.on_socket_message = function( socket, data ) {
48+
console.log( 'socket message from: ' );
49+
console.log( socket.handshake.sessionID );
50+
var me = connections[data.id];
51+
if ( me ) {
52+
var updated = {};
53+
for ( var k in connections ) {
54+
console.log( connections[k].name );
55+
56+
if ( !connections[k].socket.disconnected ) {
57+
updated[k] = connections[k];
58+
if ( connections[k] !== me ) {
59+
var s = connections[k].socket;
60+
s.emit('appdata', {
61+
key: 'message',
62+
data: {
63+
name: me.name,
64+
message: data.message
65+
}
66+
});
67+
}
68+
}
69+
}
70+
connections = updated;
71+
}
72+
73+
};
74+
75+
76+
exports.on_destroy = function() {
77+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"created": "2013-11-30",
3+
"modified": "2013-12-01",
4+
"color": "#2ecc71",
5+
"author": "",
6+
"name": "sockettest",
7+
"hidden": false
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.pagecontent {
3+
padding: 24px;
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var id;
2+
3+
$(document).ready( function() {
4+
5+
id=randomID();
6+
7+
//This code will run after your page loads
8+
Coder.connectSocket(function(){
9+
console.log("connected!");
10+
Coder.sendData( appname, 'connect', {'name':'jimmy','id': id } );
11+
12+
Coder.addSocketListener( 'message', function( d ){
13+
console.log("message from: " + d.name + " : " + d.message);
14+
});
15+
});
16+
17+
});
18+
19+
var randomID = function()
20+
{
21+
var id = "";
22+
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
23+
24+
for( var i=0; i < 32; i++ )
25+
id += possible.charAt(Math.floor(Math.random() * possible.length));
26+
27+
return id;
28+
}

coder-apps/tests/sockettest/static/media/.gitignore

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Coder</title>
5+
<meta charset="utf-8">
6+
<!-- Standard Coder Includes -->
7+
<script>
8+
var appname = "{{app_name}}"; //app name (id) of this app
9+
var appurl = "{{&app_url}}";
10+
var staticurl = "{{&static_url}}"; //base path to your static files /static/apps/yourapp
11+
</script>
12+
<link href="/static/apps/coderlib/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
13+
<script src="/static/common/js/jquery.min.js"></script>
14+
<script src="/static/common/ace-min/ace.js" type="text/javascript" charset="utf-8"></script>
15+
<script src="/static/apps/coderlib/js/index.js"></script>
16+
<script>
17+
Coder.addBasicNav();
18+
</script>
19+
<!-- End Coder Includes -->
20+
21+
<!-- This app's includes -->
22+
<link href="{{&static_url}}/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
23+
<script src="{{&static_url}}/js/index.js"></script>
24+
<!-- End apps includes -->
25+
</head>
26+
<body class="">
27+
<div class="pagecontent">
28+
<h1>Hello World</h1>
29+
<p>Your html goes here.</p>
30+
31+
</div>
32+
</body>
33+
</html>

coder-base/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"socket.io": "0.9.13",
1212
"express-params": "0.0.3",
1313
"bcrypt": "0.7.4",
14-
"connect": "*"
14+
"connect": "*",
15+
"cookie": "*"
1516
}
1617
}

0 commit comments

Comments
 (0)