You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js调用EasyRTC的joinRoom函数报错:TypeError: easyrtc.joinRoom is not a function

EasyRTC TypeError: easyrtc.joinRoom is not a function - Server-Side Usage Mixup

Hey there, let's break down what's going wrong here. The core issue is that you're trying to call a client-side EasyRTC method directly on the server-side easyrtc object—those are two entirely different APIs!

Your Code Context

First, here's your server setup (index.js):

//// // setup main 
var express = require("express"); 
var http = require("http"); 
var socketio = require("socket.io"); 
var easyrtc = require("easyrtc"); 
var app = express(); 
var rootdir = "/opt/dev/nodejs/easyrtc/v100"; 

//// // middleware 
app.use("/public", express.static(rootdir + "/public")); 
app.set("view engine", "ejs"); 
app.set("views", rootdir + "/public/views"); 

//// // web server and socket server 
var httpserver = http.createServer(app); 
httpserver.listen(3000, function() { 
  console.log("Server Started : Port 3000"); 
}); 
var socketioserver = socketio.listen(httpserver, {"log level":1}); 

//// // easyrtc server (extract from easyrtc example file) 
easyrtc.setOption("logLevel", "debug"); 
var easyrtcserver = easyrtc.listen(app, socketioserver, null, function(err, rtcRef) { 
  rtcRef.events.on ("roomCreate", function(appObj, creatorConnectionObj, roomName, roomOptions, callback) { 
    appObj.events.defaultListeners.roomCreate ( 
      appObj, creatorConnectionObj, roomName, roomOptions, callback 
    ); 
  }); 
}); 

//// // routes 
var main_route = require("./public/logic/main/main_route.js"); 
app.use(main_route.router(express, easyrtc)); 

Server logs confirm it's running correctly:

info - EasyRTC: Starting EasyRTC Server (v1.1.0) on Node (v8.7.0) 
debug - EasyRTC: Emitting event 'startup' 
debug - EasyRTC: Running func 'onStartup' 
Server Started : Port 3000 
debug - EasyRTC: Configuring Http server 
debug - EasyRTC: Setting up demos to be accessed from '/demos/' 
debug - EasyRTC: Setting up API files to be accessed from '/easyrtc/' 
debug - EasyRTC: Configuring Socket server 
debug - EasyRTC: Creating application: 'default' 
debug - EasyRTC: [default] Room [default] Running func 'onRoomCreate' 
debug - EasyRTC: Creating room: 'default' with options: {} 
info - EasyRTC: EasyRTC Server Ready For Connections (v1.1.0) 

But your route code (main_route.js) is where the mistake happens:

exports.router = function(express, easyrtc) { 
  var router = express.Router(); 
  router.get("/", function(req, res) { 
    f_main(res, easyrtc); 
  }); 
  return router; 
} 

function f_main(res, easyrtc) { 
  //// // render the gui 
  res.render("main/main.ejs"); 
  //// // execute something to test although this fails 
  easyrtc.joinRoom("Nothing", null, function() {}, function() {}); 
} 

And you're getting this error:

TypeError: easyrtc.joinRoom is not a function 

Why This Is Happening

The easyrtc object you're passing to your route is the server-side EasyRTC module—it's designed for configuring the server, handling connection events, and managing server-side room logic. Methods like joinRoom are part of the client-side EasyRTC library (loaded in the browser via /easyrtc/easyrtc.js), not the server-side API.

How to Fix This

You need to move the joinRoom call to the client-side code in your EJS template:

  1. Update your main/main.ejs file to include the client-side EasyRTC script and run the join logic in the browser:
<!DOCTYPE html>
<html>
<head>
  <title>EasyRTC Test</title>
</head>
<body>
  <h1>EasyRTC Room Test</h1>

  <!-- Load client-side EasyRTC script (served by the EasyRTC server) -->
  <script src="/easyrtc/easyrtc.js"></script>
  <script>
    // Connect to the EasyRTC server first
    easyrtc.setSocketUrl("/");
    easyrtc.connect("default", 
      function(connectionObj) {
        console.log("Successfully connected to EasyRTC server");
        
        // Now call joinRoom from the client-side API
        easyrtc.joinRoom("Nothing", null, 
          function() {
            console.log("Joined room 'Nothing' successfully!");
          },
          function(error) {
            console.error("Failed to join room:", error);
          }
        );
      },
      function(error) {
        console.error("Failed to connect to EasyRTC server:", error);
      }
    );
  </script>
</body>
</html>
  1. Remove the easyrtc.joinRoom call from your server-side route code—your f_main function should just render the template:
function f_main(res, easyrtc) { 
  // Just render the GUI; client-side code will handle joining the room
  res.render("main/main.ejs"); 
}

If You Need Server-Side Room Management

If you want to interact with rooms from the server (like listing rooms, forcing a client to join, etc.), use the easyrtcserver object returned by easyrtc.listen in your index.js. For example:

// In index.js, after creating easyrtcserver
easyrtcserver.getRoomList(function(err, roomList) {
  if (!err) {
    console.log("Current active rooms:", roomList);
  }
});

Key Takeaway

Always remember:

  • Server-side EasyRTC = config, event handling, server-side room/admin logic
  • Client-side EasyRTC = connecting, joining rooms, peer-to-peer communication

内容的提问来源于stack exchange,提问作者TheLovelySausage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:43:44