Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca62807

Browse files
author
Mingliang Chen
committed
Use the global nodeEvent
1 parent 861a107 commit ca62807

6 files changed

Lines changed: 27 additions & 32 deletions

File tree

node_core_ctx.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ const EventEmitter = require('events');
88
let sessions = new Map();
99
let publishers = new Map();
1010
let idlePlayers = new Set();
11+
let nodeEvent = new EventEmitter();
1112

12-
13-
module.exports = { sessions, publishers, idlePlayers };
13+
module.exports = { sessions, publishers, idlePlayers, nodeEvent };

node_core_utils.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ const EventEmitter = require('events');
88
const context = require('./node_core_ctx');
99

1010
function generateNewSessionID() {
11-
let SessionID = '';
11+
let sessionID = '';
1212
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWKYZ0123456789';
1313
const numPossible = possible.length;
1414
do {
1515
for (var i = 0; i < 8; i++) {
16-
SessionID += possible.charAt((Math.random() * numPossible) | 0);
16+
sessionID += possible.charAt((Math.random() * numPossible) | 0);
1717
}
18-
} while (context.sessions.has(SessionID))
19-
return SessionID;
18+
} while (context.sessions.has(sessionID))
19+
return sessionID;
2020
}
2121

2222
function verifyAuth(signStr, streamId, secretKey) {
@@ -36,5 +36,4 @@ function verifyAuth(signStr, streamId, secretKey) {
3636
}
3737

3838
module.exports.generateNewSessionID = generateNewSessionID;
39-
module.exports.verifyAuth = verifyAuth;
40-
module.exports.nodeEvent = new EventEmitter();
39+
module.exports.verifyAuth = verifyAuth;

node_flv_session.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class NodeFlvSession extends EventEmitter {
2424
this.isPublisher = false;
2525
this.playStreamPath = '';
2626
this.playArgs = null;
27-
this.nodeEvent = NodeCoreUtils.nodeEvent;
2827

2928
this.on('connect', this.onConnect);
3029
this.on('play', this.onPlay);
@@ -53,7 +52,7 @@ class NodeFlvSession extends EventEmitter {
5352
let streamPath = urlInfo.pathname.split('.')[0];
5453
let format = urlInfo.pathname.split('.')[1];
5554
this.connectCmdObj = { method, streamPath, query: urlInfo.query };
56-
this.nodeEvent.emit('preConnect', this.id, this.connectCmdObj);
55+
context.nodeEvent.emit('preConnect', this.id, this.connectCmdObj);
5756

5857
this.isStarting = true;
5958
this.bp.init();
@@ -66,7 +65,7 @@ class NodeFlvSession extends EventEmitter {
6665
this.res.end();
6766
return;
6867
}
69-
this.nodeEvent.emit('postConnect', this.id, this.connectCmdObj);
68+
context.nodeEvent.emit('postConnect', this.id, this.connectCmdObj);
7069
if (method == 'GET') {
7170
//Play
7271
this.playStreamPath = streamPath;
@@ -128,10 +127,10 @@ class NodeFlvSession extends EventEmitter {
128127
let publisherId = context.publishers.get(this.playStreamPath);
129128
if (publisherId != null) {
130129
context.sessions.get(publisherId).players.delete(this.id);
131-
this.nodeEvent.emit('donePlay', this.id, this.playStreamPath, this.playArgs);
130+
context.nodeEvent.emit('donePlay', this.id, this.playStreamPath, this.playArgs);
132131
}
133132
}
134-
this.nodeEvent.emit('doneConnect', this.id, this.connectCmdObj);
133+
context.nodeEvent.emit('doneConnect', this.id, this.connectCmdObj);
135134
this.res.end();
136135
context.idlePlayers.delete(this.id);
137136
context.sessions.delete(this.id);
@@ -147,7 +146,7 @@ class NodeFlvSession extends EventEmitter {
147146

148147
onPlay() {
149148

150-
this.nodeEvent.emit('prePlay', this.id, this.playStreamPath, this.playArgs);
149+
context.nodeEvent.emit('prePlay', this.id, this.playStreamPath, this.playArgs);
151150
if (!this.isStarting) {
152151
return;
153152
}
@@ -228,7 +227,7 @@ class NodeFlvSession extends EventEmitter {
228227
}
229228
}
230229
console.log(`[${this.TAG} play] join stream ` + this.playStreamPath);
231-
this.nodeEvent.emit('postPlay', this.id, this.playStreamPath, this.playArgs);
230+
context.nodeEvent.emit('postPlay', this.id, this.playStreamPath, this.playArgs);
232231
}
233232

234233
onPublish() {

node_http_server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class NodeHttpServer {
2626
this.inbytes = 0;
2727
this.outbytes = 0;
2828
this.accepted = 0;
29-
this.nodeEvent = NodeCoreUtils.nodeEvent;
3029

3130
this.expressApp = Express();
3231
this.expressApp.all('*.flv', (req, res, next) => {
@@ -123,15 +122,15 @@ class NodeHttpServer {
123122
});
124123
}
125124

126-
this.nodeEvent.on('postPlay', (id, args) => {
125+
context.nodeEvent.on('postPlay', (id, args) => {
127126
this.accepted++;
128127
});
129128

130-
this.nodeEvent.on('postPublish', (id, args) => {
129+
context.nodeEvent.on('postPublish', (id, args) => {
131130
this.accepted++;
132131
});
133132

134-
this.nodeEvent.on('doneConnect', (id, args) => {
133+
context.nodeEvent.on('doneConnect', (id, args) => {
135134
let session = context.sessions.get(id);
136135
let socket = session instanceof NodeFlvSession ? session.req.socket : session.socket;
137136
this.inbytes += socket.bytesRead;

node_media_server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const context = require('./node_core_ctx');
1212
class NodeMediaServer {
1313
constructor(config) {
1414
this.config = config;
15-
this.nodeEvent = NodeCoreUtils.nodeEvent;
1615
}
1716

1817
run() {
@@ -22,14 +21,14 @@ class NodeMediaServer {
2221
}
2322

2423
if (this.config.http) {
25-
this.nhs = new NodeHttpServer(this.config, context.sessions, context.publishers, context.idlePlayers);
24+
this.nhs = new NodeHttpServer(this.config);
2625
this.nhs.run();
2726
}
2827

2928
}
3029

3130
on(eventName, listener) {
32-
this.nodeEvent.on(eventName, listener);
31+
context.nodeEvent.on(eventName, listener);
3332
}
3433

3534
stop() {

node_rtmp_session.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class NodeRtmpSession extends EventEmitter {
3838
this.config = config;
3939
this.id = NodeCoreUtils.generateNewSessionID();
4040
this.bp = new BufferPool(this.handleData());
41-
this.nodeEvent = NodeCoreUtils.nodeEvent;
4241
this.socket = socket;
4342
this.players = null;
4443

@@ -324,7 +323,7 @@ class NodeRtmpSession extends EventEmitter {
324323
clearImmediate(this.pingInterval);
325324
this.pingInterval = null;
326325
}
327-
this.nodeEvent.emit('doneConnect', this.id, this.connectCmdObj);
326+
context.nodeEvent.emit('doneConnect', this.id, this.connectCmdObj);
328327
this.socket.destroy();
329328
context.sessions.delete(this.id);
330329
}
@@ -811,7 +810,7 @@ class NodeRtmpSession extends EventEmitter {
811810

812811
onConnect(cmdObj) {
813812
cmdObj.app = cmdObj.app.replace('/', '');
814-
this.nodeEvent.emit('preConnect', this.id, cmdObj);
813+
context.nodeEvent.emit('preConnect', this.id, cmdObj);
815814
if (!this.isStarting) {
816815
return;
817816
}
@@ -828,11 +827,11 @@ class NodeRtmpSession extends EventEmitter {
828827
this.pingRequest();
829828
}, this.ping);
830829
console.log('[rtmp connect] app: ' + cmdObj.app);
831-
this.nodeEvent.emit('postConnect', this.id, cmdObj);
830+
context.nodeEvent.emit('postConnect', this.id, cmdObj);
832831
}
833832

834833
onPublish() {
835-
this.nodeEvent.emit('prePublish', this.id, this.publishStreamPath, this.publishArgs);
834+
context.nodeEvent.emit('prePublish', this.id, this.publishStreamPath, this.publishArgs);
836835
if (!this.isStarting) {
837836
return;
838837
}
@@ -864,12 +863,12 @@ class NodeRtmpSession extends EventEmitter {
864863
context.idlePlayers.delete(idlePlayerId);
865864
}
866865
}
867-
this.nodeEvent.emit('postPublish', this.id, this.publishStreamPath, this.publishArgs);
866+
context.nodeEvent.emit('postPublish', this.id, this.publishStreamPath, this.publishArgs);
868867
}
869868
}
870869

871870
onPlay() {
872-
this.nodeEvent.emit('prePlay', this.id, this.playStreamPath, this.playArgs);
871+
context.nodeEvent.emit('prePlay', this.id, this.playStreamPath, this.playArgs);
873872
if (!this.isStarting) {
874873
return;
875874
}
@@ -951,7 +950,7 @@ class NodeRtmpSession extends EventEmitter {
951950

952951
console.log("[rtmp play] join stream " + this.playStreamPath + ' streamId:' + this.playStreamId);
953952
players.add(this.id);
954-
this.nodeEvent.emit('postPlay', this.id, this.playStreamPath, this.playArgs);
953+
context.nodeEvent.emit('postPlay', this.id, this.playStreamPath, this.playArgs);
955954
}
956955
}
957956

@@ -972,7 +971,7 @@ class NodeRtmpSession extends EventEmitter {
972971
}
973972
this.isPlaying = false;
974973
this.playStreamId = del ? 0 : this.playStreamId;
975-
this.nodeEvent.emit('donePlay', this.id, this.playStreamPath, this.playArgs);
974+
context.nodeEvent.emit('donePlay', this.id, this.playStreamPath, this.playArgs);
976975
}
977976

978977
if (this.isPublishing && this.publishStreamId == streamID) {
@@ -1002,7 +1001,7 @@ class NodeRtmpSession extends EventEmitter {
10021001
context.publishers.delete(this.publishStreamPath);
10031002
this.isPublishing = false;
10041003
this.publishStreamId = del ? 0 : this.publishStreamId;
1005-
this.nodeEvent.emit('donePublish', this.id, this.publishStreamPath, this.publishArgs);
1004+
context.nodeEvent.emit('donePublish', this.id, this.publishStreamPath, this.publishArgs);
10061005
}
10071006
}
10081007

0 commit comments

Comments
 (0)