Can detect if a call was rejected

This commit is contained in:
Pierre HUBERT 2019-01-25 15:24:32 +01:00
parent be03249f11
commit b08255cd18
4 changed files with 399 additions and 4 deletions

View File

@ -0,0 +1,261 @@
/**
* Signal exchanger web client
*
* @author Pierre HUBERT
*/
class SignalExchangerClient {
/**
* Server domain
*
* @type {String}
*/
//domain;
/**
* Server port
*
* @type {Number}
*/
//port;
/**
* Current client ID
*
* @type {String}
*/
//clientID;
/**
* Socket connection to the server
*
* @type {WebSocket}
*/
//socket;
/**
* Function called in case of error
*
* @type {Function}
*/
//onError = null;
/**
* Function called when the connection is etablished
*
* @type {Function}
*/
//onConnected = null;
/**
* Function called when the connection to the socket is closed
*
* @type {Function}
*/
//onClosed = null;
/**
* Function called when we get a new signal information
*
* @type {Function}
*/
//onSignal = null;
/**
* Function called when we get a ready message notice
*
* @type {Function}
*/
//onReadyMessage = null;
/**
* Construct a client instance
*
* @param {String} domain The name of the signal server
* @param {Number} port The port of the server to use
* @param {String} clientID The ID of current client
*/
constructor(domain, port, clientID) {
//Save information
this.domain = domain,
this.port = port;
this.clientID = clientID;
this.socket = new WebSocket("ws://" + this.domain + ":" + this.port + "/socket");
//Add a few events listeners
this.socket.addEventListener("open", () => {
this.serverConnected();
if(this.onConnected != null)
setTimeout(this.onConnected, 10);
});
this.socket.addEventListener("message", message => {
let data;
try {
data = JSON.parse(message.data);
} catch(e){
console.error("Could not parse message from server!");
return;
}
console.log("New message from socket", data);
this.serverMessage(data);
});
this.socket.addEventListener("error", () => {
if(this.onError != null)
setTimeout(this.onError, 0);
});
this.socket.addEventListener("close", () => {
if(this.onClosed != null)
setTimeout(this.onClosed, 0);
});
}
/**
* Use this method to get the current connection status to the server
*
* @return {Boolean} TRUE if the client is connected to the server / FALSE else
*/
isConnected() {
return this.socket.readyState == WebSocket.OPEN;
}
/**
* Close the connection to the server (if connected)
*/
close() {
if(this.isConnected())
this.socket.close();
}
/**
* Method called once the client is successfully
* connected to the client
*/
serverConnected(){
//Send data to the server to identificate client
this.sendData({
client_id: this.clientID
});
}
/**
* Send ready message to a peer
*
* @param {String} peerID The ID of the target peer for the message
*/
sendReadyMessage(peerID){
this.sendData({
ready_msg: true,
target_id: peerID
});
}
/**
* Send a signal to the server
*
* @param target_id The ID of the target for the signal
* @param content Signal to send to the target
*/
sendSignal(target_id, content){
//Send directly the message to the server
this.sendData({
signal: content,
target_id: target_id
});
//Save the current signal being sent to be able to send
//it again in case of failure
this.pending_signal = content;
this.pending_signal_target = target_id;
}
/**
* Stop to try to send the current signal message in queue
*
* This does not cancel the sending of messages already sent through
* socket
*/
cancelCurrentSignal() {
this.pending_signal = undefined;
this.pending_signal_target = undefined;
}
/**
* Send data to the server
*
* @param {Object} data The data to send to the server
*/
sendData(data){
console.log("Sending data to server", data);
this.socket.send(JSON.stringify(data));
}
/**
* This method is called when the server has sent a new message to this client
*
* @param {Object} message The message sent by the server, as a JSON object
*/
serverMessage(message){
//Check if it is a callback for a pending message
if(message.signal_sent){
if(message.number_of_targets < 1 && this.pending_signal && this.pending_signal_target){
//We have to send the message again
setTimeout(() => {
this.sendSignal(this.pending_signal, this.pending_signal_target);
}, 1000);
}
else {
//Else we can remove from this class information about the signal being sent
this.cancelCurrentSignal();
}
}
//Check if message is a callback for a ready notice
else if(message.ready_message_sent){
if(message.number_of_targets < 1){
//Try to send message again
setTimeout(() => {
this.sendReadyMessage(message.target_id);
}, 1000);
}
}
// Check if message is a ready notice
else if(message.ready_msg){
if(this.onReadyMessage != null)
this.onReadyMessage(message.source_id);
}
// Check if the message is a signal
else if(message.signal){
if(this.onSignal != null)
this.onSignal(message.signal, message.source_id);
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -18,9 +18,24 @@ ComunicWeb.components.calls.callWindow = {
*/ */
var call = { var call = {
info: info, info: info,
/**
* @type {String}
*/
localPeerID: undefined,
/**
* @type {Boolean}
*/
open: true, open: true,
window: {}, window: {},
streams: {} streams: {},
/**
* @type {SignalExchangerClient}
*/
signalClient: undefined
}; };
//We have to begin to draw conversation UI //We have to begin to draw conversation UI
@ -102,6 +117,9 @@ ComunicWeb.components.calls.callWindow = {
call.close = function(){ call.close = function(){
call.open = false; call.open = false;
callContainer.remove(); callContainer.remove();
//Close sockets connections too
ComunicWeb.components.calls.callWindow.stop(call);
} }
call.window.closeButton.addEventListener("click", function(){ call.window.closeButton.addEventListener("click", function(){
@ -122,7 +140,8 @@ ComunicWeb.components.calls.callWindow = {
//Load user media //Load user media
call.setLoadingMessage("Waiting for your microphone and camera..."); call.setLoadingMessage("Waiting for your microphone and camera...");
ComunicWeb.components.calls.userMedia.get().then(function(stream){ ComunicWeb.components.calls.userMedia.get()
.then(function(stream){
//Mark as connecting //Mark as connecting
call.setLoadingMessage("Connecting..."); call.setLoadingMessage("Connecting...");
@ -131,11 +150,111 @@ ComunicWeb.components.calls.callWindow = {
return true; return true;
}).catch(function(e){ })
//Start to automaticaly refresh information the call
.then(function(){
var interval = setInterval(function(){
if(!call.open)
return clearInterval(interval);
ComunicWeb.components.calls.callWindow.refreshInfo(call);
}, 4000);
return true;
})
.catch(function(e){
console.error("Get user media error: ", e); console.error("Get user media error: ", e);
call.setLoadingMessageVisibility(false); call.setLoadingMessageVisibility(false);
return notify("Could not get your microphone and camera!", "danger"); return notify("Could not get your microphone and camera!", "danger");
}); });
//Initialize connection to signaling server
//Get current user call ID
call.info.members.forEach(function(member){
if(member.userID == userID())
call.localPeerID = member.user_call_id;
});
var config = ComunicWeb.components.calls.controller.getConfig();
call.signalClient = new SignalExchangerClient(
config.signal_server_name,
config.signal_server_port,
call.localPeerID
);
},
/**
* Refresh at a regular interval information about the call
*
* @param {Object} call Call Root object
*/
refreshInfo: function(call){
ComunicWeb.components.calls.interface.getInfo(call.info.id, function(result){
if(result.error)
return notify("Could not get information about the call!", "danger");
call.info = result;
ComunicWeb.components.calls.callWindow.gotNewCallInfo(call);
});
},
/**
* This method get called each time information about the call
* are refreshed on the server
*
* @param {Object} call Information about the call
*/
gotNewCallInfo: function(call) {
//Check if we are connected to signaling server
if(!call.signalClient.isConnected())
return;
//Check if all other members rejected call
var allRejected = true;
call.info.members.forEach(function(member){
if(member.accepted != "rejected" && member.userID != userID())
allRejected = false;
});
//Check if all call peer rejected the call
if(allRejected){
call.setLoadingMessage("All other peers rejected the call !");
setTimeout(function(){
call.close();
}, 20000);
return;
} }
},
/**
* Stop the ongoing call
*
* @param {Object} call Information about the call
*/
stop: function(call){
//Remove the call from the opened list
ComunicWeb.components.calls.currentList.removeCallFromList(call.info.id);
//Close the connection to the server
if(call.signalClient.isConnected())
call.signalClient.close();
} }
}

View File

@ -159,6 +159,10 @@ class Dev {
//JS BBCode Parser //JS BBCode Parser
"3rdparty/js-bbcode-parser/bbcode-config.js", "3rdparty/js-bbcode-parser/bbcode-config.js",
"3rdparty/js-bbcode-parser/bbcode-parser.js", "3rdparty/js-bbcode-parser/bbcode-parser.js",
//Simple peer
"3rdparty/simplepeer/simplepeer.min.js",
"3rdparty/SignalExchangerClient/SignalExchangerClient.js"
); );
/** /**