Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added closeEntireSession and onUserIdAlreadyTaken.
  • Loading branch information
muaz-khan committed Mar 14, 2016
1 parent 22f0577 commit cab5ceb
Show file tree
Hide file tree
Showing 46 changed files with 2,013 additions and 60 deletions.
102 changes: 99 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ All files from `/dist` directory are available on CDN: `https://cdn.webrtc-exper
<script src="https://cdn.webrtc-experiment.com:443/rmc3.min.js"></script>

<!-- or specific version -->
<script src="https://github.com/muaz-khan/RTCMultiConnection/releases/download/3.2.93/rmc3.min.js"></script>
<script src="https://github.com/muaz-khan/RTCMultiConnection/releases/download/3.2.94/rmc3.min.js"></script>
```

If you're sharing files, you also need to link:
Expand All @@ -92,7 +92,7 @@ If you're sharing files, you also need to link:
<script src="https://cdn.webrtc-experiment.com:443/rmc3.fbr.min.js"></script>

<!-- or specific version -->
<script src="https://github.com/muaz-khan/RTCMultiConnection/releases/download/3.2.93/rmc3.fbr.min.js"></script>
<script src="https://github.com/muaz-khan/RTCMultiConnection/releases/download/3.2.94/rmc3.fbr.min.js"></script>
```

> You can link multiple files from `dev` directory. Order doesn't matters.
Expand All @@ -111,7 +111,7 @@ Either via `config.json` file:
or override in your HTML code:

```javascript
connection.socketURL = 'http://yourdomain.com:8080/';
connection.socketURL = 'http:s//yourdomain.com:9001/';

// if your server is already having "message" event
// then you can use something else, unique.
Expand Down Expand Up @@ -144,6 +144,26 @@ Here is a demo explaining how to use above `socketURL`:
require('./Signaling-Server.js')(httpServerHandlerOrPort);
```

If you're using [expressjs](http://stackoverflow.com/a/35985109/552182):

```javascript
var fs = require('fs');

var options = {
key: fs.readFileSync('fake-keys/privatekey.pem'),
cert: fs.readFileSync('fake-keys/certificate.pem')
};

var express = require("express"),
http = require("https"), // Use HTTPs here -------------
app = express(),
server = http.createServer(options, app);

server.listen(3000);

require('./Signaling-Server.js')(server);
```

## Migrating from older versions?

Use `streamEvents` instead of `connection.streams`:
Expand Down Expand Up @@ -867,6 +887,11 @@ Change userid and update userid among all connected peers:

```javascript
connection.changeUserId('new-userid');

// or callback to check if userid is successfully changed
connection.changeUserId('new-userid', function() {
alert('Your userid is successfully changed to: ' + connection.userid);
});
```

## `closeBeforeUnload`
Expand All @@ -880,6 +905,56 @@ window.onbeforeunlaod = function() {
};
```

## `closeEntireSession`

You can skip using `autoCloseEntireSession`. You can keep session/room opened whenever/wherever required and dynamically close the entire room using this method.

```javascript
connection.closeEntireSession();

// or callback
connection.closeEntireSession(function() {
alert('Entire session has been closed.');
});

// or before leaving a page
connection.closeBeforeUnload = false;
window.onbeforeunlaod = function() {
connection.closeEntireSession();
};
```

## `onUserIdAlreadyTaken`

This event is fired if two users tries to open same room.

```javascript
connection.onUserIdAlreadyTaken = function(useridAlreadyTaken, yourNewUserId) {
if (connection.enableLogs) {
console.warn('Userid already taken.', useridAlreadyTaken, 'Your new userid:', yourNewUserId);
}

connection.join(useridAlreadyTaken);
};
```

Above event gets fired out of this code:

```javascript
moderator1.open('same-roomid');
moderator2.open('same-roomid');
```

## `onEntireSessionClosed`

You can tell users that room-moderator closed entire session:

```javascript
connection.onEntireSessionClosed = function(event) {
console.info('Entire session is closed: ', event.sessionid, event.extra);
};
```

## `captureUserMedia`

