Skip to content

Commit b6df66f

Browse files
committed
bahhh
1 parent 9279bcb commit b6df66f

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎v3/js/opt-frontend-common.js‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var pyInputAceEditor; // Ace editor object that contains the input code
7070
var useCodeMirror = false; // true -> use CodeMirror, false -> use Ace
7171

7272

73-
var loggingSocketIO; // socket.io instance -- optional: not all frontends use it
73+
var loggingSocketIO = undefined; // socket.io instance -- OPTIONAL: not all frontends use it
7474

7575
// From http://stackoverflow.com/a/8809472
7676
function generateUUID(){
@@ -1721,6 +1721,19 @@ function logEvent(obj) {
17211721
}
17221722
// this probably won't match the server time due to time zones, etc.
17231723
obj.clientTime = new Date().getTime();
1724-
loggingSocketIO.emit('opt-client-event', obj);
1724+
1725+
if (loggingSocketIO.connected) {
1726+
loggingSocketIO.emit('opt-client-event', obj);
1727+
//console.log('emitted opt-client-event:', obj);
1728+
} else {
1729+
// TODO: be careful about this getting HUGE if loggingSocketIO
1730+
// never connects properly ...
1731+
logEventQueue.push(obj); // queue this up to be logged when the client
1732+
// finishes successfully connecting to the server
1733+
1734+
// we're not yet connected, or we've been disconnected by the
1735+
// server, so try to connect/reconnect first before emitting the event
1736+
loggingSocketIO.connect(); // will trigger the .on('connect', ...) handler
1737+
}
17251738
}
17261739
}

‎v3/js/opt-frontend.js‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4141
// should all be imported BEFORE this file
4242

4343

44+
// NASTY GLOBALS for socket.io!
45+
var reconnectAttempts = 0;
46+
var logEventQueue = []; // TODO: make sure this doesn't grow too large if socketio isn't enabled
47+
48+
4449
var originFrontendJsFile = 'opt-frontend.js';
4550

4651
// for OPT live chat tutoring interface
@@ -470,6 +475,31 @@ $(document).ready(function() {
470475

471476
genericOptFrontendReady(); // initialize at the end
472477

473-
//loggingSocketIO = io.connect('http://104.237.139.253:5000/'); // PRODUCTION_PORT
474-
//loggingSocketIO = io.connect('http://104.237.139.253:5001/'); // DEBUG_PORT
478+
// connect on-demand in logEvent(), not here
479+
//loggingSocketIO = io('http://104.237.139.253:5000/'); // PRODUCTION_PORT
480+
//loggingSocketIO = io('http://104.237.139.253:5001/'); // DEBUG_PORT
481+
482+
483+
if (loggingSocketIO) {
484+
loggingSocketIO.on('connect', function() {
485+
//console.log('CONNECTED and emitting', logEventQueue.length, 'events');
486+
487+
if (logEventQueue.length > 0) {
488+
// the reconnectAttempts field that denotes how many times you've
489+
// attempted to reconnect (which is also how many times you've
490+
// been kicked off by the logging server for, say, being idle).
491+
// add this as an extra field on the FIRST event
492+
if (reconnectAttempts > 0) {
493+
logEventQueue[0].reconnectAttempts = reconnectAttempts;
494+
}
495+
496+
while (logEventQueue.length > 0) {
497+
loggingSocketIO.emit('opt-client-event', logEventQueue.pop());
498+
}
499+
}
500+
assert(logEventQueue.length === 0);
501+
502+
reconnectAttempts++;
503+
});
504+
}
475505
});

0 commit comments

Comments
 (0)