File: source/peer-privileged.js

  1. /**
  2. * <blockquote class="info">
  3. * Note that this feature requires <code>"isPrivileged"</code> flag to be enabled for the App Key
  4. * provided in the <a href="#method_init"><code>init()</code> method</a>, as only Users connecting using
  5. * the App Key with this flag enabled (which we call privileged Users / Peers) can retrieve the list of
  6. * Peer IDs from Rooms within the same App space.
  7. * <a href="http://support.temasys.io/support/solutions/articles/12000012342-what-is-a-privileged-key-">
  8. * Read more about privileged App Key feature here</a>.
  9. * </blockquote>
  10. * Function that retrieves the list of Peer IDs from Rooms within the same App space.
  11. * @method getPeers
  12. * @param {Boolean} [showAll=false] The flag if Signaling server should also return the list of privileged Peer IDs.
  13. * <small>By default, the Signaling server does not include the list of privileged Peer IDs in the return result.</small>
  14. * @param {Function} [callback] The callback function fired when request has completed.
  15. * <small>Function parameters signature is <code>function (error, success)</code></small>
  16. * <small>Function request completion is determined by the <a href="#event_getPeersStateChange">
  17. * <code>getPeersStateChange</code> event</a> triggering <code>state</code> parameter payload value as
  18. * <code>RECEIVED</code> for request success.</small>
  19. * [Rel: Skylink.GET_PEERS_STATE]
  20. * @param {Error|String} callback.error The error result in request.
  21. * <small>Defined as <code>null</code> when there are no errors in request</small>
  22. * <small>Object signature is the <code>getPeers()</code> error when retrieving list of Peer IDs from Rooms
  23. * within the same App space.</small>
  24. * @param {JSON} callback.success The success result in request.
  25. * <small>Defined as <code>null</code> when there are errors in request</small>
  26. * <small>Object signature matches the <code>peerList</code> parameter payload received in the
  27. * <a href="#event_getPeersStateChange"><code>getPeersStateChange</code> event</a>.</small>
  28. * @trigger <ol class="desc-seq">
  29. * <li>If App Key provided in the <a href="#method_init"><code>init()</code> method</a> is not
  30. * a Privileged enabled Key: <ol><li><b>ABORT</b> and return error.</li></ol></li>
  31. * <li>Retrieves the list of Peer IDs from Rooms within the same App space. <ol>
  32. * <li><a href="#event_getPeersStateChange"><code>getPeersStateChange</code> event</a> triggers parameter
  33. * payload <code>state</code> value as <code>ENQUIRED</code>.</li>
  34. * <li>If received list from Signaling server successfully: <ol>
  35. * <li><a href="#event_getPeersStateChange"><code>getPeersStateChange</code> event</a> triggers parameter
  36. * payload <code>state</code> value as <code>RECEIVED</code>.</li></ol></li></ol>
  37. * @example
  38. * // Example 1: Retrieving the un-privileged Peers
  39. * skylinkDemo.joinRoom(function (jRError, jRSuccess) {
  40. * if (jRError) return;
  41. * skylinkDemo.getPeers(function (error, success) {
  42. * if (error) return;
  43. * console.log("The list of only un-privileged Peers in the same App space ->", success);
  44. * });
  45. * });
  46. *
  47. * // Example 2: Retrieving the all Peers (privileged or un-privileged)
  48. * skylinkDemo.joinRoom(function (jRError, jRSuccess) {
  49. * if (jRError) return;
  50. * skylinkDemo.getPeers(true, function (error, success) {
  51. * if (error) return;
  52. * console.log("The list of all Peers in the same App space ->", success);
  53. * });
  54. * });
  55. * @for Skylink
  56. * @since 0.6.1
  57. */
  58. Skylink.prototype.getPeers = function(showAll, callback){
  59. var self = this;
  60. if (!self._isPrivileged){
  61. log.warn('Please upgrade your key to privileged to use this function');
  62. return;
  63. }
  64. if (!self._initOptions.appKey){
  65. log.warn('App key is not defined. Please authenticate again.');
  66. return;
  67. }
  68.  
  69. // Only callback is provided
  70. if (typeof showAll === 'function'){
  71. callback = showAll;
  72. showAll = false;
  73. }
  74.  
  75. self._sendChannelMessage({
  76. type: self._SIG_MESSAGE_TYPE.GET_PEERS,
  77. showAll: showAll || false
  78. });
  79.  
  80. self._trigger('getPeersStateChange',self.GET_PEERS_STATE.ENQUIRED, self._user.sid, null);
  81.  
  82. log.log('Enquired server for peers within the realm');
  83.  
  84. if (typeof callback === 'function'){
  85. self.once('getPeersStateChange', function(state, privilegedPeerId, peerList){
  86. callback(null, peerList);
  87. }, function(state, privilegedPeerId, peerList){
  88. return state === self.GET_PEERS_STATE.RECEIVED;
  89. });
  90. }
  91.  
  92. };
  93.  
  94. /**
  95. * <blockquote class="info">
  96. * Note that this feature requires <code>"isPrivileged"</code> flag to be enabled and
  97. * <code>"autoIntroduce"</code> flag to be disabled for the App Key provided in the
  98. * <a href="#method_init"><code>init()</code> method</a>, as only Users connecting using
  99. * the App Key with this flag enabled (which we call privileged Users / Peers) can retrieve the list of
  100. * Peer IDs from Rooms within the same App space.
  101. * <a href="http://support.temasys.io/support/solutions/articles/12000012342-what-is-a-privileged-key-">
  102. * Read more about privileged App Key feature here</a>.
  103. * </blockquote>
  104. * Function that selects and introduces a pair of Peers to start connection with each other.
  105. * @method introducePeer
  106. * @param {String} sendingPeerId The Peer ID to be connected with <code>receivingPeerId</code>.
  107. * @param {String} receivingPeerId The Peer ID to be connected with <code>sendingPeerId</code>.
  108. * @trigger <ol class="desc-seq">
  109. * <li>If App Key provided in the <a href="#method_init"><code>init()</code> method</a> is not
  110. * a Privileged enabled Key: <ol><li><b>ABORT</b> and return error.</li></ol></li>
  111. * <li>Starts sending introduction request for the selected pair of Peers to the Signaling server. <ol>
  112. * <li><a href="#event_introduceStateChange"><code>introduceStateChange</code> event</a> triggers parameter
  113. * payload <code>state</code> value as <code>INTRODUCING</code>.</li>
  114. * <li>If received errors from Signaling server: <ol>
  115. * <li><a href="#event_introduceStateChange"><code>introduceStateChange</code> event</a> triggers parameter
  116. * payload <code>state</code> value as <code>ERROR</code>.</li></ol></li></ol></li></ol>
  117. * @example
  118. * // Example 1: Introduce a pair of Peers
  119. * skylinkDemo.on("introduceStateChange", function (state, privilegedPeerId, sendingPeerId, receivingPeerId) {
  120. * if (state === skylinkDemo.INTRODUCE_STATE.INTRODUCING) {
  121. * console.log("Peer '" + sendingPeerId + "' has been introduced to '" + receivingPeerId + "'");
  122. * }
  123. * });
  124. *
  125. * skylinkDemo.joinRoom(function (jRError, jRSuccess) {
  126. * if (jRError) return;
  127. * skylinkDemo.getPeers(function (gPError, gPSuccess) {
  128. * if (gPError) return;
  129. * skylinkDemo.introducePeer(gPSuccess.roomName[0], gPSuccess.roomName[1]);
  130. * });
  131. * });
  132. * @for Skylink
  133. * @since 0.6.1
  134. */
  135. Skylink.prototype.introducePeer = function(sendingPeerId, receivingPeerId){
  136. var self = this;
  137. if (!self._isPrivileged){
  138. log.warn('Please upgrade your key to privileged to use this function');
  139. self._trigger('introduceStateChange', self.INTRODUCE_STATE.ERROR, self._user.sid, sendingPeerId, receivingPeerId, 'notPrivileged');
  140. return;
  141. }
  142.  
  143. var introduceMsg = {
  144. type: self._SIG_MESSAGE_TYPE.INTRODUCE,
  145. sendingPeerId: sendingPeerId,
  146. receivingPeerId: receivingPeerId
  147. };
  148.  
  149. self._sendChannelMessage(introduceMsg);
  150. self._handleSessionStats(introduceMsg);
  151. self._trigger('introduceStateChange', self.INTRODUCE_STATE.INTRODUCING, self._user.sid, sendingPeerId, receivingPeerId, null);
  152. log.log('Introducing',sendingPeerId,'to',receivingPeerId);
  153. };
  154.  
  155.