On this page
搭建直播服务器
安装
shell
npm install node-media-server --save
文档
https://github.com/illuspas/Node-Media-Server/blob/master/README_CN.md
配置
config/config.default.js
js
// 流媒体配置
config.mediaServer = {
rtmp: {
port: 23480,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60,
},
http: {
port: 23481,
allow_origin: "*",
},
auth: {
play: true,
publish: true,
secret: "nodemedia2017privatekey",
},
};
启动流媒体服务:app.js
js
// app.js
// 引入
const NodeMediaServer = require("node-media-server");
class AppBootHook {
constructor(app) {
this.app = app;
}
configWillLoad() {
// 此时 config 文件已经被读取并合并,但是还并未生效
// 这是应用层修改配置的最后时机
// 注意:此函数只支持同步调用
}
async didLoad() {
// 所有的配置已经加载完毕
// 可以用来加载应用自定义的文件,启动自定义的服务
// 启动流媒体服务
if (!this.app.nms) {
this.app.nms = new NodeMediaServer(this.app.config.mediaServer);
this.app.nms.run();
this.app.nms.on("preConnect", (id, args) => {
console.log(
"[NodeEvent on preConnect]",
`id=${id} args=${JSON.stringify(args)}`,
);
// let session = nms.getSession(id);
// session.reject();
});
this.app.nms.on("postConnect", (id, args) => {
console.log(
"[NodeEvent on postConnect]",
`id=${id} args=${JSON.stringify(args)}`,
);
});
this.app.nms.on("doneConnect", (id, args) => {
console.log(
"[NodeEvent on doneConnect]",
`id=${id} args=${JSON.stringify(args)}`,
);
});
this.app.nms.on("prePublish", (id, StreamPath, args) => {
console.log(
"[NodeEvent on prePublish]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
// let session = nms.getSession(id);
// session.reject();
});
this.app.nms.on("postPublish", (id, StreamPath, args) => {
console.log(
"[NodeEvent on postPublish]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
});
this.app.nms.on("donePublish", (id, StreamPath, args) => {
console.log(
"[NodeEvent on donePublish]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
});
this.app.nms.on("prePlay", (id, StreamPath, args) => {
console.log(
"[NodeEvent on prePlay]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
// let session = nms.getSession(id);
// session.reject();
});
this.app.nms.on("postPlay", (id, StreamPath, args) => {
console.log(
"[NodeEvent on postPlay]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
});
this.app.nms.on("donePlay", (id, StreamPath, args) => {
console.log(
"[NodeEvent on donePlay]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
});
}
}
async willReady() {
// 所有的插件都已启动完毕,但是应用整体还未 ready
// 可以做一些数据初始化等操作,这些操作成功才会启动应用
}
async didReady() {
// 应用已经启动完毕
}
async serverDidReady() {
// http / https server 已启动,开始接受外部请求
// 此时可以从 app.server 拿到 server 的实例
// this.app.server.on('timeout', socket => {
// // handle socket timeout
// });
}
}
module.exports = AppBootHook;