我使用了这个: https : //www.webrtc-experiment.com/broadcast/#7001888010026977进行一对多的视频/音频聊天。 所以我的问题是我的应用程序没有安全的Https连接(意味着它在不安全的https连接上运行),并且在localhost
运行,PC可以通过LAN网络互相通信,当用户和客户端运行时,应用程序可以正常工作在运行node.js服务器的同一台PC上,但是当从另一台PC连接时,应用程序失败
1.不交换create offer()
和create answer()
信息。 2.但是显示从服务器PC创建的房间。 3.当我加入时,远程流没有显示,并且没有信息交换。
我使用了从源代码获得的证书(上面的链接),所以我认为创建某种不安全的问题我真正的问题是我是否可以为不存在一对多Webrtc聊天的情况创建没有那些证书的node.js服务器。 这是server.js的代码
var fs = require('fs');
// don't forget to use your own keys!
var path='D:\webRTC experiments\conf';
var options = {
key: fs.readFileSync(require('path').resolve(__dirname, 'privatekey.pem'), 'UTF-8'),
cert:fs.readFileSync(require('path').resolve(__dirname, 'certificate.pem'), 'UTF-8'),
rejectUnauthorized:true,
requestCert:false,
agent:true
};
// HTTPs server
var app = require('https').createServer(options, function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
var link = 'https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket-over-nodejs';
response.write('<title>websocket-over-nodejs</title><h1><a href="'+ link + '">websocket-over-nodejs</a></h1><pre>var websocket = new WebSocket("wss://webrtcweb.com:9449/");</pre>');
response.end();
});
// shared stuff
var CHANNELS = { };
function onRequest(socket) {
var origin = socket.origin + socket.resource;
var websocket = socket.accept(null, origin);
websocket.on('message', function(message) {
if(!message || !websocket) return;
if (message.type === 'utf8') {
try {
onMessage(JSON.parse(message.utf8Data), websocket);
}
catch(e) {}
}
});
websocket.on('close', function() {
try {
truncateChannels(websocket);
}
catch(e) {}
});
}
function onMessage(message, websocket) {
if(!message || !websocket) return;
try {
if (message.checkPresence) {
checkPresence(message, websocket);
}
else if (message.open) {
onOpen(message, websocket);
}
else {
sendMessage(message, websocket);
}
}
catch(e) {}
}
function onOpen(message, websocket) {
if(!message || !message.channel || !websocket) return;
try {
var channel = CHANNELS[message.channel];
if (channel) {
CHANNELS[message.channel][channel.length] = websocket;
}
else {
CHANNELS[message.channel] = [websocket];
}
}
catch(e) {}
}
function sendMessage(message, websocket) {
if(!message || !message.data || !websocket) return;
var channel = [];
try {
message.data = JSON.stringify(message.data);
channel = CHANNELS[message.channel];
}
catch(e) {}
if (!channel || !message || !message.data) {
return;
}
for (var i = 0; i < channel.length; i++) {
if (channel[i] && channel[i] != websocket) {
try {
channel[i].sendUTF(message.data);
} catch(e) {}
}
}
}
function checkPresence(message, websocket) {
if(!message || !message.channel || !websocket) return;
try {
websocket.sendUTF(JSON.stringify({
isChannelPresent: !!CHANNELS[message.channel]
}));
}
catch(e) {}
}
function swapArray(arr) {
var swapped = [],
length = arr.length || 0;
for (var i = 0; i < length; i++) {
if (arr[i]) {
try {
swapped[swapped.length] = arr[i];
}
catch(e) {}
}
}
return swapped;
}
function truncateChannels(websocket) {
if(!websocket) return;
for (var channel in CHANNELS) {
var _channel = CHANNELS[channel];
if(_channel && _channel.length) {
for (var i = 0; i < _channel.length; i++) {
try {
if (_channel[i] == websocket) {
delete _channel[i];
}
}
catch(e) {}
}
try {
CHANNELS[channel] = swapArray(_channel);
if (CHANNELS && CHANNELS[channel] && !CHANNELS[channel].length) {
delete CHANNELS[channel];
}
}
catch(e) {}
}
}
}
var WebSocketServer = require('websocket').server;
new WebSocketServer({
httpServer: app,
autoAcceptConnections: false
}).on('request', onRequest);
app.listen(process.env.PORT || 9449);
process.on('unhandledRejection', (reason, promise) => {
process.exit(1);
});
console.log('Please open SSL URL: https://192.168.100.00:'+(process.env.PORT || 9449)+'/');
我如何才能通过这段代码
var options = {
key: fs.readFileSync(require('path').resolve(__dirname, 'privatekey.pem'), 'UTF-8'),
cert:fs.readFileSync(require('path').resolve(__dirname, 'certificate.pem'), 'UTF-8'),
rejectUnauthorized:true,
requestCert:false,
agent:true
};