Index: lams_learning/web/includes/javascript/kumalive.js =================================================================== diff -u -ra5b247dd91cb3ffabf9de46cba029e5537fad087 -r9496ff3ffe5e98272f7939a2cf4e8c6dbb2901f7 --- lams_learning/web/includes/javascript/kumalive.js (.../kumalive.js) (revision a5b247dd91cb3ffabf9de46cba029e5537fad087) +++ lams_learning/web/includes/javascript/kumalive.js (.../kumalive.js) (revision 9496ff3ffe5e98272f7939a2cf4e8c6dbb2901f7) @@ -1,10 +1,6 @@ "use strict" -var kumaliveWebsocket = new WebSocket(LEARNING_URL.replace('http', 'ws') - + 'kumaliveWebsocket?organisationID=' + orgId + '&role=' +role), - kumaliveWebsocketPingTimeout = null, - kumaliveWebsocketPingFunc = null, - // is the user a learenr or a teacher +var // is the user a learenr or a teacher roleTeacher = false, // was the initial set up run initialised = false, @@ -28,139 +24,106 @@ .append($('
').addClass('name')), REFRESH_DELAY = 1000, ANIMATION_DURATION = 1000, - PING_DELAY = 3*60*1000; + websocket = initWebsocket('kumalive' + orgId, + LEARNING_URL.replace('http', 'ws') + + 'kumaliveWebsocket?organisationID=' + orgId + '&role=' + role); -kumaliveWebsocketPingFunc = function(skipPing){ - if (kumaliveWebsocket.readyState == kumaliveWebsocket.CLOSING - || kumaliveWebsocket.readyState == kumaliveWebsocket.CLOSED){ - location.reload(); - } - - // check and ping every few minutes - kumaliveWebsocketPingTimeout = setTimeout(kumaliveWebsocketPingFunc, PING_DELAY); - // initial set up does not send ping - if (!skipPing) { - kumaliveWebsocket.send("ping"); - } -}; -// set up timer for the first time -kumaliveWebsocketPingFunc(true); +if (websocket) { + /** + * Fetches existing Kumalive session + */ + websocket.onopen = function(e) { + websocket.send(JSON.stringify({ + 'type' : 'start' + })); + }; -kumaliveWebsocket.onclose = function(e){ - // react only on abnormal close - if (e.code === 1006) { - location.reload(); - } -}; -// when the server pushes new messages and roster to the learner's browser + // when the server pushes new inputs + websocket.onmessage = function (e) { + // read JSON object + var message = JSON.parse(e.data), + type = message.type; + // check what is this message about + switch(type) { + case 'start' : { + // user tried to join a Kumalive which is not started yet + // try to start it, if user is a teacher + // otherwise just wait for a teacher + websocket.send(JSON.stringify({ + 'type' : 'start', + 'role' : role + })); + } + break; + case 'create' : { + // user is a teacher and will now create a new Kumalive + // hide splash screen + $('#initDiv').hide(); + // show name input + var createDiv = $('#createKumaliveDiv'), + rubricsDiv = $('#rubrics .panel-body', createDiv); + if (message.rubrics) { + $.each(message.rubrics, function(){ + if (this) { + var checkbox = $('
').addClass('checkbox').appendTo(rubricsDiv), + label = $('