Javascript 无法使PubNub WebRTC教程正常工作
我正试图按照PubNub教程()构建我的第一个WebRTC应用程序;然而,它根本没有起作用。我对编程相当陌生,因此非常感谢您的帮助。下面是我的代码。请注意,我已经创建了一个帐户,并且有自己的“pub”和“sub”,我已经适当地插入了它们Javascript 无法使PubNub WebRTC教程正常工作,javascript,html,webrtc,pubnub,Javascript,Html,Webrtc,Pubnub,我正试图按照PubNub教程()构建我的第一个WebRTC应用程序;然而,它根本没有起作用。我对编程相当陌生,因此非常感谢您的帮助。下面是我的代码。请注意,我已经创建了一个帐户,并且有自己的“pub”和“sub”,我已经适当地插入了它们 <!DOCTYPE html> <html> <div id="vid-box"></div> <form name="loginForm" id="login" action="#" onsub
<!DOCTYPE html>
<html>
<div id="vid-box"></div>
<form name="loginForm" id="login" action="#" onsubmit="return login(this);">
<input type="text" name="username" id="username" placeholder="Pick a username!" />
<input type="submit" name="login_submit" value="Log In">
</form>
<form name="callForm" id="call" action="#" onsubmit="return makeCall(this);">
<input type="text" name="number" placeholder="Enter user to dial!" />
<input type="submit" value="Call"/>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="js/webrtc.js"></script>
<script type="text/javascript">
var video_out = document.getElementById("vid-box");
function login(form) {
var phone = window.phone = PHONE({
number : form.username.value || "Anonymous", // listen on username line else Anonymous
publish_key : 'MY OWN PUB KEY',
subscribe_key : 'MY OWN SUB KEY',
});
phone.ready(function(){ form.username.style.background="#55ff5b"; });
phone.receive(function(session){
session.connected(function(session) { video_out.appendChild(session.video); });
session.ended(function(session) { video_out.innerHTML=''; });
});
return false; // So the form does not submit.
}
function makeCall(form){
if (!window.phone) alert("Login First!");
else phone.dial(form.number.value);
return false;
}
</script>
</html>
var video_out=document.getElementById(“视频框”);
函数登录(表单){
var phone=window.phone=phone({
编号:form.username.value | | |“Anonymous”//在用户名行上侦听其他匿名
发布密钥:“我自己的发布密钥”,
订阅密钥:“我自己的子密钥”,
});
ready(function(){form.username.style.background=“#55ff5b”;});
电话接收(功能(会话){
session.connected(函数(session){video_out.appendChild(session.video);});
session.end(函数(session){video_out.innerHTML='';});
});
return false;//因此表单不会提交。
}
函数makeCall(表单){
如果(!window.phone)警报(“先登录!”);
其他电话。拨号(形式。号码。值);
返回false;
}
WebRTC在本地主机HTTPS上运行
您希望使用localhost在笔记本电脑上本地运行WebRTC演示。您必须使用HTTPS。这是一个使用本地安全Web服务器(包括!)的演示的GIF视频我们清理了您的视频DOM/jQuery代码,还更正了一些错误。您可以找到的HTML源代码
在本地运行WebRTC演示
这些终端命令将把html文件下载到本地框中,为TLS加密创建PEM密钥,并使用Python运行本地HTTPS服务器
curl https://gist.githubusercontent.com/stephenlb/edd4b0c218a72a34349baa004a80fd7a/raw/1b28c5e39db0d5eaabc10006cede0a8825b9afd4/webrtc-demo.html > webrtc-demo.html
python <(curl -L https://gist.githubusercontent.com/stephenlb/2e19d98039469b9d0134/raw/5afefc79647e0786097ca3406dbf93c5de919aed/https.py)
运行上面的命令来测试演示
WebRTC的参考链接
curl https://gist.githubusercontent.com/stephenlb/edd4b0c218a72a34349baa004a80fd7a/raw/1b28c5e39db0d5eaabc10006cede0a8825b9afd4/webrtc-demo.html > webrtc-demo.html
python <(curl -L https://gist.githubusercontent.com/stephenlb/2e19d98039469b9d0134/raw/5afefc79647e0786097ca3406dbf93c5de919aed/https.py)
运行上面的命令来测试演示
WebRTC的参考链接