简体   繁体   English

node.js 上的视频聊天应用程序与 webRTC(socket.io)在本地主机上工作正常,但在网络上出现错误

[英]video chat application on node.js with webRTC( socket.io) works fine on localhost but gives error on network

I am making a video chat application on node.js with WebRTC(socket.io).我正在使用 WebRTC(socket.io)在 node.js 上制作视频聊天应用程序。 This application runs fine on localhost but gives error while running on my network with my IP address.此应用程序在 localhost 上运行良好,但在使用我的 IP 地址在我的网络上运行时出错。 Also, I tried it on Heroku but error remained same另外,我在 Heroku 上尝试过,但错误仍然相同


Only Top of my server looks like只有我的服务器顶部看起来像

   const express = require("express");
    const socket = require("socket.io");
    const app = express();
    
    //Starts the server
    
    let server = app.listen(process.env.PORT||4000, function () {
      console.log("Server is running");
    });
    
    app.use(express.static(__dirname+"/public"));
    
    //Upgrades the server to accept websockets.
    
    let io = socket(server);
    
    //Triggered when a client is connected.
    
    io.on("connection", function (socket) {
      console.log("User Connected :" + socket.id);

Top part of client side is客户端的顶部是

let socket = io.connect("//192.168.1.73:4000");
let divVideoChatLobby = document.getElementById("video-chat-lobby");
let divVideoChat = document.getElementById("video-chat-room");
let joinButton = document.getElementById("join");
let userVideo = document.getElementById("user-video");
let peerVideo = document.getElementById("peer-video");
let roomInput = document.getElementById("roomName");
let roomName;
let creator = false;
let rtcPeerConnection;
let userStream;

// Contains the stun server URL we will be using.
let iceServers = {
  iceServers: [
    { urls: "stun:stun.services.mozilla.com"},
    { urls: "stun:stun.l.google.com:19302"},
  ],
};

Error on running on running server on 192.168.1.73:4000 is在 192.168.1.73:4000 上运行的服务器上运行时出错是

chat.js:37 Uncaught TypeError: Cannot read property 'getUserMedia' of undefined

    at f.<anonymous> (chat.js:37)
    at f.r.emit (socket.io-3.0.1.min.js:6)
    at f.value (socket.io-3.0.1.min.js:6)
    at f.value (socket.io-3.0.1.min.js:6)
    at f.value (socket.io-3.0.1.min.js:6)
    at b.<anonymous> (socket.io-3.0.1.min.js:6)
    at b.r.emit (socket.io-3.0.1.min.js:6)
    at b.value (socket.io-3.0.1.min.js:6)
    at n.<anonymous> (socket.io-3.0.1.min.js:6)
    at n.r.emit (socket.io-3.0.1.min.js:6)

(anonymous) @ chat.js:37
r.emit @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
(anonymous) @ socket.io-3.0.1.min.js:6
r.emit @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
(anonymous) @ socket.io-3.0.1.min.js:6
r.emit @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
(anonymous) @ socket.io-3.0.1.min.js:6
r.emit @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
(anonymous) @ socket.io-3.0.1.min.js:6
r.emit @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
value @ socket.io-3.0.1.min.js:6
ws.onmessage @ socket.io-3.0.1.min.js:6

on Heroku error looks like在 Heroku 错误看起来像

GET https://projectnepflix.herokuapp.com:4000/socket.io/?EIO=4&transport=polling&t=NVEwhoG net::ERR_CONNECTION_TIMED_OUT on Chrome在 Chrome 上获取https://projectnepflix.herokuapp.com:4000/socket.io/?EIO=4&transport=polling&t=NVEwhoG net::ERR_CONNECTION_TIMED_OUT

git repo sample: https://github.com/innovatoraakash/nepflix git 回购样本: https://github.com/innovatoraakash/nepflix

I've had this same problem a while ago.前段时间我也遇到过同样的问题。

This note from getUserMedia documentation describes the reason for your issue on the server: getUserMedia 文档中的此注释描述了您在服务器上出现问题的原因:

Note: If the current document isn't loaded securely, navigator.mediaDevices will be undefined, and you cannot use getUserMedia().注意:如果当前文档没有安全加载,navigator.mediaDevices 将是未定义的,并且您不能使用 getUserMedia()。 See Security for more information on this and other security issues related to using getUserMedia()有关此问题以及与使用 getUserMedia() 相关的其他安全问题的更多信息,请参阅安全性

This means that, you have run your app with HTTPS (unless you're localhost), or else navigator.mediaDevices will be undefined as stated above, hence when you try to call navigator.mediaDevices.getUserMedia it crashes.这意味着,您已经使用 HTTPS 运行了您的应用程序(除非您是本地主机),否则navigator.mediaDevices将如上所述未定义,因此当您尝试调用navigator.mediaDevices.getUserMedia时它会崩溃。

It was difficult to make my public IP HTTPS over my network so I changed the很难通过我的网络公开我的 IP HTTPS 所以我改变了

let socket = io.connect("//192.168.1.73:4000"); 

line with符合

var socket = io.connect(window.location.href);

This was the reason for the error in Heroku and I Tested finely over Heroku as it runs over https so navigator.mediaDevices.getUserMedi does not crash这就是 Heroku 中出现错误的原因,我在 Heroku 上进行了精细测试,因为它在 https 上运行,因此 navigator.mediaDevices.getUserMedi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM