【发布时间】:2017-10-30 17:09:00
【问题描述】:
所以我使用以下命令在我的 digitalocean 主机上安装了 peerjs 服务器。
npm install peer
peerjs --port 9000 --key peerjs
运行上述命令后,腻子给出以下打印:
在 :: 上启动 PeerServer,端口:9000,路径:/ (v. 0.2.8)
以下是我的 index.html 代码:
<!DOCTYPE html>
<html>
<head>
<title>Peer JS Testing</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.13/peer.js"></script>
<script>
$(document).ready(function(){
// var peer = new Peer({key: 'r6xc2gopu33j714i'});
// var peer = new Peer({ host: '139.59.3.130', port: 9000, debug: 3});
var peer = new Peer('', {host: 'thegraphicplanet.com', port: 9000});
var anotherid;
var mypeerid;
mypeerid = peer.id;
setTimeout(function(){
console.log(peer);
$('#showid').text('Your ID is: '+peer.id);
}, 3000);
$('.connect').click(function(){
anotherid = document.getElementById('anotherid').value;
// var conn = peer.connect(anotherid);
// conn.on('open', function(){
// conn.send('hi!');
// });
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
navigator.getUserMedia({video: true, audio: true}, function(stream) {
var call = peer.call(anotherid, stream);
call.on('stream', function(remoteStream) {
$('.showvideo').prop('src', URL.createObjectURL(stream));
});
}, function(err) {
console.log('Failed to get local stream' ,err);
});
});
/*
peer.on('connection', function(conn) {
conn.on('data', function(data){
// Will print 'hi!'
console.log(data);
});
});
*/
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
peer.on('call', function(call) {
navigator.getUserMedia({video: true, audio: true}, function(stream) {
call.answer(stream); // Answer the call with an A/V stream.
call.on('stream', function(remoteStream) {
$('.showvideo').src = URL.createObjectURL(remotestream);
$('.showvideo').play();
});
}, function(err) {
console.log('Failed to get local stream' ,err);
});
});
});
</script>
</head>
<body>
<h1 id="showid"></h1>
<label>Enter ID and Connect</label>
<input type="text" id="anotherid" />
<button class="connect">Connect</button>
<video class="showvideo" autoplay></video>
</body>
</html>
当我通过我的 URL 访问它时:https://thegraphicplanet.com/aumkarsandbox/peerjs/
控制台报如下错误:
:9000/peerjs/id?ts=15093825487790.7152652631730707 加载资源失败:net::ERR_CONNECTION_TIMED_OUT
【问题讨论】:
-
你应该添加 node.js 而不是 JavaScript 作为标签 ;-)
-
我认为这意味着您的服务器没有运行。默认情况下,您的 digitalocean 盒子上可能未打开端口 9000
标签: javascript webrtc peerjs