Connection established between the two peers !

This commit is contained in:
Pierre HUBERT 2019-01-19 17:33:36 +01:00
parent bc723c5f3c
commit 62e247488a
5 changed files with 94 additions and 7 deletions

View File

@ -3,10 +3,22 @@
A lightweight node application that allows to exchange signals connection through websockets.
## Requirements
# Requirements
```sh
sudo apt install nodejs node-express node-wc
```
## Author
# Using for signaling
## Installation
* Install docker
* Install coturn: `sudo apt install coturn`
* Add a client to coturn: `sudo turnadmin -a -u anonymous -r anonymous.com -p anonymous`
## Running TURN server
```sh
sudo turnserver -o -a -f -r anonymous.com # Use -L option to specify an IP address
```
# Author
Pierre HUBERT 2019

View File

@ -55,6 +55,13 @@ class SignalExchangerClient {
*/
//onClosed = null;
/**
* Function called when we get a new signal information
*
* @type {Function}
*/
//onSignal = null;
/**
* Construct a client instance
*
@ -183,7 +190,13 @@ class SignalExchangerClient {
this.cancelCurrentSignal();
}
}
}
// Check if the message is a signal
else if(message.signal){
if(this.onSignal != null)
this.onSignal(message.signal, message.source_id);
}
}

11
client/simplepeer.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,7 @@
<h1>Signal Exchanger test client</h1>
<script src="simplepeer.min.js"></script>
<script src="SignalExchangerClient.js"></script>
<script src="test-client.js"></script>
</body>

View File

@ -17,13 +17,12 @@ let client = new SignalExchangerClient(
Config.clientID
);
var p;
client.onConnected = function(){
console.log("Connected to signal exchanger server.");
client.sendSignal(
Config.otherPeerID,
"A signal content from " + Config.clientID + " to " + Config.otherPeerID,
);
InitializePeer();
}
client.onError = function() {
@ -32,4 +31,55 @@ client.onError = function() {
client.onClosed = function() {
console.log("Connection to server closed.");
}
client.onSignal = function(signal, peerID){
if(peerID !== Config.otherPeerID)
return alert("A signal from an unknow peer ID has just been received!");
p.signal(JSON.parse(signal));
}
/**
* Initialize peer connection
*
* Warning : It is better to wait for the socket connection to be ready before
* launching this function if this peer is the initiator of the connection
*/
function InitializePeer() {
p = new SimplePeer({
initiator: location.hash === '#1',
trickle: false,
config: {
'iceServers': [
{ urls: 'stun:127.0.0.1:3478' },
{"url":"turn:127.0.0.1:3478",
"credential":"anonymous",
"username": "anonymous"}
]
}
});
p.on('error', function (err) { console.log('SimplePeer error', err) });
//Automatically send new signals to the other peer
p.on('signal', function (data) {
console.log('SIGNAL', JSON.stringify(data))
client.sendSignal(Config.otherPeerID, JSON.stringify(data));
});
p.on('connect', function () {
console.log('CONNECT');
msg = 'whatever' + Math.random();
console.log("Sending: " + msg);
p.send(msg);
})
p.on('data', function (data) {
console.log('data: ' + data)
})
}