我正在忙着处理一些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
我不知道为什么会这样,我确定我做的事情非常愚蠢
答案 0 :(得分:0)
您可以在应用程序对象上调用函数joinRoom。
var easyrtcServer = easyrtc.listen(app, socketServer, null,
function(err, rtc) {
if (err) console.log(err);
rtc.createApp(
"rtcApp", null, rtcAppCallback
);
});
var rtcAppCallback = function(err, appObj) {
if (err) console.log(err);
app.use('/', router(appObj));
};