Handles clients IDs

This commit is contained in:
2019-01-19 10:53:09 +01:00
parent 42a1ad4182
commit 0d599128e1
6 changed files with 309 additions and 4 deletions

View 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
View File

@ -0,0 +1,3 @@
#!/usr/bin/env python
import SimpleHTTPServer
SimpleHTTPServer.test();

16
client/test-client.html Normal file
View 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
View 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.");
}