Поэтому я установил сервер peerjs на свой хостинг digitalocean, используя следующую команду.
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