* http://www.rtcmulticonnection.org/docs/captureUserMedia/
Expand Down Expand Up @@ -1250,6 +1325,27 @@ if(connection.DetectRTC.browser.name === 'Firefox') {

RTCMultiConnection-v3.0 supports `cordova` based iOS/android apps.

Copy/paste entire [`rmc3.min.js`](https://github.com/muaz-khan/RTCMultiConnection/tree/master/dist/rmc3.min.js) file inside `deviceready` callback:

```javascript
// please read below comments:
// normally you can place below code in your www/js/index.js file
document.addEventListener('deviceready', function() {
// copy/paste entire/all code from "rmc3.min.js" file
// here --- exact here
// paste inside this callback
// if you will NOT do this, RTCMultiConnection will fail on cordova-based apps
// again, you MUST NOT link rmc3.min.js
// instead copy/paste all the codes here

// you can put your custom-ui-codes here
// e.g.
// var connection = new RTCMultiConnection();
}, false);
```

Installations:

```
sudo npm install cordova -g
sudo npm install xcode -g
Expand Down
88 changes: 76 additions & 12 deletions RTCMultiConnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last time updated: 2016-03-02 9:37:16 AM UTC
// Last time updated: 2016-03-14 12:12:30 PM UTC

// ______________________________
// RTCMultiConnection-v3.0 (Beta)
Expand Down Expand Up @@ -498,6 +498,8 @@
if (connection.peers[participant] && connection.peers[participant].peer) {
connection.peers[participant].peer.close();
}

delete connection.peers[participant];
});

if (!dontCloseSocket) {
Expand All @@ -512,9 +514,9 @@
window.addEventListener('beforeunload', beforeUnload, false);

connection.userid = getRandomString();
connection.changeUserId = function(newUserId) {
connection.changeUserId = function(newUserId, callback) {
connection.userid = newUserId || getRandomString();
socket.emit('changed-uuid', connection.userid);
socket.emit('changed-uuid', connection.userid, callback || function() {});
};

connection.observers = {
Expand Down Expand Up @@ -793,6 +795,32 @@
beforeUnload(false, true);
};

connection.closeEntireSession = function(callback) {
callback = callback || function() {};
socket.emit('close-entire-session', function looper() {
if (connection.getAllParticipants().length) {
setTimeout(looper, 100);
return;
}

connection.onEntireSessionClosed({
sessionid: connection.sessionid,
userid: connection.userid,
extra: connection.extra
});

connection.changeUserId(null, function() {
connection.close();
callback();
});
});
};

connection.onEntireSessionClosed = function(event) {
if (!connection.enableLogs) return;
console.info('Entire session is closed: ', event.sessionid, event.extra);
};

connection.onstream = function(e) {
var parentNode = connection.videosContainer;
parentNode.insertBefore(e.mediaElement, parentNode.firstChild);
Expand Down Expand Up @@ -838,9 +866,21 @@
}
};

connection.addStream = function(session) {
connection.addStream = function(session, remoteUserId) {
if (!!session.getAudioTracks) {
if (connection.attachStreams.indexOf(session) === -1) {
if (!session.streamid) {
session.streamid = session.id;
}

connection.attachStreams.push(session);
}
connection.renegotiate(remoteUserId);
return;
}

if (isData(session)) {
connection.renegotiate();
connection.renegotiate(remoteUserId);
return;
}

Expand Down Expand Up @@ -887,7 +927,7 @@
return callback();
}

connection.renegotiate();
connection.renegotiate(remoteUserId);
},
onLocalMediaError: function(error, constraints) {
mPeer.onLocalMediaError(error, constraints);
Expand All @@ -902,7 +942,7 @@
return callback();
}

connection.renegotiate();
connection.renegotiate(remoteUserId);
},
localMediaConstraints: localMediaConstraints || {
audio: session.audio ? connection.mediaConstraints.audio : false,
Expand Down Expand Up @@ -1509,6 +1549,14 @@
if (!!forceOptions.autoOpenOrJoin) {
connection.openOrJoin(connection.sessionid);
}

connection.onUserIdAlreadyTaken = function(useridAlreadyTaken, yourNewUserId) {
if (connection.enableLogs) {
console.warn('Userid already taken.', useridAlreadyTaken, 'Your new userid:', yourNewUserId);
}

connection.join(useridAlreadyTaken);
};
}

function SocketConnection(connection, connectCallback) {
Expand Down Expand Up @@ -1772,6 +1820,22 @@
});
});

socket.on('closed-entire-session', function(sessionid, extra) {
connection.leave();
connection.onEntireSessionClosed({
sessionid: sessionid,
userid: sessionid,
extra: extra
});
});

socket.on('userid-already-taken', function(useridAlreadyTaken, yourNewUserId) {
connection.isInitiator = false;
connection.userid = yourNewUserId;

connection.onUserIdAlreadyTaken(useridAlreadyTaken, yourNewUserId);
})

socket.on('logs', function(log) {
if (!connection.enableLogs) return;
console.debug('server-logs', log);
Expand Down Expand Up @@ -1870,7 +1934,7 @@
streamsToShare: userPreferences.streamsToShare || {},
rtcMultiConnection: connection,
connectionDescription: userPreferences.connectionDescription,
remoteUserId: remoteUserId,
userid: remoteUserId,
localPeerSdpConstraints: userPreferences.localPeerSdpConstraints,
remotePeerSdpConstraints: userPreferences.remotePeerSdpConstraints,
dontGetRemoteStream: !!userPreferences.dontGetRemoteStream,
Expand Down Expand Up @@ -3794,7 +3858,7 @@
var connection = config.rtcMultiConnection;

this.extra = config.remoteSdp ? config.remoteSdp.extra : connection.extra;
this.remoteUserId = config.remoteUserId;
this.userid = config.userid;
this.streams = [];
this.channels = [];
this.connectionDescription = config.connectionDescription;
Expand Down Expand Up @@ -3915,8 +3979,8 @@

peer.oniceconnectionstatechange = peer.onsignalingstatechange = function() {
var extra = that.extra;
if (connection.peers[that.remoteUserId]) {
extra = connection.peers[that.remoteUserId].extra || extra;
if (connection.peers[that.userid]) {
extra = connection.peers[that.userid].extra || extra;
}

if (!peer) {
Expand All @@ -3928,7 +3992,7 @@
iceGatheringState: peer.iceGatheringState,
signalingState: peer.signalingState,
extra: extra,
userid: that.remoteUserId
userid: that.userid
});
};

Expand Down
10 changes: 5 additions & 5 deletions RTCMultiConnection.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit cab5ceb

Please sign in to comment.