-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·101 lines (98 loc) · 3.22 KB
/
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
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
#! /usr/bin/env node
var audionode = require('./')
, AudioReader = audionode.AudioReader
, AudioLeecher = audionode.AudioLeecher
, AudioMixer = audionode.AudioMixer
, net = require('net')
, http = require('http')
, url = require('url')
var rooms = {}
var webserver = http.createServer(function(req, res) {
var requrl = url.parse(req.url)
var urlparts = requrl.pathname.split('/')
if (urlparts[0] !== '' || urlparts[1] !== 'listen' || !urlparts[2]) {
res.writeHead(404, {'Content-Type': 'text/plain'});
return res.end('invalid url')
}
var roomName = urlparts[2]
if (!rooms.hasOwnProperty(roomName) || !rooms[roomName].ready) {
res.writeHead(404, {'Content-Type': 'text/plain'});
return res.end('room name is unused\n')
}
res.writeHead(200, {'Content-Type': 'audio/x-wav'})
new AudioLeecher({socket: res, audioReader: rooms[roomName], attachDelay: 10000})
})
webserver.listen(8001)
var server = net.createServer(function(c) {
var header = new Buffer(0)
c.on('data', headerDataListener)
function headerDataListener(chunk) {
// concat header
var newHeader = new Buffer(header.length + chunk.length)
header.copy(newHeader)
chunk.copy(newHeader, header.length)
var oldHeaderLength = header.length
header = newHeader
if (header.length > 1000) {
// anti-flooding
c.destroy()
}
for (var i=oldHeaderLength; i<header.length; i++) {
if (header[i] !== 0xa) continue
var headerStr = header.slice(0, i).toString().trim() // trim() for \r and stuff
var firstChunk = header.slice(i+1)
var headerParts = headerStr.split(' ')
header = null
c.removeListener('data', headerDataListener)
if (headerParts.length !== 2) {
c.write('invalid header\n')
c.end()
break
}
var clientType = headerParts[0]
var roomName = headerParts[1]
if (clientType === 'provide') {
if (rooms.hasOwnProperty(roomName)) {
c.write('room name is already used\n')
c.end()
break
}
var audioReader = new AudioReader({inStream: c})
var audioMixer = new AudioMixer(audioReader)
rooms[roomName] = audioMixer
audioMixer.on('end', function() {
delete rooms[roomName]
})
audioReader.on('error', function() {
c.destroy()
})
audioReader.parse(firstChunk)
} else if (clientType === 'join') {
if (!rooms.hasOwnProperty(roomName) || !rooms[roomName].ready) {
c.write('room name is unused\n')
c.end()
break
}
var audioReader = new AudioReader({inStream: c})
var audioMixer = rooms[roomName]
audioMixer.addReader(audioReader)
audioReader.on('error', function() {
c.destroy()
})
audioReader.parse(firstChunk)
} else if (clientType === 'listen') {
if (!rooms.hasOwnProperty(roomName) || !rooms[roomName].ready) {
c.write('room name is unused\n')
c.end()
break
}
new AudioLeecher({socket: c, audioReader: rooms[roomName]})
} else {
c.write('invalid client type: needs to be provide/listen\n')
c.end()
}
break
}
}
})
server.listen(1289)