56 lines
2.2 KiB
JavaScript
56 lines
2.2 KiB
JavaScript
"use strict";
|
|
const config = {
|
|
iceServers: [{ urls: "stun: stun.l.google.com" }],
|
|
};
|
|
let localConnection = new RTCPeerConnection();
|
|
function handleSendChannelStatusChange() {
|
|
console.log(handleSendChannelStatusChange);
|
|
}
|
|
let sendChannel = localConnection.createDataChannel("sendChannel");
|
|
sendChannel.onopen = handleSendChannelStatusChange;
|
|
sendChannel.onclose = handleSendChannelStatusChange;
|
|
let remoteConnection = new RTCPeerConnection();
|
|
remoteConnection.ondatachannel = receiveChannelCallback;
|
|
localConnection.onicecandidate = (e) => !e.candidate ||
|
|
remoteConnection.addIceCandidate(e.candidate).catch(handleAddCandidateError);
|
|
remoteConnection.onicecandidate = (e) => !e.candidate ||
|
|
localConnection.addIceCandidate(e.candidate).catch(handleAddCandidateError);
|
|
function handleCreateDescriptionError(error) {
|
|
console.log(`Unable to create an offer: ${error.toString()}`);
|
|
}
|
|
function handleLocalAddCandidateSuccess() {
|
|
console.log('handleLocalAddCandidateSuccess');
|
|
}
|
|
function handleRemoteAddCandidateSuccess() {
|
|
console.log('handleRemoteAddCandidateSuccess');
|
|
}
|
|
function handleAddCandidateError() {
|
|
console.log("Oh noes! addICECandidate failed!");
|
|
}
|
|
localConnection
|
|
.createOffer()
|
|
.then((offer) => localConnection.setLocalDescription(offer))
|
|
.then(() => remoteConnection.setRemoteDescription(localConnection.localDescription))
|
|
.then(() => remoteConnection.createAnswer())
|
|
.then((answer) => remoteConnection.setLocalDescription(answer))
|
|
.then(() => localConnection.setRemoteDescription(remoteConnection.localDescription))
|
|
.catch(handleCreateDescriptionError);
|
|
function handleReceiveChannelStatusChange(event) {
|
|
let receiveChannel = event.channel;
|
|
if (receiveChannel) {
|
|
console.log(`Receive channel's status has changed to ${receiveChannel.readyState}`);
|
|
}
|
|
}
|
|
function receiveChannelCallback(event) {
|
|
let receiveChannel = event.channel;
|
|
receiveChannel.onmessage = handleReceiveMessage;
|
|
receiveChannel.onopen = handleReceiveChannelStatusChange;
|
|
receiveChannel.onclose = handleReceiveChannelStatusChange;
|
|
}
|
|
function sendMessage(message) {
|
|
sendChannel.send(message);
|
|
}
|
|
function handleReceiveMessage(event) {
|
|
console.log(event.data);
|
|
}
|
|
//# sourceMappingURL=webRTC.js.map
|