Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let local_peer = new RTCPeerConnection()
- let channel = local_peer.createDataChannel("channel");
- channel.onopen = handleSendChannelStatusChange;
- channel.onclose = handleSendChannelStatusChange;
- let remote_peer = new RTCPeerConnection();
- remote_peer.ondatachannel = function(event) {
- let receive_channel = event.channel;
- receive_channel.onmessage = event => console.log(event);
- receive_channel.onopen = function() { console.log("Receive channel has been opened!") };
- receive_channel.onclose = function() { console.log("Receive channel has been closed!") };
- };
- local_peer.onicecandidate = function(event) {
- remote_peer.addIceCandidate(event.candidate);
- }
- remote_peer.onicecandidate = e => !e.candidate || local_peer.addIceCandidate(e.candidate);
- local_peer.createOffer()
- .then(offer => local_peer.setLocalDescription(offer))
- .then(() => remote_peer.setRemoteDescription(local_peer.localDescription))
- .then(() => remote_peer.createAnswer())
- .then(answer => remote_peer.setLocalDescription(answer))
- .then(() => local_peer.setRemoteDescription(remote_peer.localDescription));
- function handleSendChannelStatusChange(event) {
- if (channel.readyState === "open") {
- console.log("Open!")
- } else {
- console.log("Not open!")
- }
- channel.send("hello!")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement