-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
114 lines (107 loc) · 2.89 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"use strict";
const net = require('net')
const dgram = require('dgram')
const WSServer = require('uws').Server
const Split = require('stream-split')
const NALSeparator = new Buffer([0, 0, 0, 1])
const express = require('express')
const systemd = require('systemd')
const app = express()
var wsServer, conf = require('nconf'),
headers = []
conf.argv().defaults({
tcpport: 8000,
udpport: 8000,
wsport: 8081,
queryport: false,
limit: 150
})
if (conf.get('queryport')) {
app.get('/', (req, res) => {
var count = 0
wsServer.clients.forEach((ws) => {
if (ws.readyState == 1) {
count++
}
})
res.set('Content-type', 'text/plain')
res.send(count.toString())
})
app.listen(conf.get('queryport'))
}
function broadcast(data) {
wsServer.clients.forEach((ws) => {
if (ws.readyState === 1) {
ws.send(data, { binary: true })
}
})
}
if (conf.get('tcpport')) {
const tcpServer = net.createServer((socket) => {
console.log('streamer connected')
socket.on('end', () => {
console.log('streamer disconnected')
})
headers = []
const NALSplitter = new Split(NALSeparator)
NALSplitter.on('data', (data) => {
if (wsServer && wsServer.clients.length > 0) {
if (headers.length < 3) headers.push(data)
broadcast(data)
}
}).on('error', (e) => {
console.log('splitter error ' + e)
process.exit(0)
})
socket.pipe(NALSplitter)
})
tcpServer.listen(conf.get('tcpport'))
if (conf.get('tcpport') == 'systemd') {
console.log('TCP server listening on systemd socket')
} else {
var address = tcpServer.address()
if (address) console.log(
`TCP server listening on ${address.address}:${address.port}`)
}
}
if (conf.get('udpport')) {
const udpServer = dgram.createSocket('udp4')
udpServer.on('listening', () => {
var address = udpServer.address()
console.log(
`UDP server listening on ${address.address}:${address.port}`)
})
const NALSplitter = new Split(NALSeparator)
NALSplitter.on('data', (data) => {
if (wsServer && wsServer.clients.length > 0) {
broadcast(data)
}
}).on('error', (e) => {
console.log('splitter error ' + e)
process.exit(0)
})
udpServer.on('message', (msg, rinfo) => {
NALSplitter.write(msg)
})
udpServer.bind(conf.get('udpport'))
}
if (conf.get('wsport')) {
wsServer = new WSServer({ port: conf.get('wsport') })
console.log(
`WS server listening on`, conf.get('wsport')
)
wsServer.on('connection', (ws) => {
if (wsServer.clients.length >= conf.get('limit')) {
console.log('client rejected, limit reached')
ws.close()
return
}
console.log('client connected, watching ' + wsServer.clients.length)
for (let i in headers) {
ws.send(headers[i])
}
ws.on('close', (ws, id) => {
console.log('client disconnected, watching ' + wsServer.clients.length)
})
})
}