我有一个运行 php 的 apache 服务器和我的前端 peerjs 代码:
<script>
$(function(){
//returns element value of GET key (e.g '&test=2'returns 2)
function getParameterByName(name){
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function getAudio(successCallback, errorCallback){
navigator.getUserMedia({
audio: true,
video: false
}, successCallback, errorCallback);
}
//request call
var from = getParameterByName('from');
var to = getParameterByName('to');
$('#start-call').click(function(){
console.log('starting call...');
getAudio(
function(MediaStream){
console.log('now calling ' + to);
var call = peer.call(to, MediaStream);
call.on('stream', onReceiveStream);
},
function(err){
console.log('an error occured while getting the audio');
console.log(err);
}
);
});
//recieve call
function onReceiveCall(call){
console.log('peer is calling...');
console.log(call);
getAudio(
function(MediaStream){
call.answer(MediaStream);
console.log('answering call started...');
},
function(err){
console.log('an error occured while getting the audio');
console.log(err);
}
);
call.on('stream', onReceiveStream);
}
function onReceiveStream(stream){
var audio = document.querySelector('audio');
audio.src = window.URL.createObjectURL(stream);
audio.onloadedmetadata = function(e){
console.log('now playing the audio');
audio.play();
}
}
var peer = new Peer({host: 'www.example.com', port:9000, path:''});
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
});
peer.on('call', onReceiveCall);
});
</script>
<div id = "start-call"> Start Call </div>
<audio controls></audio>
然后我有一个节点服务器,它在端口 9000 上运行对等服务器:
var fs = require('fs');
var PeerServer = require('peer').PeerServer;
var server = PeerServer({
port: 9000,
ssl: {
key: fs.readFileSync('/xxx/xxx/privkey.pem'),
cert: fs.readFileSync('/xxx/xxx/cert.pem')
},
proxied: true
});
server.on('connection', function(id) {
console.log("connecion made:" + id);
});
使用 chrome,前端控制台中没有出现任何错误,我确实得到了提示"Connection made: 35hy2o4ydfqw"
。因此,当我单击开始呼叫时,控制台会输出,"now calling xxx..."
但是另一侧根本没有收到任何内容,所以我认为它是
peer.on('call', onReceiveCall);
未接听电话。关于接收器端出了什么问题的提示很少,绝对没有任何反应。
感谢您提供任何帮助或建议。