Я работаю в системе чата с логином и прочим, и когда кто-то присоединяется к чату со мной, я получаю эту ошибку, напечатанную моим обработчиком ошибок:
Не удалось выполнить 'setRemoteDescription' для 'RTCPeerConnection': предоставленное значение 'null' не является допустимым значением перечисления типа RTCSdpType.
мой код:
Клиент (мопс):
script(src='https://unpkg.com/peerjs@1.0.0/dist/peerjs.min.js')
script(src='https://code.jquery.com/jquery-3.4.1.js')
script(src='/socket.io/socket.io.js')
script.
navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
let socket = io('');
let peers = {
objects: []
}
let token = '#{token}';
let chat = '#{id}';
let userData = token.split('_._');
let peerID = userData[0]+'FFFGMMM'+chat;
let RTCconnected = false;
let socketConnected = false;
let me;
let str;
function init() {
//navigator.getUserMedia (
// {video: false, audio: true},
// function success(audioStream) {
// str = audioStream;
// },
// function error(err) {
// console.log('Cannot connect to microphone.');
// }
//);
navigator.mediaDevices.getUserMedia({video: false, audio: true})
.then(function(stream) {
str = stream;
})
.catch(function(err) {
console.log('user media error lmoa');
});
setTimeout(connect, 5000);
}
function playStream(stream) {
console.log('playing stream');
var audio = $('<audio autoplay />').appendTo('body');
audio[0].srcObject = stream;
}
function findPeerIndex(id) {
for (var i = peers.objects.length - 1; i >= 0; i--) {
if (peers.objects[i].id == id) {
return i;
}
}
}
function callPeer(peer) {
if (peer.id == peerID) {
return false;
}
console.log('Calling peer: '+peer);
let peerRef = findPeerIndex(peer.id);
let ong = me.call(peer.id, str);
ong.on('error', function(err) {
console.log('Call error: '+err);
});
ong.on('stream', function(stream) {
peers.objects[peerRef].stream = stream;
playStream(stream);
});
}
//function incomingCall(incoming) {
//}
function hb() {
socket.emit('getUsers', {id: chat});
}
function connect() {
console.log('Subscribing to server...');
socket.emit('subscribe', {id: peerID});
console.log('RTC connecting...');
me = new Peer(peerID, {
host: '/',
port: 3000,
path: '/peerjs',
config: {
iceServers: [
{
urls: "turn:188.25.47.146:3478",
username: "paperplane",
credential: "dev1"
},
{urls:'stun:stun.l.google.com:19302'}
]
}
});
me.on('open', function(id) {
console.log('Connected to RTC. ID: ' + id);
RTCconnected = true;
});
me.on('error', function(err) {
console.log('Error: '+err);
});
me.on('call', function(incoming) {
var peerIndex = findPeerIndex(incoming.peer);
incoming.answer(str);
console.log('Answered call litocimoticoanetemao');
incoming.on('stream', function(stream) {
peers.objects[peerIndex].stream = stream;
playStream(stream);
});
});
hb();
}
socket.on('users', function(d) {
peers.objects = [];
let conns = d.connections;
for (var i = conns.length - 1; i >= 0; i--) {
peers.objects.push({id: conns[i]});
callPeer({id: conns[i]});
}
});
socket.on('ok', function(m) {
console.log(m.message);
socketConnected = true;
});
socket.on('join', function(p) {
peers.objects.push({id: p.id});
callPeer(p);
});
button(onclick='init()')
Сервер (node.js):
// server.js
// Main server file for BoaiChat
const express = require('express');
const app = express();
const port = 3000;
const utils = require('./tools.js');
const mongoose = require('mongoose');
var bodyParser = require('body-parser');
var url = "mongodb://localhost:27017/paperplane";
const ExpressPeerServer = require('peer').ExpressPeerServer;
app.use(bodyParser.urlencoded({ extended: false }));
mongoose.connect(url, {useNewUrlParser: true});
var session = require('express-session');
app.use(session({ secret: utils.cookieSecret, cookie: { maxAge: utils.cookieAge }}));
app.set('view engine', 'pug');
// Define routers
const membership = require('./user.js');
const chat = require('./chat.js');
app.get('/', (req, res) => {
res.sendFile(utils.getFilePath('index.html'));
});
app.use('/membership', membership);
app.use('/chat', chat);
var server = app.listen(port, '0.0.0.0');
var io = require('socket.io').listen(server);
const options = {
debug: true
}
const peerserver = ExpressPeerServer(server, options);
let connections = [];
let sockets = [];
peerserver.on('connection', (client) => {
connections.push(client);
for (var i = connections.length - 1; i >= 0; i--) {
if (connections[i].split('FFFGMMM')[1] == client.split('FFFGMMM')[1]) {
for (var i = sockets.length - 1; i >= 0; i--) {
if (sockets[i].peer.split('FFFGMMM')[1] == client.split('FFFGMMM')[1]) {
sockets[i].socket.emit('join', {id: client});
console.log('nibber');
}
}
}
}
console.log('Client joined');
});
peerserver.on('disconnect', (client) => {
let i = connections.indexOf(client);
let l = connections[i]
connections.splice(i, 1);
console.log('Client left');
});
io.on('connection', function(socket){
console.log('a user connected to the socket');
socket.on('subscribe', function(data){
socket.peer = data.id;
sockets.push({sid: socket.id, peer: data.id, socket: socket});
socket.emit('ok', {message: 'Subscribed to socket server'});
});
socket.on('disconnect', function(){
console.log('user disconnected from socket');
for (var i = sockets.length - 1; i >= 0; i--) {
if (sockets[i].sid == socket.id) {
sockets.splice(i, 1);
}
}
});
socket.on('getUsers', function(data){
let ret = [];
for (var i = connections.length - 1; i >= 0; i--) {
let sp = connections[i].split('FFFGMMM');
if (sp.length == 2) {
if (sp[0] && sp[1]) {
if (sp[1] == data.id) {
ret.push(connections[i]);
}
}
}
}
socket.emit('users', {connections: ret});
});
});
app.use('/peerjs', peerserver);
Кто-нибудь знает, что это такое?
Если кому-то это нужно, вот консольный вывод