Skip to content

Commit

Permalink
Fixes - disconnect and reconnect peer client
Browse files Browse the repository at this point in the history
  • Loading branch information
Valar authored and Valar committed Apr 28, 2017
1 parent 194f842 commit 821c78f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
7 changes: 6 additions & 1 deletion js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function myjsapp(peerClient) {

$('.username-done').click(function (event) {
var username = $('#user-name').val().trim();
startPeerClient(username)
if(cookie.get('username') != username)
startPeerClient(username)
})
}

Expand All @@ -96,6 +97,7 @@ function myjsapp(peerClient) {
// Show Username Modal
var username = cookie.get('username');
if(username) {
$('#user-name').val(username)
startPeerClient(username)
} else {
$('#getUserNameModal').modal()
Expand Down Expand Up @@ -206,6 +208,9 @@ function myjsapp(peerClient) {
setMyVideo : function (stream) {
$('#my-video').prop('src', URL.createObjectURL(stream));
},
showError : function (msg) {

}
};
}

Expand Down
36 changes: 25 additions & 11 deletions js/peer-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ peerapp = (function() {
var connectedPeers = {};
var myPeerID = generateRandomID(4);
var peer;
var peerIdAlreadyTakenCount = 3;

// Compatibility shim
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

// Connect to server
function connectToServerWithId(peerId) {
myPeerID = peerId || myPeerID;
if(peer && peer.disconnected == false) {
peer.disconnect()
}
peer = new Peer(myPeerID, { host: PEER_SERVER, port: PORT, path: '/', secure: true });
peerCallbacks(peer);
}
Expand All @@ -33,7 +37,7 @@ peerapp = (function() {
}

// Data channel
// Handle a connection object.
// Handle a Text and File connection objects
function connect(c) {
console.log(c)
// Handle a chat connection.
Expand Down Expand Up @@ -66,6 +70,7 @@ peerapp = (function() {
}
}

// Handle Audio and Video Calls
function callConnect(call) {

// Hang up on an existing call if present
Expand Down Expand Up @@ -116,11 +121,14 @@ peerapp = (function() {
});

peer.on('disconnected', function(conn) {
console.log("Peer connection disconnected");
console.log("Peer connection disconnected : " + conn);
console.log(new Date());
if(conn != myPeerID) {
return
}
setTimeout(function () {
connectToServerWithId();
}, 3000);
connectToServerWithId();
}, 5000);

// peer.reconnect()
});
Expand All @@ -130,8 +138,14 @@ peerapp = (function() {
console.log("Peer connection error:")
console.log(err)
if("unavailable-id" == err.type) {
// ID Already taken, so assigning random ID
myPeerID = generateRandomID(4);
// ID Already taken, so assigning random ID after 3 attempts
peerIdAlreadyTakenCount++;
if(peerIdAlreadyTakenCount >= 3) {
peerIdAlreadyTakenCount = 0;
myPeerID = generateRandomID(4);
}
} else if ("peer-unavailable" == err.type) {
myapp.showError(err.message)
}
});
};
Expand All @@ -153,11 +167,11 @@ peerapp = (function() {
c.on('error', function(err) { alert(err); });

// File Sharing
var f = peer.connect(requestedPeer, { label: 'file', reliable: true });
f.on('open', function() {
connect(f);
});
f.on('error', function(err) { alert(err); });
// var f = peer.connect(requestedPeer, { label: 'file', reliable: true });
// f.on('open', function() {
// connect(f);
// });
// f.on('error', function(err) { alert(err); });
}
}

Expand Down

0 comments on commit 821c78f

Please sign in to comment.