forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_rtmp_server.js
50 lines (41 loc) · 1.23 KB
/
node_rtmp_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// 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 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;
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();
})
}
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.');
});
}
stop() {
this.tcpServer.close();
context.sessions.forEach((session, id) => {
if (session instanceof NodeRtmpSession) {
session.socket.destroy();
context.sessions.delete(id);
}
});
}
}
module.exports = NodeRtmpServer