WIP
This commit is contained in:
83
src/webRTC.ts
Normal file
83
src/webRTC.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
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:any) =>
|
||||
!e.candidate ||
|
||||
remoteConnection.addIceCandidate(e.candidate).catch(handleAddCandidateError);
|
||||
|
||||
remoteConnection.onicecandidate = (e) =>
|
||||
!e.candidate ||
|
||||
localConnection.addIceCandidate(e.candidate).catch(handleAddCandidateError);
|
||||
|
||||
|
||||
|
||||
function handleCreateDescriptionError(error:any) {
|
||||
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 as RTCSessionDescriptionInit),
|
||||
)
|
||||
.then(() => remoteConnection.createAnswer())
|
||||
.then((answer) => remoteConnection.setLocalDescription(answer))
|
||||
.then(() =>
|
||||
localConnection.setRemoteDescription(remoteConnection.localDescription as RTCSessionDescriptionInit),
|
||||
)
|
||||
.catch(handleCreateDescriptionError);
|
||||
|
||||
function handleReceiveChannelStatusChange(event:any) {
|
||||
let receiveChannel = event.channel;
|
||||
|
||||
if (receiveChannel) {
|
||||
console.log(
|
||||
`Receive channel's status has changed to ${receiveChannel.readyState}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function receiveChannelCallback(event:any) {
|
||||
let receiveChannel = event.channel;
|
||||
receiveChannel.onmessage = handleReceiveMessage;
|
||||
receiveChannel.onopen = handleReceiveChannelStatusChange;
|
||||
receiveChannel.onclose = handleReceiveChannelStatusChange;
|
||||
}
|
||||
|
||||
function sendMessage(message:string) {
|
||||
sendChannel.send(message);
|
||||
}
|
||||
|
||||
function handleReceiveMessage(event:any) {
|
||||
console.log(event.data);
|
||||
}
|
||||
Reference in New Issue
Block a user