javascript - JavaScript 中的 WebRTC

标签 javascript firebase webrtc

我正在尝试使 WebRTC 成为 JavaScript 中的对象以方便使用。 由于 javascript 在我这边执行,因此我收到一条错误消息,指出 Firebase 未定义。 所以我使用以下代码调用脚本源;

///in this section I am trying to get the firebase.js 
var head=document.getElementsByTagName('head')[0];
var script=document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', "https://cdn.firebase.com/v0/firebase.js");
head.appendChild(script);

之后,我尝试获取对 FireBase 数据库的引用并创建自己的

var dbRef = new Firebase("https://webrtcdemo.firebaseIO.com/");///this is the line i get the error
var roomRef = dbRef.child("rooms");

总结一下我的问题是否可以在 javascript 中使用 WebRTC(引用 Firebase)?如果是这样怎么可能?

谢谢

最佳答案

由于您是从 JavaScript 加载代码,因此您需要等待它加载后再继续:

// define onload handler
function onFirebaseLoad() {
  var dbRef = new Firebase("https://webrtcdemo.firebaseIO.com/");
  var roomRef = dbRef.child("rooms");
  // ...
}

// load the code
var head=document.getElementsByTagName('head')[0];
var script=document.createElement('script');
script.onload = onFirebaseLoad;
script.onreadystatechange = function() {
  if(script.readyState == 'complete') onFirebaseLoad();
};
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', "https://cdn.firebase.com/v0/firebase.js");
head.appendChild(script);

关于javascript - JavaScript 中的 WebRTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25294701/

相关文章:

javascript - 使用 onMouseOver 插入时如何提供 .gif 高度和宽度

android - 如何摆脱 Android Studio 警告 "Result of getException() not thrown"?

android - Facebook 登录不再有效,因为我将 Firebase 应用程序升级到新的控制台(仅)

javascript - RTCMultiConnection 返回当前流

javascript - Mongoose - 无法访问对象属性?

javascript - 获取特定 Array 属性的最小值的更好方法

javascript - 在类中声明方法与在 JavaScript 中使用类原型(prototype)之间的区别

javascript - Firestore 网络无法到达 Firestore 后端

webrtc - OpenTok/TokBox : is there a way to limit the number of published streams allowed in a session?

websocket - 使用 Web 套接字将媒体流式传输到服务器