Decouple PeerExchange.
parent
ca640e39da
commit
b450e4255f
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { createUUID } from './random.js';
|
||||||
|
|
||||||
|
export function createPeerExchange(address) {
|
||||||
|
const uuid = createUUID();
|
||||||
|
|
||||||
|
const server = new WebSocket(address);
|
||||||
|
const listeners = new Set();
|
||||||
|
|
||||||
|
function onMessage(message) {
|
||||||
|
const data = JSON.parse(message.data);
|
||||||
|
|
||||||
|
if (data.uuid === uuid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
listeners.forEach(callback => callback(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
function listen(callback) {
|
||||||
|
listeners.add(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
function send(data) {
|
||||||
|
server.send(JSON.stringify(Object.assign({uuid}, data)));
|
||||||
|
}
|
||||||
|
|
||||||
|
server.addEventListener('message', onMessage);
|
||||||
|
|
||||||
|
return {
|
||||||
|
listen,
|
||||||
|
send,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
import { dataChannelConfig, peerConnectionConfig } from './config.js';
|
import { dataChannelConfig, peerConnectionConfig } from './config.js';
|
||||||
import { createUUID } from './random.js';
|
import { createPeerExchange } from './peer.js';
|
||||||
|
|
||||||
var localVideo;
|
var localVideo;
|
||||||
var localStream;
|
var localStream;
|
||||||
var remoteVideo;
|
var remoteVideo;
|
||||||
var peerConnection;
|
var peerConnection;
|
||||||
var uuid;
|
|
||||||
var serverConnection;
|
var serverConnection;
|
||||||
var dataChannel;
|
var dataChannel;
|
||||||
|
|
||||||
|
|
@ -13,13 +12,11 @@ function pageReady() {
|
||||||
document.querySelector("button#start")
|
document.querySelector("button#start")
|
||||||
.addEventListener("click", start);
|
.addEventListener("click", start);
|
||||||
|
|
||||||
uuid = createUUID();
|
|
||||||
|
|
||||||
localVideo = document.getElementById('localVideo');
|
localVideo = document.getElementById('localVideo');
|
||||||
remoteVideo = document.getElementById('remoteVideo');
|
remoteVideo = document.getElementById('remoteVideo');
|
||||||
|
|
||||||
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
serverConnection = createPeerExchange('wss://' + window.location.hostname + ':8443');
|
||||||
serverConnection.addEventListener('message', gotMessageFromServer);
|
serverConnection.listen(gotMessageFromServer);
|
||||||
|
|
||||||
var constraints = {
|
var constraints = {
|
||||||
video: true,
|
video: true,
|
||||||
|
|
@ -71,14 +68,9 @@ function start(isCaller) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function gotMessageFromServer(message) {
|
function gotMessageFromServer(signal) {
|
||||||
if(!peerConnection) start(false);
|
if(!peerConnection) start(false);
|
||||||
|
|
||||||
var signal = JSON.parse(message.data);
|
|
||||||
|
|
||||||
// Ignore messages from ourself
|
|
||||||
if(signal.uuid == uuid) return;
|
|
||||||
|
|
||||||
if(signal.sdp) {
|
if(signal.sdp) {
|
||||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||||
// Only create answers in response to offers
|
// Only create answers in response to offers
|
||||||
|
|
@ -93,7 +85,7 @@ function gotMessageFromServer(message) {
|
||||||
|
|
||||||
function gotIceCandidate(event) {
|
function gotIceCandidate(event) {
|
||||||
if(event.candidate != null) {
|
if(event.candidate != null) {
|
||||||
serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
|
serverConnection.send({'ice': event.candidate});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,7 +93,7 @@ function createdDescription(description) {
|
||||||
console.log('got description');
|
console.log('got description');
|
||||||
|
|
||||||
peerConnection.setLocalDescription(description).then(function() {
|
peerConnection.setLocalDescription(description).then(function() {
|
||||||
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
serverConnection.send({'sdp': peerConnection.localDescription});
|
||||||
}).catch(errorHandler);
|
}).catch(errorHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue