Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. let local_peer = new RTCPeerConnection()
  2. let channel = local_peer.createDataChannel("channel");
  3.  
  4. channel.onopen = handleSendChannelStatusChange;
  5. channel.onclose = handleSendChannelStatusChange;
  6.  
  7. let remote_peer = new RTCPeerConnection();
  8.  
  9. remote_peer.ondatachannel = function(event) {
  10. let receive_channel = event.channel;
  11. receive_channel.onmessage = event => console.log(event);
  12. receive_channel.onopen = function() { console.log("Receive channel has been opened!") };
  13. receive_channel.onclose = function() { console.log("Receive channel has been closed!") };
  14. };
  15.  
  16. local_peer.onicecandidate = function(event) {
  17. remote_peer.addIceCandidate(event.candidate);
  18. }
  19.  
  20. remote_peer.onicecandidate = e => !e.candidate || local_peer.addIceCandidate(e.candidate);
  21.  
  22. local_peer.createOffer()
  23. .then(offer => local_peer.setLocalDescription(offer))
  24. .then(() => remote_peer.setRemoteDescription(local_peer.localDescription))
  25. .then(() => remote_peer.createAnswer())
  26. .then(answer => remote_peer.setLocalDescription(answer))
  27. .then(() => local_peer.setRemoteDescription(remote_peer.localDescription));
  28.  
  29. function handleSendChannelStatusChange(event) {
  30. if (channel.readyState === "open") {
  31. console.log("Open!")
  32. } else {
  33. console.log("Not open!")
  34. }
  35.  
  36. channel.send("hello!")
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement