This commit is contained in:
bobbydigitales
2024-09-13 22:57:30 -07:00
parent 9140739879
commit f0d073bab1
30 changed files with 2091 additions and 765 deletions

56
webRTC.js Normal file
View File

@@ -0,0 +1,56 @@
"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