|
| 1 | +// Codeopticon learner interface. Include on a page AFTER AFTER AFTER |
| 2 | +// opt-frontend-common.js and opt-frontend.js |
| 3 | +// |
| 4 | +// assumes that an empty #chat_div div exists on the webpage page, |
| 5 | +// preferably on the BOTTOM. |
| 6 | +// also requires jquery.ui.chatbox-LEARNER.js and jquery.ui.chatbox-LEARNER.css |
| 7 | + |
| 8 | +var codeopticonSocketIO = undefined; // socket.io instance |
| 9 | +var codeopticonSession = undefined; |
| 10 | +//var codeopticonSession = 'CODEOPTICON_TESTING'; // to turn all monitoring on |
| 11 | + |
| 12 | +var reconnectAttempts = 0; |
| 13 | +var logEventQueue = []; // TODO: make sure this doesn't grow too large if socketio isn't enabled |
| 14 | +var chatBox = undefined; |
| 15 | + |
| 16 | + |
| 17 | +function createChatBox() { |
| 18 | + assert(!chatBox); |
| 19 | + chatBox = $("#chat_div").chatbox({id: "Me", |
| 20 | + user: {key : "value"}, |
| 21 | + title: "Live Chat With Tutor", |
| 22 | + width: 250, |
| 23 | + offset: 2, // offset from right edge |
| 24 | + messageSent: chatMsgSent, |
| 25 | + boxClosed: chatBoxClosed, |
| 26 | + chatboxToggled: chatBoxToggled, |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +function chatMsgSent(id, user, msg) { |
| 31 | + $("#chat_div").chatbox("option", "boxManager").addMsg(id, msg); |
| 32 | + logEventCodeopticon({type: 'opt-client-chat', |
| 33 | + text: msg, |
| 34 | + sid: codeopticonSocketIO ? codeopticonSocketIO.id : undefined |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +// only called when the user hits the X button to explicitly close the chat box |
| 39 | +function chatBoxClosed(id) { |
| 40 | + logEventCodeopticon({type: 'opt-client-chat', |
| 41 | + text: '[closed chat box]', |
| 42 | + sid: codeopticonSocketIO ? codeopticonSocketIO.id : undefined |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +// called when the user toggles the chat box open or close |
| 47 | +function chatBoxToggled(visible) { |
| 48 | + var msg = '[minimized chat box]'; |
| 49 | + if (visible) { |
| 50 | + msg = '[maximized chat box]'; |
| 51 | + } |
| 52 | + logEventCodeopticon({type: 'opt-client-chat', |
| 53 | + text: msg, |
| 54 | + sid: codeopticonSocketIO ? codeopticonSocketIO.id : undefined |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +function initCodeopticon() { |
| 59 | + // only initialize if you have a valid session ID |
| 60 | + if (!codeopticonSession) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // hide header elements only if we're not doing CODEOPTICON_TESTING |
| 65 | + if (codeopticonSession !== 'CODEOPTICON_TESTING') { |
| 66 | + $("#ssDiv,#surveyHeader,#sharedSessionDisplayDiv").hide(); |
| 67 | + $("#togetherjsStatus").css("font-size", "8pt"); |
| 68 | + $("#togetherjsStatus").html("Codeopticon session " + codeopticonSession); |
| 69 | + } |
| 70 | + |
| 71 | + // connect on-demand in logEventCodeopticon(), not here |
| 72 | + codeopticonSocketIO = io('http://104.237.139.253:5000/userlog'); // PRODUCTION_PORT |
| 73 | + //codeopticonSocketIO = io('http://104.237.139.253:5001/userlog'); // DEBUG_PORT |
| 74 | + //codeopticonSocketIO = io('http://localhost:5000/userlog'); // localhost debugging |
| 75 | + |
| 76 | + if (codeopticonSocketIO) { |
| 77 | + codeopticonSocketIO.on('connect', function() { |
| 78 | + //console.log('CONNECTED and emitting', logEventQueue.length, 'events'); |
| 79 | + |
| 80 | + if (logEventQueue.length > 0) { |
| 81 | + // the reconnectAttempts field that denotes how many times you've |
| 82 | + // attempted to reconnect (which is also how many times you've |
| 83 | + // been kicked off by the logging server for, say, being idle). |
| 84 | + // add this as an extra field on the FIRST event |
| 85 | + if (reconnectAttempts > 0) { |
| 86 | + logEventQueue[0].reconnectAttempts = reconnectAttempts; |
| 87 | + } |
| 88 | + |
| 89 | + while (logEventQueue.length > 0) { |
| 90 | + codeopticonSocketIO.emit('opt-client-event', logEventQueue.pop()); |
| 91 | + } |
| 92 | + } |
| 93 | + assert(logEventQueue.length === 0); |
| 94 | + |
| 95 | + reconnectAttempts++; |
| 96 | + }); |
| 97 | + |
| 98 | + codeopticonSocketIO.on('opt-codeopticon-observer-chat', function(msg) { |
| 99 | + if (!chatBox) { |
| 100 | + createChatBox(); |
| 101 | + } else { |
| 102 | + $("#chat_div").chatbox("option", "hidden", false); |
| 103 | + $("#chat_div").chatbox("showContent"); |
| 104 | + } |
| 105 | + |
| 106 | + $("#chat_div").chatbox("option", "boxManager").addMsg('Tutor', msg.text); |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// using socket.io: |
| 112 | +function logEventCodeopticon(obj) { |
| 113 | + //console.log(obj); |
| 114 | + if (codeopticonSocketIO) { |
| 115 | + assert(codeopticonSession); |
| 116 | + obj.codeopticonSession = codeopticonSession; |
| 117 | + |
| 118 | + obj.user_uuid = supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined, |
| 119 | + obj.session_uuid = sessionUUID; |
| 120 | + |
| 121 | + // this probably won't match the server time due to time zones, etc. |
| 122 | + obj.clientTime = new Date().getTime(); |
| 123 | + |
| 124 | + if (codeopticonSocketIO.connected) { |
| 125 | + codeopticonSocketIO.emit('opt-client-event', obj); |
| 126 | + //console.log('emitted opt-client-event:', obj); |
| 127 | + } else { |
| 128 | + // TODO: be careful about this getting HUGE if codeopticonSocketIO |
| 129 | + // never connects properly ... |
| 130 | + logEventQueue.push(obj); // queue this up to be logged when the client |
| 131 | + // finishes successfully connecting to the server |
| 132 | + |
| 133 | + // we're not yet connected, or we've been disconnected by the |
| 134 | + // server, so try to connect/reconnect first before emitting the event |
| 135 | + codeopticonSocketIO.connect(); // will trigger the .on('connect', ...) handler |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments