mirror of
https://gitlab.com/comunic/nodejs-webrtcsignalexchangerserver
synced 2024-11-22 05:19:26 +00:00
127 lines
2.7 KiB
JavaScript
127 lines
2.7 KiB
JavaScript
|
/**
|
||
|
* Connections sockets management
|
||
|
*
|
||
|
* @author Pierre HUBERT
|
||
|
*/
|
||
|
|
||
|
let SocketsList = [];
|
||
|
|
||
|
/**
|
||
|
* Send a message to the socket
|
||
|
*
|
||
|
* @param socket The target socket
|
||
|
* @param {Object} message The message to send to the socket
|
||
|
*/
|
||
|
function SendMessageToSocket(socket, message){
|
||
|
socket.send(JSON.stringify(message));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add a socket to the list
|
||
|
*
|
||
|
* @param {String} id The ID of the socket
|
||
|
* @param socket The socket to add
|
||
|
*/
|
||
|
function AddSocketToList(id, socket){
|
||
|
|
||
|
SocketsList.push({
|
||
|
id: id,
|
||
|
socket: socket
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove the socket from the list
|
||
|
*
|
||
|
* @param socket Socket to remove
|
||
|
*/
|
||
|
function RemoveSocketFromList(socket){
|
||
|
|
||
|
for(let i = 0; i < SocketsList.length; i++){
|
||
|
|
||
|
if(SocketsList[i].socket == socket){
|
||
|
SocketsList.splice(i, 1);
|
||
|
i--;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add a socket to the list of active sockets
|
||
|
*
|
||
|
* This method is called when the server handles a new connection
|
||
|
*
|
||
|
* @param socket
|
||
|
*/
|
||
|
exports.addSocket = function(socket){
|
||
|
|
||
|
console.log("New socket connection detected!");
|
||
|
|
||
|
/**
|
||
|
* This variable will contains the ID of the client
|
||
|
* as soon as the client will send it
|
||
|
*/
|
||
|
let client_id = false;
|
||
|
|
||
|
/**
|
||
|
* We listen to socket messages
|
||
|
*/
|
||
|
socket.on("message", message => {
|
||
|
|
||
|
//Decode message
|
||
|
let data;
|
||
|
try {
|
||
|
data = JSON.parse(message);
|
||
|
}
|
||
|
catch (err) {
|
||
|
console.error("Invalid message received from socket!");
|
||
|
SendMessageToSocket(socket, {
|
||
|
error: "Message could not be parsed!"
|
||
|
})
|
||
|
return;
|
||
|
};
|
||
|
|
||
|
if(!client_id){
|
||
|
|
||
|
//Check if the client ID was specified or not in the request
|
||
|
if(!data.client_id){
|
||
|
SendMessageToSocket(socket, {
|
||
|
error: "Please specify your client ID!"
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(typeof data.client_id != "string"){
|
||
|
SendMessageToSocket(socket, {
|
||
|
error: "Invalid client ID specified!"
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//Save client ID
|
||
|
client_id = data.client_id;
|
||
|
AddSocketToList(client_id, socket);
|
||
|
console.log("New connection from client ID " + client_id);
|
||
|
SendMessageToSocket(socket, {
|
||
|
success: "Client ID successfully set."
|
||
|
});
|
||
|
}
|
||
|
|
||
|
else {
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
});
|
||
|
|
||
|
|
||
|
socket.on("close", () => {
|
||
|
if(client_id) {
|
||
|
console.log("Connection from " + client_id + " closed.");
|
||
|
RemoveSocketFromList(socket);
|
||
|
}
|
||
|
});
|
||
|
}
|