From 7318468d49bbf4e46b1307a9be3f4b5615c0b48b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 25 Jan 2019 10:47:42 +0100 Subject: [PATCH] Can exchange ready messages --- client/SignalExchangerClient.js | 41 +++++++++++++++++++++ client/test-client.js | 4 +++ sockets.js | 51 ++++++++++++++++++++++++++- video-client/SignalExchangerClient.js | 41 +++++++++++++++++++++ 4 files changed, 136 insertions(+), 1 deletion(-) diff --git a/client/SignalExchangerClient.js b/client/SignalExchangerClient.js index 341fa62..62649e5 100644 --- a/client/SignalExchangerClient.js +++ b/client/SignalExchangerClient.js @@ -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) diff --git a/client/test-client.js b/client/test-client.js index ce37f64..fc302e5 100644 --- a/client/test-client.js +++ b/client/test-client.js @@ -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 * diff --git a/sockets.js b/sockets.js index 51b3d20..d24f6fd 100644 --- a/sockets.js +++ b/sockets.js @@ -50,7 +50,35 @@ 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 * @param {String} target_id The ID of the target client @@ -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) { diff --git a/video-client/SignalExchangerClient.js b/video-client/SignalExchangerClient.js index 341fa62..62649e5 100644 --- a/video-client/SignalExchangerClient.js +++ b/video-client/SignalExchangerClient.js @@ -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)