mirror of
https://gitlab.com/comunic/nodejs-webrtcsignalexchangerserver
synced 2024-11-21 21:09:27 +00:00
Can exchange messages between clients
This commit is contained in:
parent
0d599128e1
commit
1f0a615e11
@ -79,6 +79,21 @@ class SignalExchangerClient {
|
|||||||
setTimeout(this.onConnected, 10);
|
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", () => {
|
this.socket.addEventListener("error", () => {
|
||||||
if(this.onError != null)
|
if(this.onError != null)
|
||||||
setTimeout(this.onError, 0);
|
setTimeout(this.onError, 0);
|
||||||
@ -103,12 +118,63 @@ class SignalExchangerClient {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send data to the server
|
* Send data to the server
|
||||||
*
|
*
|
||||||
* @param {Object} data The data to send to the server
|
* @param {Object} data The data to send to the server
|
||||||
*/
|
*/
|
||||||
sendData(data){
|
sendData(data){
|
||||||
|
console.log("Sending data to server", data);
|
||||||
this.socket.send(JSON.stringify(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.pending_signal = undefined;
|
||||||
|
this.pending_signal_target = undefined;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -7,7 +7,8 @@
|
|||||||
const Config = {
|
const Config = {
|
||||||
port: 8081,
|
port: 8081,
|
||||||
server_name: "localhost",
|
server_name: "localhost",
|
||||||
clientID: location.href.toString().includes("#1") ? "client-1" : "client-2"
|
clientID: location.href.toString().includes("#1") ? "client-1" : "client-2",
|
||||||
|
otherPeerID: location.href.toString().includes("#1") ? "client-2" : "client-1"
|
||||||
};
|
};
|
||||||
|
|
||||||
let client = new SignalExchangerClient(
|
let client = new SignalExchangerClient(
|
||||||
@ -18,6 +19,11 @@ let client = new SignalExchangerClient(
|
|||||||
|
|
||||||
client.onConnected = function(){
|
client.onConnected = function(){
|
||||||
console.log("Connected to signal exchanger server.");
|
console.log("Connected to signal exchanger server.");
|
||||||
|
|
||||||
|
client.sendSignal(
|
||||||
|
Config.otherPeerID,
|
||||||
|
"A signal content from " + Config.clientID + " to " + Config.otherPeerID,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.onError = function() {
|
client.onError = function() {
|
||||||
|
62
sockets.js
62
sockets.js
@ -49,6 +49,34 @@ function RemoveSocketFromList(socket){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
* @return {Number} The number of sockets to which the message was sent
|
||||||
|
*/
|
||||||
|
function SendSignalToClient(content, source_id, target_id){
|
||||||
|
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
SocketsList.forEach(info => {
|
||||||
|
|
||||||
|
if(info.id == target_id){
|
||||||
|
SendMessageToSocket(info.socket, {
|
||||||
|
signal: content,
|
||||||
|
source_id: source_id
|
||||||
|
});
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
return count;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a socket to the list of active sockets
|
* Add a socket to the list of active sockets
|
||||||
*
|
*
|
||||||
@ -71,6 +99,14 @@ exports.addSocket = function(socket){
|
|||||||
*/
|
*/
|
||||||
socket.on("message", message => {
|
socket.on("message", message => {
|
||||||
|
|
||||||
|
//Make sure the message is not too heavy
|
||||||
|
if(message.length > 10000){
|
||||||
|
SendMessageToSocket(socket, {
|
||||||
|
error: "Your request is too heavy for this server!"
|
||||||
|
})
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Decode message
|
//Decode message
|
||||||
let data;
|
let data;
|
||||||
try {
|
try {
|
||||||
@ -110,10 +146,30 @@ exports.addSocket = function(socket){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Check if the client wants to send a signal to another client
|
||||||
|
else if(data.signal && data.target_id) {
|
||||||
|
|
||||||
|
if(typeof data.signal !== "string" || typeof data.target_id !== "string"){
|
||||||
|
SendMessageToSocket(socket, {
|
||||||
|
error: "Unsecure request!"
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send the signal to the target client
|
||||||
|
let number_target = SendSignalToClient(data.signal, client_id, data.target_id);
|
||||||
|
|
||||||
|
SendMessageToSocket(socket, {
|
||||||
|
signal_sent: true,
|
||||||
|
number_of_targets: number_target
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//Message not understood
|
||||||
else {
|
else {
|
||||||
|
SendMessageToSocket(socket, {
|
||||||
|
error: "Could not understand your request!"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user