forked from dochong/drachtio-freeswitch-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_server.js
33 lines (28 loc) · 1.01 KB
/
ws_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
const WebSocket = require('ws');
const fs = require('fs');
const argv = require('minimist')(process.argv.slice(2));
const recordingPath = argv._.length ? argv._[0] : '/tmp/audio.raw';
const port = argv.port && parseInt(argv.port) ? parseInt(argv.port) : 3001
let wstream;
console.log(`listening on port ${port}, writing incoming raw audio to file ${recordingPath}`);
const wss = new WebSocket.Server({
port,
handleProtocols: (protocols, req) => {
return 'audio.drachtio.org';
}
});
wss.on('connection', (ws, req) => {
console.log(`received connection from ${req.connection.remoteAddress}`);
wstream = fs.createWriteStream(recordingPath);
ws.on('message', (message) => {
if (typeof message === 'string') {
console.log(`received message: ${message}`);
} else if (message instanceof Buffer) {
wstream.write(message);
}
});
ws.on('close', (code, reason) => {
console.log(`socket closed ${code}:${reason}`);
wstream.end();
});
});