UNPKG

rtmp.js

Version:

A Node.js implementation of RTMP/HTTP-FLV Media Server

42 lines (33 loc) 923 B
const net = require('net'); const RtmpSession = require('../session/rtmp'); const context = require('../context'); const Logger = require('../utils/logger'); class RtmpServer { constructor(config) { this.port = 1935; this.tcpServer = net.createServer((socket) => { const session = new RtmpSession(config, socket); session.run(); }); } run() { this.tcpServer.listen(this.port, () => { Logger.log(`Rtmp Server started on port: ${this.port}`); }); this.tcpServer.on('error', (e) => { Logger.error(`Rtmp Server ${e}`); }); this.tcpServer.on('close', () => { Logger.log('Rtmp Server Close.'); }); } stop() { this.tcpServer.close(); context.sessions.forEach((session, id) => { if (session instanceof RtmpSession) { session.stop(); } }); } } module.exports = RtmpServer;