Skip to content

Commit

Permalink
Merge pull request #307 from Temasys/0.6.x/refreshDataChannel
Browse files Browse the repository at this point in the history
 refreshDatachannel(peerId) to revive the MESSAGING dataChannel
  • Loading branch information
oooookk7 authored Feb 21, 2018
2 parents 1ed3d7f + 0ada9ad commit 0d93e42
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 7 deletions.
150 changes: 147 additions & 3 deletions doc/classes/Skylink.html
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,15 @@ <h2 id="current-doc-selected-title"></h2>



</span>
</a>

<a href="#method_refreshDatachannel" class="list-group-item code-menu-item method-item ">
&nbsp;&nbsp;refreshDatachannel
<span class="settings">



</span>
</a>

Expand Down Expand Up @@ -4863,7 +4872,7 @@ <h3 class="code-item-name"><code>_closeDataChannel</code></h3>


<small>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l292"><code>source&#x2F;data-channel.js:292</code></a>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l339"><code>source&#x2F;data-channel.js:339</code></a>
</small>


Expand Down Expand Up @@ -5972,7 +5981,7 @@ <h3 class="code-item-name"><code>_getDataChannelBuffer</code></h3>


<small>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l183"><code>source&#x2F;data-channel.js:183</code></a>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l230"><code>source&#x2F;data-channel.js:230</code></a>
</small>


Expand Down Expand Up @@ -11043,7 +11052,7 @@ <h3 class="code-item-name"><code>_sendMessageToDataChannel</code></h3>


<small>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l217"><code>source&#x2F;data-channel.js:217</code></a>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l264"><code>source&#x2F;data-channel.js:264</code></a>
</small>


Expand Down Expand Up @@ -21259,6 +21268,141 @@ <h4>Parameters:</h4>



<hr>
</div>

<div id="method_refreshDatachannel" class="code-item method item method-item ">
<h3 class="code-item-name"><code>refreshDatachannel</code></h3>

<div class="args">
<span class="paren">(</span>


<code class="optional">[peerId]</code>

<i class="seperator">,&nbsp;&nbsp;</i>

<span class="paren">)</span>
</div>


<div class="code-item-properties">
<div class="properties-wrapper">

























</div>

<div class="code-item-information">





<!---->

</div>


<small>
<i class="fa fa-chevron-right"></i>&nbsp;&nbsp;Defined in: <a href="../files/source_data-channel.js.html#l183"><code>source&#x2F;data-channel.js:183</code></a>
</small>


<small>

<i class="fa">&nbsp;&nbsp;&nbsp;</i>&nbsp;&nbsp;Available since <span class="badge">0.6.30</span>



</small>
</div>

<div class="code-item-description">
<p>Function that refreshes the main messaging Datachannel.</p>

</div>


<div class="code-item-section">
<h4>Example:</h4>
<pre class="example-content prettyprint">
<p> // Example 1: Retrieve offerer and refresh datachannel:
skylink.on(&quot;dataChannelState&quot;, function (state, peerId, error, channelName, channelType) {
if (channelType === skylink.DATA_CHANNEL_TYPE.MESSAGING &amp;&amp;
state === skylink.DATA_CHANNEL_STATE.CLOSED) {
var userWeight = skylink.getPeerInfo().config.priorityWeight;
var peerWeight = skylink.getPeerInfo(peerId).config.priorityWeight;
// Determine who is offerer because as per SM protocol, higher weight is offerer
if (userWeight &gt; peerWeight) {
skylink.refreshDatachannel(peerId);
}
}
});</p>

</pre>
</div>





<div class="code-item-section">
<h4>Events Sequence:</h4>
<span class="code-item-trigger"><small>There is no event sequence for this method.</small></span>
</div>



<div class="code-item-section">
<h4>Parameters:</h4>

<ul class="params-list">

<li class="param">
<code class="param-name">peerId</code>
<span class="type"><b>{</b>String<b>}</b></span>

<span class="label">Optional</span>


<div class="param-description">

<p>The target Peer ID to retrieve connection stats from.</p>

</div>


</li>

</ul>
</div>




<hr>
</div>

Expand Down
24 changes: 22 additions & 2 deletions doc/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,26 @@
{
"file": "source/data-channel.js",
"line": 183,
"description": "Function that refreshes the main messaging Datachannel.",
"itemtype": "method",
"name": "refreshDatachannel",
"params": [
{
"name": "peerId",
"description": "The target Peer ID to retrieve connection stats from.",
"type": "String",
"optional": true
}
],
"example": [
"\n // Example 1: Retrieve offerer and refresh datachannel:\n skylink.on(\"dataChannelState\", function (state, peerId, error, channelName, channelType) {\n if (channelType === skylink.DATA_CHANNEL_TYPE.MESSAGING &&\n state === skylink.DATA_CHANNEL_STATE.CLOSED) {\n var userWeight = skylink.getPeerInfo().config.priorityWeight;\n var peerWeight = skylink.getPeerInfo(peerId).config.priorityWeight;\n // Determine who is offerer because as per SM protocol, higher weight is offerer\n if (userWeight > peerWeight) {\n skylink.refreshDatachannel(peerId);\n }\n }\n });"
],
"since": "0.6.30",
"class": "Skylink"
},
{
"file": "source/data-channel.js",
"line": 230,
"description": "Function that returns the Datachannel buffer threshold and amount.",
"itemtype": "method",
"name": "_getDataChannelBuffer",
Expand All @@ -2758,7 +2778,7 @@
},
{
"file": "source/data-channel.js",
"line": 217,
"line": 264,
"description": "Function that sends data over the Datachannel connection.",
"itemtype": "method",
"name": "_sendMessageToDataChannel",
Expand All @@ -2769,7 +2789,7 @@
},
{
"file": "source/data-channel.js",
"line": 292,
"line": 339,
"description": "Function that stops the Datachannel connection and removes object references.",
"itemtype": "method",
"name": "_closeDataChannel",
Expand Down
47 changes: 47 additions & 0 deletions doc/files/source_data-channel.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,53 @@ <h1 class="file-heading">File: source/data-channel.js</h1>
}
};

/**
* Function that refreshes the main messaging Datachannel.
* @method refreshDatachannel
* @param {String} [peerId] The target Peer ID to retrieve connection stats from.
* @example
* // Example 1: Retrieve offerer and refresh datachannel:
* skylink.on(&quot;dataChannelState&quot;, function (state, peerId, error, channelName, channelType) {
* if (channelType === skylink.DATA_CHANNEL_TYPE.MESSAGING &amp;&amp;
* state === skylink.DATA_CHANNEL_STATE.CLOSED) {
* var userWeight = skylink.getPeerInfo().config.priorityWeight;
* var peerWeight = skylink.getPeerInfo(peerId).config.priorityWeight;
* // Determine who is offerer because as per SM protocol, higher weight is offerer
* if (userWeight &gt; peerWeight) {
* skylink.refreshDatachannel(peerId);
* }
* }
* });
* @for Skylink
* @since 0.6.30
*/

