mirror of
https://gitlab.com/comunic/nodejs-webrtcsignalexchangerserver
synced 2024-11-21 12:59:27 +00:00
Handles clients IDs
This commit is contained in:
parent
42a1ad4182
commit
0d599128e1
114
client/SignalExchangerClient.js
Normal file
114
client/SignalExchangerClient.js
Normal file
@ -0,0 +1,114 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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("error", () => {
|
||||
if(this.onError != null)
|
||||
setTimeout(this.onError, 0);
|
||||
});
|
||||
|
||||
this.socket.addEventListener("close", () => {
|
||||
if(this.onClosed != null)
|
||||
setTimeout(this.onClosed, 0);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 data to the server
|
||||
*
|
||||
* @param {Object} data The data to send to the server
|
||||
*/
|
||||
sendData(data){
|
||||
this.socket.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
3
client/server.py
Executable file
3
client/server.py
Executable file
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env python
|
||||
import SimpleHTTPServer
|
||||
SimpleHTTPServer.test();
|
16
client/test-client.html
Normal file
16
client/test-client.html
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Signal Exchanger test client</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Signal Exchanger test client</h1>
|
||||
|
||||
|
||||
<script src="SignalExchangerClient.js"></script>
|
||||
<script src="test-client.js"></script>
|
||||
</body>
|
||||
</html>
|
29
client/test-client.js
Normal file
29
client/test-client.js
Normal file
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Node Signal Exchanger test client
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
const Config = {
|
||||
port: 8081,
|
||||
server_name: "localhost",
|
||||
clientID: location.href.toString().includes("#1") ? "client-1" : "client-2"
|
||||
};
|
||||
|
||||
let client = new SignalExchangerClient(
|
||||
Config.server_name,
|
||||
Config.port,
|
||||
Config.clientID
|
||||
);
|
||||
|
||||
client.onConnected = function(){
|
||||
console.log("Connected to signal exchanger server.");
|
||||
}
|
||||
|
||||
client.onError = function() {
|
||||
console.error("An error occurred with the connection to the signal exchanger server");
|
||||
}
|
||||
|
||||
client.onClosed = function() {
|
||||
console.log("Connection to server closed.");
|
||||
}
|
24
index.js
24
index.js
@ -14,7 +14,8 @@ const WebSocket = require("ws");
|
||||
/**
|
||||
* Import project modules
|
||||
*/
|
||||
const config = require("./config");
|
||||
const Config = require("./config");
|
||||
const Sockets = require("./sockets");
|
||||
|
||||
/**
|
||||
* If user tries to connect to home page,
|
||||
@ -25,7 +26,22 @@ app.get("/", (req, res) => {
|
||||
res.send("Welcome to NodeSignalExchanger!");
|
||||
});
|
||||
|
||||
/**
|
||||
* Start express server
|
||||
*/
|
||||
var server = app.listen(Config.port, () => {
|
||||
console.log("Application listening on port " + Config.port);
|
||||
});
|
||||
|
||||
var server = app.listen(config.port, () => {
|
||||
console.log("Application listening on port " + config.port);
|
||||
})
|
||||
|
||||
/**
|
||||
* Handles socket connections
|
||||
*/
|
||||
var wss = WebSocket.Server({
|
||||
server: server,
|
||||
path: "/socket"
|
||||
});
|
||||
|
||||
wss.on("connection", socket => {
|
||||
Sockets.addSocket(socket);
|
||||
});
|
||||
|
127
sockets.js
Normal file
127
sockets.js
Normal file
@ -0,0 +1,127 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user