我有一个聊天室系统,用户可以创建一个聊天室,然后仅在语音聊天中交谈,但是,现在它打电话给我的朋友,但我听不到它们,也没有创建音频元素。 Here is my console output。任何人都可以快速浏览一下吗?
在服务器上,此操作正在进行中:
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);
let server = app.listen(port);
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);
在客户端上,我有此文件(.pug):
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.');
}
);
setTimeout(connect, 5000);
}
function playStream(stream) {
var audio = $('<audio autoplay />').appendTo('body');
audio[0].src = (URL || webkitURL || mozURL).createObjectURL(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'});
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);
});
我急忙写这封信,因为我很快就要离开了,对于任何错误,我们深表歉意。感谢您考虑我的问题。