Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <script>
  6.  
  7. var configuration = { "iceServers": [{ "url": "stun:23.21.150.121" }] };
  8.  
  9. var pc;
  10. var str;
  11.  
  12. var ws = new WebSocket("ws://localhost:8182/");
  13. ws.binaryType = "arraybuffer";
  14.  
  15. ws.onmessage = e => {
  16.  
  17. if(e.data.constructor.name == "String") {
  18.  
  19. try {
  20.  
  21. var message = JSON.parse(e.data);
  22.  
  23. switch (message.type) {
  24. case "init":
  25.  
  26. var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
  27. var RTCI = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
  28. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  29. pc = new RTCPeerConnection(configuration);
  30.  
  31. pc.onicecandidate = function (evt) {
  32. console.log("onicecandidate", evt);
  33. if(evt.candidate) {
  34. ws.send(JSON.stringify({ type: "icecandidate", candidate: evt.candidate }));
  35. }
  36. };
  37. pc.onaddstream = function (evt) {
  38. console.log("Adding stream…", evt.stream);
  39. var element = document.getElementById("stream");
  40. element.src = webkitURL.createObjectURL(evt.stream);
  41. };
  42. pc.onopen = function (evt) {
  43. console.log("onopen");
  44. };
  45. pc.onremovestream = function (evt) {
  46. console.log("onremovestream");
  47. };
  48.  
  49. console.log("um");
  50. navigator.getUserMedia({ "audio": true }, function (stream) {
  51. console.log("stream", stream);
  52. str = stream;
  53. pc.addStream(stream);
  54. pc.createOffer(function (desc) {
  55. console.log("desc", desc);
  56. pc.setLocalDescription(desc);
  57. ws.send(JSON.stringify({ type: "sdp", desc: desc }));
  58. }, function (error) { console.log(error); });
  59. }, function (error) { console.log(error); });
  60.  
  61. break;
  62.  
  63. case "icecandidate":
  64.  
  65. pc.addIceCandidate(new RTCIceCandidate(message.candidate));
  66.  
  67. break;
  68.  
  69. case "answer":
  70.  
  71. pc.setRemoteDescription(new RTCSessionDescription(message.desc));
  72.  
  73. break;
  74. }
  75.  
  76. } catch (e) {
  77. console.log("not JSON", e);
  78. }
  79.  
  80. }
  81.  
  82. };
  83.  
  84. ws.onopen = () => ws.send(JSON.stringify({ type: "login", username: "rschuh", password: "asdnajkshdas" }));
  85.  
  86. </script>
  87. </head>
  88. <body>
  89. <audio id="stream" autoplay="autoplay" />
  90. </body>
  91. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement