我正忙于阅读一些 EasyRTC 文档,以便了解它的工作原理。我在调用 EasyRTC 函数(easyApp、joinRoom 等)时遇到了一些麻烦。我确定这与我设置它的方式有关,但不知道为什么。
我的 node.js 服务器文件设置如下(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));
这似乎工作正常,因为我可以看到服务器输出到控制台的消息。
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)
所以我对此很满意,但是当我尝试使用我的主页(main_route.js)与服务器交互时,问题就来了
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() {});
}
该函数正在传递服务器创建的easyrtc对象,如果我回显它,我可以看到它是一个有效的对象,但是如果我调用joinRoom或任何其他函数,我会收到以下错误
TypeError: easyrtc.joinRoom is not a function
我不知道为什么会这样,但我确定我在做一些非常愚蠢的事情