PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/websources/lib/thm.de/arsnova/api/socket.js

https://github.com/thm-projects/libarsnova-js
JavaScript | 157 lines | 125 code | 13 blank | 19 comment | 8 complexity | 621f85259d98634437ef5c97dc89c653 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*
  2. * Copyright 2013-2014 Daniel Gerhardt <anp-dev@z.dgerhardt.net> <daniel.gerhardt@mni.thm.de>
  3. *
  4. * This file is part of libarsnova-js.
  5. *
  6. * libarsnova-js is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. define(
  20. [
  21. "dojo/when",
  22. "dojo/request",
  23. "arsnova-api/globalConfig"
  24. ],
  25. function (when, request, globalConfig) {
  26. "use strict";
  27. var
  28. self = null,
  29. socketApiPrefix = globalConfig.get().apiPath + "/socket/",
  30. socketUrl = request.get(socketApiPrefix + "url"),
  31. socket = null,
  32. firstConnect = true,
  33. callbacks = [],
  34. reconnectListeners = [],
  35. latencyUpdateListeners = [],
  36. pingSentTime = 0,
  37. pingTimeoutHandle = null,
  38. /* private methods */
  39. setupLatencyCheck = null
  40. ;
  41. self = {
  42. connect: function () {
  43. if (!io || socket) {
  44. return;
  45. }
  46. socket = when(socketUrl, function (socketUrl) {
  47. var socketConn = io.connect(socketUrl);
  48. socketConn.on("connect", function () {
  49. if (!firstConnect) {
  50. return;
  51. }
  52. firstConnect = false;
  53. console.log("Socket.IO: connected");
  54. self.assign();
  55. for (var i = 0; i < callbacks.length; i++) {
  56. self.on(callbacks[i][0], callbacks[i][1]);
  57. }
  58. callbacks = [];
  59. setupLatencyCheck(socketConn.io.engine);
  60. });
  61. socketConn.on("disconnect", function () {
  62. console.log("Socket.IO: disconnected");
  63. });
  64. socketConn.on("reconnect", function () {
  65. console.log("Socket.IO: reconnected");
  66. when(self.assign(), function () {
  67. for (var i = 0; i < reconnectListeners.length; i++) {
  68. reconnectListeners[i]();
  69. }
  70. });
  71. setupLatencyCheck(socketConn.io.engine);
  72. });
  73. return socketConn;
  74. });
  75. },
  76. assign: function () {
  77. return when(socket, function (socket) {
  78. return request.post(socketApiPrefix + "assign", {
  79. headers: {"Content-Type": "application/json"},
  80. data: JSON.stringify({session: socket.io.engine.id})
  81. }).then(function () {
  82. console.log("Socket.IO: sessionid " + socket.io.engine.id + " assigned to user");
  83. });
  84. });
  85. },
  86. onReconnect: function (listener) {
  87. reconnectListeners.push(listener);
  88. },
  89. onLatencyUpdate: function (listener) {
  90. latencyUpdateListeners.push(listener);
  91. },
  92. on: function (eventName, callback) {
  93. if (!socket) {
  94. callbacks.push([eventName, callback]);
  95. return;
  96. }
  97. when(socket, function (socket) {
  98. console.log("Socket.IO: added listener for " + eventName + " events");
  99. socket.on(eventName, function (data) {
  100. console.debug("Socket.IO: " + eventName + " received");
  101. callback(data);
  102. });
  103. });
  104. },
  105. emit: function (eventName, data) {
  106. if (!socket) {
  107. return;
  108. }
  109. when(socket, function (socket) {
  110. console.debug("Socket.IO: " + eventName + " emitted");
  111. socket.emit(eventName, data);
  112. });
  113. }
  114. };
  115. setupLatencyCheck = function (eio) {
  116. var ping = eio.ping;
  117. eio.ping = function () {
  118. console.debug("PING");
  119. ping.call(eio);
  120. pingSentTime = Date.now();
  121. pingTimeoutHandle = setTimeout(function () {
  122. var latency = -1;
  123. console.log("Socket.IO: high latency (> 5000ms)");
  124. latencyUpdateListeners.forEach(function (listener) {
  125. listener(latency);
  126. });
  127. }, 5000);
  128. };
  129. eio.on("heartbeat", function () {
  130. console.debug("PONG");
  131. if (pingSentTime > 0) {
  132. clearTimeout(pingTimeoutHandle);
  133. var latency = Date.now() - pingSentTime;
  134. console.debug("Socket.IO: latency is " + latency + "ms");
  135. latencyUpdateListeners.forEach(function (listener) {
  136. listener(latency);
  137. });
  138. }
  139. pingSentTime = 0;
  140. });
  141. };
  142. return self;
  143. }
  144. );