1 Star 0 Fork 0

fengyongjun/Node-Media-Server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
node_rtmp_server.js 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
//
// Created by Mingliang Chen on 17/8/1.
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//
const Logger = require('./node_core_logger');
const Tls = require('tls');
const Fs = require('fs');
const Net = require('net');
const NodeRtmpSession = require('./node_rtmp_session');
const NodeCoreUtils = require('./node_core_utils');
const context = require('./node_core_ctx');
const RTMP_PORT = 1935;
const RTMPS_PORT = 443;
class NodeRtmpServer {
constructor(config) {
config.rtmp.port = this.port = config.rtmp.port ? config.rtmp.port : RTMP_PORT;
this.tcpServer = Net.createServer((socket) => {
let session = new NodeRtmpSession(config, socket);
session.run();
})
if (config.rtmp.ssl){
config.rtmp.ssl.port = this.sslPort = config.rtmp.ssl.port ? config.rtmp.ssl.port : RTMPS_PORT;
try {
const options = {
key: Fs.readFileSync(config.rtmp.ssl.key),
cert: Fs.readFileSync(config.rtmp.ssl.cert)
}
this.tlsServer = Tls.createServer(options, (socket) => {
let session = new NodeRtmpSession(config, socket);
session.run();
});
} catch (e) {
Logger.error(`Node Media Rtmps Server error while reading ssl certs: <${e}>`);
}
}
}
run() {
this.tcpServer.listen(this.port, () => {
Logger.log(`Node Media Rtmp Server started on port: ${this.port}`);
});
this.tcpServer.on('error', (e) => {
Logger.error(`Node Media Rtmp Server ${e}`);
});
this.tcpServer.on('close', () => {
Logger.log(`Node Media Rtmp Server Close.`);
});
if (this.tlsServer) {
this.tlsServer.listen(this.sslPort, () => {
Logger.log(`Node Media Rtmps Server started on port: ${this.sslPort}`);
});
this.tlsServer.on('error', (e) => {
Logger.error(`Node Media Rtmps Server ${e}`);
});
this.tlsServer.on('close', () => {
Logger.log(`Node Media Rtmps Server Close.`);
});
}
}
stop() {
this.tcpServer.close();
if (this.tlsServer) {
this.tlsServer.close();
}
context.sessions.forEach((session, id) => {
if (session instanceof NodeRtmpSession)
session.stop();
});
}
}
module.exports = NodeRtmpServer
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fengyongjun/Node-Media-Server.git
[email protected]:fengyongjun/Node-Media-Server.git
fengyongjun
Node-Media-Server
Node-Media-Server
master

搜索帮助