首页 > 解决方案 > 带有 webRTC(socket.io)的 node.js 上的视频聊天应用程序在 localhost 上运行良好,但在网络上出现错误

问题描述

我正在使用 WebRTC(socket.io) 在 node.js 上制作视频聊天应用程序。此应用程序在 localhost 上运行良好,但在使用我的 IP 地址在我的网络上运行时出错。另外,我在 Heroku 上尝试过,但错误仍然相同


只有我的服务器顶部看起来像

   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);

客户端的顶部是

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"},
  ],
};

在 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

关于 Heroku 错误看起来像

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

git repo 示例:https ://github.com/innovatoraakash/nepflix

标签: node.jssocket.iowebrtcstreamingchat

解决方案


前段时间我也遇到过同样的问题。

getUserMedia 文档中的此注释描述了您在服务器上出现问题的原因:

注意:如果当前文档没有安全加载,navigator.mediaDevices 将是未定义的,并且您不能使用 getUserMedia()。有关此问题以及与使用 getUserMedia() 相关的其他安全问题的更多信息,请参阅安全性

这意味着,您已经使用 HTTPS 运行您的应用程序(除非您是本地主机),否则navigator.mediaDevices将如上所述未定义,因此当您尝试调用navigator.mediaDevices.getUserMedia它时会崩溃。


推荐阅读