Can exchange ready messages

This commit is contained in:
Pierre HUBERT 2019-01-25 10:47:42 +01:00
parent 89041d7009
commit 7318468d49
4 changed files with 136 additions and 1 deletions

View File

@ -62,6 +62,13 @@ class SignalExchangerClient {
*/
//onSignal = null;
/**
* Function called when we get a ready message notice
*
* @type {Function}
*/
//onReadyMessage = null;
/**
* Construct a client instance
*
@ -125,6 +132,20 @@ class SignalExchangerClient {
}
/**
* 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
*
@ -192,6 +213,26 @@ class SignalExchangerClient {
}
}
//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)

View File

@ -43,6 +43,10 @@ client.onSignal = function(signal, peerID){
}
client.onReadyMessage = function(peerID) {
console.log("Peer " + peerID + " sent a signal to inform it is ready to establish a connection...");
}
/**
* Initialize peer connection
*

View File

@ -50,6 +50,34 @@ function RemoveSocketFromList(socket){
}
/**
* Send message to a target to inform that source is ready to establish
* a connection
*
* @param {String} source_id The ID of the source of the message
* @param {String} target_id The ID of the target of the message
*/
function SendReadyMessageToClient(source_id, target_id){
let count = 0;
SocketsList.forEach(info => {
if(info.id == target_id){
SendMessageToSocket(info.socket, {
ready_msg: true,
source_id: source_id
});
count++;
}
});
return count;
}
/**
* Send a signal to several clients
*
* @param {String} content The content of the signal
* @param {String} source_id The ID of the source of the message
@ -146,6 +174,27 @@ exports.addSocket = function(socket){
});
}
//Check if the client wants to send a ready signal to another client
else if(data.ready_msg, data.target_id){
if(typeof data.target_id !== "string"){
SendMessageToSocket(socket, {
error: "Unsecure request!"
});
return;
}
//Send the signal to the target client
let number_target = SendReadyMessageToClient(client_id, data.target_id);
SendMessageToSocket(socket, {
ready_message_sent: true,
target_id: data.target_id,
number_of_targets: number_target
});
}
//Check if the client wants to send a signal to another client
else if(data.signal && data.target_id) {

View File

@ -62,6 +62,13 @@ class SignalExchangerClient {
*/
//onSignal = null;
/**
* Function called when we get a ready message notice
*
* @type {Function}
*/
//onReadyMessage = null;
/**
* Construct a client instance
*
@ -125,6 +132,20 @@ class SignalExchangerClient {
}
/**
* 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
*
@ -192,6 +213,26 @@ class SignalExchangerClient {
}
}
//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)