mirror of
https://gitlab.com/comunic/nodejs-webrtcsignalexchangerserver
synced 2024-11-25 06:49:28 +00:00
114 lines
2.3 KiB
JavaScript
114 lines
2.3 KiB
JavaScript
/**
|
|
* 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));
|
|
}
|
|
} |