Skylink.prototype.refreshDatachannel = function (peerId) {

var self = this;
if(!self._dataChannels[peerId]) {
var channelName = self._dataChannels[peerId].main.channelName;
var channelType = self._dataChannels[peerId].main.channelType;
var channelProp = channelType === self.DATA_CHANNEL_TYPE.MESSAGING ? &#x27;main&#x27; : channelName;
var bufferThreshold= self._dataChannels[peerId].main.channel.bufferedAmountLowThreshold;

if (channelType === self.DATA_CHANNEL_TYPE.MESSAGING) {
setTimeout(function () {
if (self._peerConnections[peerId] &amp;&amp;
self._peerConnections[peerId].signalingState !== self.PEER_CONNECTION_STATE.CLOSED &amp;&amp;
(self._peerConnections[peerId].localDescription &amp;&amp;
self._peerConnections[peerId].localDescription.type === self.HANDSHAKE_PROGRESS.OFFER)) {
log.debug([peerId, &#x27;RTCDataChannel&#x27;, channelProp, &#x27;Reviving Datachannel connection&#x27;]);
self._createDataChannel(peerId, channelName, bufferThreshold, true);
}
}, 100);
}
}
else {
log.debug([peerId, &#x27;RTCDataChannel&#x27;, &#x27;Not a valid Datachannel connection&#x27;]);
}
};

/**
* Function that returns the Datachannel buffer threshold and amount.
* @method _getDataChannelBuffer
Expand Down
56 changes: 54 additions & 2 deletions source/data-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,54 @@ Skylink.prototype._createDataChannel = function(peerId, dataChannel, bufferThres
}
};

/**
* Function that refreshes the main messaging Datachannel.
* @method refreshDatachannel
* @param {String} [peerId] The target Peer ID to retrieve connection stats from.
* @example
* // Example 1: Retrieve offerer and refresh datachannel:
* skylink.on("dataChannelState", function (state, peerId, error, channelName, channelType) {
* if (channelType === skylink.DATA_CHANNEL_TYPE.MESSAGING &&
* state === skylink.DATA_CHANNEL_STATE.CLOSED) {
* var userWeight = skylink.getPeerInfo().config.priorityWeight;
* var peerWeight = skylink.getPeerInfo(peerId).config.priorityWeight;
* // Determine who is offerer because as per SM protocol, higher weight is offerer
* if (userWeight > peerWeight) {
* skylink.refreshDatachannel(peerId);
* }
* }
* });
* @for Skylink
* @since 0.6.30
*/

Skylink.prototype.refreshDatachannel = function (peerId) {

var self = this;
if(self._dataChannels[peerId] && self._dataChannels[peerId]["main"] && self._dataChannels[peerId].main.channel) {
var channelName = self._dataChannels[peerId].main.channelName;
var channelType = self._dataChannels[peerId].main.channelType;
var channelProp = 'main';
var bufferThreshold= self._dataChannels[peerId].main.channel.bufferedAmountLowThreshold || 0;

if (channelType === self.DATA_CHANNEL_TYPE.MESSAGING) {
setTimeout(function () {
if (self._peerConnections[peerId] &&
self._peerConnections[peerId].signalingState !== self.PEER_CONNECTION_STATE.CLOSED &&
(self._peerConnections[peerId].localDescription &&
self._peerConnections[peerId].localDescription.type === self.HANDSHAKE_PROGRESS.OFFER)) {
self._closeDataChannel(peerId, 'main', true);
log.debug([peerId, 'RTCDataChannel', channelProp, 'Reviving Datachannel connection']);
self._createDataChannel(peerId, channelName, bufferThreshold, true);
}
}, 100);
}
}
else {
log.debug([peerId, 'RTCDataChannel', 'Not a valid Datachannel connection']);
}
};

/**
* Function that returns the Datachannel buffer threshold and amount.
* @method _getDataChannelBuffer
Expand Down Expand Up @@ -296,7 +344,7 @@ Skylink.prototype._sendMessageToDataChannel = function(peerId, data, channelProp
* @for Skylink
* @since 0.1.0
*/
Skylink.prototype._closeDataChannel = function(peerId, channelProp) {
Skylink.prototype._closeDataChannel = function(peerId, channelProp, isCloseMainChannel) {
var self = this;

if (!self._dataChannels[peerId]) {
Expand All @@ -321,7 +369,11 @@ Skylink.prototype._closeDataChannel = function(peerId, channelProp) {
}
};

if (!channelProp || channelProp === 'main') {
if(isCloseMainChannel)
{
closeFn(channelProp);
}
else if (!channelProp || channelProp === 'main') {
for (var channelNameProp in self._dataChannels) {
if (self._dataChannels[peerId].hasOwnProperty(channelNameProp)) {
if (self._dataChannels[peerId][channelNameProp]) {
Expand Down

0 comments on commit 0d93e42

Please sign in to comment.