index.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title> URTC DEMO PureJS </title>
  5. <style type="text/css">
  6. video {
  7. margin: 6px;
  8. width: 300px;
  9. height: 200px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div>房间号:<span id="roomId"></span></div>
  15. <div>当前用户ID:<span id="userId"></span></div>
  16. <br/>
  17. <div>本地音视频</div>
  18. <div>
  19. <video id="pusher" autoplay playsinline></video>
  20. </div>
  21. <br/>
  22. <div>远端音视频</div>
  23. <div id="pullers"></div>
  24. <br/>
  25. <a href="https://github.com/ucloud/urtc-sdk-web" target="_blank">API 文档请看这里</a>
  26. <script type="text/javascript" src="https://ucloud.github.io/urtc-sdk-web/lib/index.js"></script>
  27. <script>
  28. window.onload = function() {
  29. const pusherElem = document.querySelector('#pusher');
  30. const pullersElem = document.querySelector('#pullers');
  31. const roomElem = document.querySelector('#roomId');
  32. const userElem = document.querySelector('#userId');
  33. const appId = ''; // "urtc-xxxxxx";
  34. const appKey = ''; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  35. const roomId = "xxxx"; // "xxxx"
  36. const userId = Math.floor(Math.random() * 1000000).toString();
  37. if (!appId || !appKey) {
  38. alert('请先设置 AppId 和 AppKey');
  39. return;
  40. }
  41. if (!roomId) {
  42. alert('请先设置roomId');
  43. return;
  44. }
  45. roomElem.innerHTML = roomId;
  46. userElem.innerHTML = userId;
  47. console.log('UCloudRTC Version: ', UCloudRTC.version);
  48. const token = UCloudRTC.generateToken(appId, appKey, roomId, userId);
  49. const client = new UCloudRTC.Client(appId, token, {type: 'rtc', role: 'push-and-pull'});
  50. // 监听 publish 成功的事件
  51. client.on('stream-published', function(stream) {
  52. console.log('成功发布流 ', stream);
  53. pusherElem.srcObject = stream.mediaStream;
  54. });
  55. // 监听 subscribe 成功的事件
  56. client.on('stream-subscribed', function(stream) {
  57. console.log('成功订阅流 ', stream);
  58. const pullerElem = document.createElement('video')
  59. pullerElem.id = stream.sid;
  60. pullerElem.autoplay = true;
  61. pullerElem.playsinline = true;
  62. pullerElem.srcObject = stream.mediaStream;
  63. pullersElem.append(pullerElem);
  64. });
  65. // 监听有远端流加入的事件
  66. client.on('stream-added', function(stream) {
  67. // 当有新的远端流加入后,就订阅该流
  68. client.subscribe(stream.sid, function(s) {
  69. console.log(`已订阅用户${stream.uid}的流${stream.sid} `, s);
  70. }, function(e) {
  71. console.log(`订阅用户${stream.uid}的流${stream.sid}失败 `, e);
  72. });
  73. });
  74. client.on('stream-removed', function(stream) {
  75. const pullerElem = document.querySelector('#' + stream.sid);
  76. pullerElem.srcObject = null;
  77. pullersElem.removeChild(pullerElem);
  78. });
  79. // 加入房间
  80. client.joinRoom(roomId, userId, function() {
  81. console.log('加入房间成功');
  82. // 加入房间成功后,就发布本地流
  83. client.publish(function(e) {
  84. console.log(`发布流失败 `, e);
  85. });
  86. }, function(e) {
  87. console.log('加入房间失败 ', e);
  88. });
  89. window.onunload = function () {
  90. // 离开页面前,先离开房间
  91. client.leaveRoom();
  92. }
  93. }
  94. </script>
  95. </body>
  96. </html>