mirror of
https://gitlab.com/comunic/nodejs-webrtcsignalexchangerserver
synced 2025-06-19 17:35:19 +00:00
Connection established between the two peers !
This commit is contained in:
@ -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
11
client/simplepeer.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -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>
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user