节点 - 媒体服务器:session.reject()不起作用
我正在尝试使用NPM软件包创建一个RTMP-Server: http://github.com/ Illuspas/Node-Media-Server 。因此,服务器工作正常,但我需要在其中实现身份验证。我正在尝试检查“ prefublish”事件的身份验证。我正在查询数据库并检索用户,如果找到了用户,我想让用户流其他拒绝。但是问题是,它不会离开它而是断开连接,然后流自动重新连接到它,然后再次断开连接,循环继续进行。如何解决此问题?
这是事件的代码:
const NodeMediaServer = require('node-media-server');
const config = require('./config').rtmp_server;
const db = require('./db');
const nms = new NodeMediaServer(config);
const getStreamKeyFromStreamPath = (path) => {
const parts = path.split('/');
return parts[parts.length - 1];
};
nms.on('prePublish', async (id, StreamPath, args) => {
const session = nms.getSession(id);
try {
const streamKey = getStreamKeyFromStreamPath(StreamPath);
const validStream = (
await db.query('SELECT * FROM public."People" WHERE stream_key = $1', [streamKey])
).rows[0];
console.log(validStream);
if (validStream) {
// do stuff
} else {
session.reject((reason) => {
console.log(reason);
});
}
console.log(
'[NodeEvent on prePublish]',
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
);
} catch (err) {
session.reject();
}
});
module.exports = nms;
以下是服务器入口点的代码:
require("dotenv").config();
const db = require("./db");
const nms = require("./nms");
// database connection
db.connect()
.then(() => {
console.log("Connected to database");
// start the rtmp server
nms.run();
})
.catch((err) => console.log(err.message));
这是DB文件:
const { Pool } = require('pg');
const connectionString = process.env.PG_CONNECTION_STRING;
const poolOptions = {
host: process.env.PG_HOST,
user: process.env.PG_USER,
port: process.env.PG_PORT,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
};
const pool = new Pool(process.env.NODE_ENV === 'production' ? connectionString : poolOptions);
module.exports = pool;
我解决该问题的过程:
- 我试图使用回调来处理数据库查询,而不是异步函数,但是它不起作用。
- 在调用
session.reject()
现在,我正在那里通过一个回调,但是
如果您对此有任何解决方案,则行为仍然相同,请告诉我。 提前致谢
I am trying to create an RTMP-server with the npm package: http://github.com/illuspas/Node-Media-Server. So the server works fine but I need to implement authentication in it. I am trying to check the authentication on "prePublish" event. I am querying the database and retrieving the user if the user was found then I want to let the user stream otherwise rejected. But the problem is, it doesn't leave it instead disconnects and then the stream automatically reconnected to it then it disconnects again and the loop goes on. How do I fix this problem?
Here is the code for the event:
const NodeMediaServer = require('node-media-server');
const config = require('./config').rtmp_server;
const db = require('./db');
const nms = new NodeMediaServer(config);
const getStreamKeyFromStreamPath = (path) => {
const parts = path.split('/');
return parts[parts.length - 1];
};
nms.on('prePublish', async (id, StreamPath, args) => {
const session = nms.getSession(id);
try {
const streamKey = getStreamKeyFromStreamPath(StreamPath);
const validStream = (
await db.query('SELECT * FROM public."People" WHERE stream_key = $1', [streamKey])
).rows[0];
console.log(validStream);
if (validStream) {
// do stuff
} else {
session.reject((reason) => {
console.log(reason);
});
}
console.log(
'[NodeEvent on prePublish]',
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
);
} catch (err) {
session.reject();
}
});
module.exports = nms;
Here is the code of the entry point of the server:
require("dotenv").config();
const db = require("./db");
const nms = require("./nms");
// database connection
db.connect()
.then(() => {
console.log("Connected to database");
// start the rtmp server
nms.run();
})
.catch((err) => console.log(err.message));
Here is the db file:
const { Pool } = require('pg');
const connectionString = process.env.PG_CONNECTION_STRING;
const poolOptions = {
host: process.env.PG_HOST,
user: process.env.PG_USER,
port: process.env.PG_PORT,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
};
const pool = new Pool(process.env.NODE_ENV === 'production' ? connectionString : poolOptions);
module.exports = pool;
My procedures to solve that problem:
- Instead of the async function, I tried to handle the database query using a callback but it didn't work.
- Before I was calling
session.reject()
now I am passing a callback there but the behavior is still the same
If you have any solution for that, please let me know.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使您分配异步函数,实际的profublish呼叫背包也不是异步。连接到数据库时,该连接在后台接受,从而导致重新连接。
解决此问题的一种方法是更改排放前出版物的实际功能,异步并在此处编写所有验证代码。以下是:
您想访问节点模块并找到节点 - 媒体服务器
您会找到一个称为
inpublish(InvokeMessage)
围绕第1050行的函数。 /p>在发射“ prefublish”的发射事件后,添加了您的验证代码。
最后,这是更新函数的示例代码:
The actual prePublish call-back is not async, even if you assign an async function; while connecting to the database, the connection is accepted in the background, thus resulting in reconnect.
One way to fix this is to change the actual function that emits the prePublish, to async and write all your validation code there. Here is how:
you want to access your node modules and find node-media-server
under src/node_rtmp_session.js you will find a function called
onPublish(invokeMessage)
around line 1050. Change it to async.after the emit event of "prePublish" add your validation code.
Finally, here is and example code of the updated function: