Index: lams_tool_chat/web/includes/javascript/learning.js =================================================================== diff -u -r7d739cafc6626a1145073e5b85f5ed42d0f72dd0 -r7cb18d1521c69062337439737a624a60a27013d4 --- lams_tool_chat/web/includes/javascript/learning.js (.../learning.js) (revision 7d739cafc6626a1145073e5b85f5ed42d0f72dd0) +++ lams_tool_chat/web/includes/javascript/learning.js (.../learning.js) (revision 7cb18d1521c69062337439737a624a60a27013d4) @@ -22,15 +22,39 @@ // only Monitor can send a personal message var selectedUser = null, // init the connection with server using server URL but with different protocol - chatToolWebsocket = new WebSocket(APP_URL.replace('http', 'ws') + 'learningWebsocket?toolSessionID=' + TOOL_SESSION_ID); + chatWebsocket = new WebSocket(APP_URL.replace('http', 'ws') + + 'learningWebsocket?toolSessionID=' + TOOL_SESSION_ID), + chatWebsocketPingTimeout = null, + chatWebsocketPingFunc = null; - chatToolWebsocket.onclose = function(e){ + chatWebsocketPingFunc = function(skipPing){ + if (chatWebsocket.readyState == chatWebsocket.CLOSING + || chatWebsocket.readyState == chatWebsocket.CLOSED){ + location.reload(); + } + + // check and ping every 3 minutes + chatWebsocketPingTimeout = setTimeout(chatWebsocketPingFunc, 3*60*1000); + // initial set up does not send ping + if (!skipPing) { + chatWebsocket.send("ping"); + } + }; + // set up timer for the first time + chatWebsocketPingFunc(true); + + chatWebsocket.onclose = function(e){ + // react only on abnormal close if (e.code === 1006) { location.reload(); } }; - chatToolWebsocket.onmessage = function(e){ + chatWebsocket.onmessage = function(e){ + // reset ping timer + clearTimeout(chatWebsocketPingTimeout); + chatWebsocketPingFunc(true); + // create JSON object var input = JSON.parse(e.data); // clear old messages @@ -72,10 +96,6 @@ }); } - - chatToolWebsocket.onerror = function(e){ - alert("Error estabilishing connection to server: " + e.data); - } function userSelected(userDiv) { var userDivContent = userDiv.html(); @@ -108,7 +128,11 @@ }; // send it to server - chatToolWebsocket.send(JSON.stringify(output)); + chatWebsocket.send(JSON.stringify(output)); + + // reset ping timer + clearTimeout(chatWebsocketPingTimeout); + chatWebsocketPingFunc(true); } }); @@ -119,4 +143,4 @@ charSum += nick.charCodeAt(i); } return PALETTE[charSum % (PALETTE.length)]; -} +} \ No newline at end of file