Skip to content

Commit 302482c

Browse files
committed
slight socket.io library refarctoring
1 parent 012518d commit 302482c

File tree

10 files changed

+182
-169
lines changed

10 files changed

+182
-169
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-12-01",
3+
"modified": "2013-12-03",
44
"color": "#3e3e3e",
55
"author": "Jason Striegel",
66
"name": "CoderLib",

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

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

26-
var socketReady = false;
27-
var onSocketReady = null;
26+
2827

2928

3029
var Coder = {
3130
coderlib_url: "/app/coderlib",
3231
appname: '',
3332
appurl: '',
34-
socket: null,
35-
socketListeners: {},
3633

3734
listApps: function( callback ) {
3835
$.get( Coder.coderlib_url + "/api/app/list", function(data){
@@ -57,71 +54,86 @@ var Coder = {
5754
});
5855
},
5956

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 );
57+
58+
socketConnection: {
59+
socket: null,
60+
listeners: {},
61+
socketReady: false,
62+
onSocketReady: null,
63+
64+
init: function( callback ) {
65+
var doConnect = function() {
66+
var socket = io.connect(window.location.protocol + window.location.hostname + ':' + window.location.port +'/' );
67+
Coder.socketConnection.socket = socket;
68+
socket.on('connect', function() {
69+
console.log('socket connected');
70+
71+
socket.on('SOCKETID', function(socketid) {
72+
Coder.socketConnection.socketID = socketid;
73+
if ( callback ) {
74+
callback(socketid);
75+
}
76+
});
77+
});
78+
socket.on('SERVERLOG', function(d) {
79+
console.log('SERVERLOG: ' + JSON.stringify(d));
80+
});
81+
socket.on('appdata', function(data) {
82+
console.log('appdata received');
83+
console.log( data );
84+
if ( data && data.key !== undefined ) {
85+
if ( Coder.socketConnection.listeners[data.key] !== undefined ) {
86+
for ( var x=0; x<Coder.socketConnection.listeners[data.key].length; x++ ) {
87+
var handler = Coder.socketConnection.listeners[data.key][x];
88+
var appdata = data.data;
89+
handler( appdata );
90+
}
8291
}
8392
}
84-
}
85-
});
86-
};
93+
});
94+
};
95+
96+
if ( Coder.socketConnection.socketReady ) {
97+
doConnect();
98+
} else {
99+
Coder.socketConnection.onSocketReady = doConnect;
100+
}
101+
}, //end socket init
87102

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] );
103+
//Sends [data] to the current [appid]'s socket handler for [key]
104+
sendData: function( key, data ) {
105+
if ( Coder.socketConnection.socket !== null ) {
106+
Coder.socketConnection.socket.emit( 'appdata', {
107+
appid: Coder.appname,
108+
key: key,
109+
data: data
110+
});
111+
}
112+
},
113+
114+
//Listens on the socket for [key] and calls [handler](data)
115+
addListener: function( key, handler ) {
116+
if ( Coder.socketConnection.listeners[key] === undefined ) {
117+
Coder.socketConnection.listeners[key] = [];
118+
}
119+
Coder.socketConnection.listeners[key].push( handler );
120+
},
121+
removeListener: function( key, handler ) {
122+
if ( Coder.socketConnection.listeners[key] !== undefined && handler !== null ) {
123+
var updated = [];
124+
for ( var x=0; x<Coder.socketConnection.listeners[key].length; x++ ) {
125+
if ( Coder.socketConnection.listeners[key][x] !== handler ) {
126+
updated.push( Coder.socketConnection.listeners[key][x] );
127+
}
128+
Coder.socketConnection.listeners[key] = updated;
118129
}
119-
Coder.socketListeners[key] = updated;
130+
} else if ( Coder.socketConnection.listeners[key] !== undefined ) {
131+
Coder.socketConnection.listeners[key] = [];
120132
}
121-
} else if ( Coder.socketListeners[key] !== undefined ) {
122-
Coder.socketListeners[key] = [];
123-
}
124-
}
133+
},
134+
135+
}, //End Coder.socketConnection
136+
125137
};
126138

127139
if ( typeof appname != 'nothing' ) {
@@ -131,28 +143,6 @@ if ( typeof appurl != 'nothing' ) {
131143
Coder.appurl = appurl;
132144
}
133145

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-
156146

157147
var getParams = (function(qs){
158148
var params = {};
@@ -171,3 +161,22 @@ var getParams = (function(qs){
171161

172162

173163

164+
//Load Socket IO Scripts
165+
(function() {
166+
var head = document.getElementsByTagName('head')[0];
167+
var script = document.createElement('script');
168+
script.type = 'text/javascript';
169+
script.src = '/socket.io/socket.io.js';
170+
171+
var socketIOReady = function() {
172+
Coder.socketConnection.socketReady = true;
173+
if ( Coder.socketConnection.onSocketReady ) {
174+
Coder.socketConnection.onSocketReady();
175+
}
176+
};
177+
178+
script.onreadystatechange = socketIOReady;
179+
script.onload = socketIOReady;
180+
head.appendChild(script);
181+
})();
182+

coder-apps/tests/sockettest/app/app.js renamed to coder-apps/tests/socket_test/app/app.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,24 @@ exports.index_handler = function( req, res ) {
3434
};
3535

3636
exports.on_socket_connect = function( socket, data ) {
37-
console.log( 'socket connect: ' + socket.handshake.sessionID );
37+
console.log( 'socket connect from ID: ' + socket.socketID );
3838
console.log( data );
39-
connections[data.id] = {
39+
40+
connections[socket.socketID] = {
4041
socket: socket,
4142
name: data.name,
42-
id: data.id
43+
id: socket.socketID
4344
};
45+
socket.on('disconnect', function() {
46+
console.log( 'socket disconnect from ID: ' + socket.socketID );
47+
delete connections[socket.socketID];
48+
});
4449

4550
};
4651

4752
exports.on_socket_message = function( socket, data ) {
4853
console.log( 'socket message from: ' );
54+
4955
console.log( socket.handshake.sessionID );
5056
var me = connections[data.id];
5157
if ( me ) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"created": "2013-11-30",
3-
"modified": "2013-12-01",
3+
"modified": "2013-12-03",
44
"color": "#2ecc71",
55
"author": "",
6-
"name": "sockettest",
6+
"name": "Socket Test",
77
"hidden": false
88
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
$(document).ready( function() {
3+
4+
//This code will run after your page loads
5+
Coder.socketConnection.init(function(){
6+
console.log("connected with ID: " + Coder.socketConnection.socketID);
7+
8+
Coder.socketConnection.sendData( 'connect', {'name':'jimmy'} );
9+
10+
Coder.socketConnection.addListener( 'message', function( d ){
11+
console.log("message from: " + d.name + " : " + d.message);
12+
});
13+
});
14+
15+
});
16+

coder-apps/tests/sockettest/static/js/index.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)