h5sevent.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. *=================Event API
  3. *
  4. */
  5. /**
  6. * Interface with h5s Event API
  7. * @constructor
  8. * @param
  9. var conf = {
  10. protocol: window.location.protocol, // {string} - 'http:' or 'https:'
  11. host: window.location.host, //{string} - 'localhost:8080'
  12. rootpath:window.location.pathname, // {string} - path of the app running
  13. callback: EventCB, //{function}(event(string), userdata(object))
  14. userdata: user data // user data
  15. session:'c1782caf-b670-42d8-ba90-2244d0b0ee83', //{string} - session got from login
  16. consolelog: 'true' // 'true' or 'false' enable/disable console.log
  17. };
  18. */
  19. function H5sEvent(conf)
  20. {
  21. this.wsSocket;
  22. this.keepaliveTimerId;
  23. this.bNeedReconnect = false;
  24. this.bDisConnected = false;
  25. this._debug = true;
  26. if (conf.consolelog !== undefined)
  27. {
  28. if (conf.consolelog === 'false')
  29. {
  30. this._debug = false;
  31. }
  32. }
  33. this._conf = conf;
  34. }
  35. H5sEvent.prototype.ReconnectFunction = function()
  36. {
  37. //if(this._debug === true) console.log('Try Reconnect...', this.bNeedReconnect);
  38. if (this.bNeedReconnect === true)
  39. {
  40. if(this._debug === true) console.log('Reconnect...');
  41. this.setupWebSocket(this._token);
  42. this.bNeedReconnect = false;
  43. }
  44. //if(this._debug === true) console.log('Try Reconnect...', this.bNeedReconnect);
  45. }
  46. H5sEvent.prototype.H5SWebSocketClient = function(h5spath)
  47. {
  48. var socket;
  49. if(this._debug === true) console.log("H5SWebSocketClient");
  50. try {
  51. //alert(this._conf.protocol);
  52. if (this._conf.protocol == "http:")
  53. {
  54. if (typeof MozWebSocket != "undefined")
  55. {
  56. socket = new MozWebSocket('ws://' + this._conf.host + h5spath);
  57. }else
  58. {
  59. socket = new WebSocket('ws://' + this._conf.host + h5spath);
  60. }
  61. }
  62. if (this._conf.protocol == "https:")
  63. {
  64. //alert(this._conf.host);
  65. if(this._debug === true) console.log(this._conf.host);
  66. if (typeof MozWebSocket != "undefined")
  67. {
  68. socket = new MozWebSocket('wss://' + this._conf.host + h5spath);
  69. }else
  70. {
  71. socket = new WebSocket('wss://' + this._conf.host + h5spath);
  72. }
  73. }
  74. if(this._debug === true) console.log(this._conf.host);
  75. } catch (e) {
  76. alert('error');
  77. return;
  78. }
  79. return socket;
  80. }
  81. H5sEvent.prototype.keepaliveTimer = function()
  82. {
  83. try {
  84. var j = {};
  85. j.type = "keepalive";
  86. this.wsSocket.send(JSON.stringify(j));
  87. } catch (e) {
  88. if(this._debug === true) console.log(e);
  89. }
  90. }
  91. H5sEvent.prototype.onWebSocketData = function(msg)
  92. {
  93. if (this._conf.callback != undefined)
  94. {
  95. this._conf.callback(msg.data, this._conf.userdata);
  96. }
  97. }
  98. H5sEvent.prototype.setupWebSocket = function(token)
  99. {
  100. var h5spath = "api/v1/h5seventapi";
  101. h5spath = this._conf.rootpath + h5spath + '?session=' + this._conf.session;
  102. if(this._debug === true) console.log(h5spath);
  103. this.wsSocket = this.H5SWebSocketClient(h5spath);
  104. if(this._debug === true) console.log("setupWebSocket", this.wsSocket);
  105. this.wsSocket.binaryType = 'arraybuffer';
  106. this.wsSocket.h5 = this;
  107. this.wsSocket.onmessage = this.onWebSocketData.bind(this);
  108. this.wsSocket.onopen = function()
  109. {
  110. if(this.h5._debug === true) console.log("wsSocket.onopen", this.h5);
  111. this.h5.keepaliveTimerId = setInterval(this.h5.keepaliveTimer.bind(this.h5), 1000);
  112. }
  113. this.wsSocket.onclose = function () {
  114. if(this.h5._debug === true) console.log("wsSocket.onclose", this.h5);
  115. if (this.h5.bDisConnected === true)
  116. {
  117. if(this.h5._debug === true) console.log("wsSocket.onclose disconnect");
  118. }else
  119. {
  120. this.h5.bNeedReconnect = true;
  121. }
  122. this.h5.CleanupWebSocket(this.h5);
  123. }
  124. }
  125. H5sEvent.prototype.CleanupWebSocket = function(h5sPlayer)
  126. {
  127. if(h5sPlayer._debug === true) console.log('CleanupWebSocket', h5sPlayer);
  128. clearInterval(h5sPlayer.keepaliveTimerId);
  129. h5sPlayer.emptyBuffCnt = 0;
  130. h5sPlayer.lastBuffTime = 0;
  131. h5sPlayer.buffTimeSameCnt = 0;
  132. }
  133. /**
  134. * Connect a websocket Stream to videoElement
  135. */
  136. H5sEvent.prototype.connect = function() {
  137. /* start connect to server */
  138. this.setupWebSocket(this._token);
  139. this.reconnectTimerId = setInterval(this.ReconnectFunction.bind(this), 3000);
  140. }
  141. /**
  142. * Disconnect a websocket Stream and clear videoElement source
  143. */
  144. H5sEvent.prototype.disconnect = function() {
  145. if(this._debug === true) console.log("disconnect", this);
  146. this.bDisConnected = true;
  147. clearInterval(this.reconnectTimerId);
  148. if (this.wsSocket != null)
  149. {
  150. this.wsSocket.close();
  151. this.wsSocket = null;
  152. }
  153. if(this._debug === true) console.log("disconnect", this);
  154. }