@@ -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+
4449var 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