-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
56 lines (37 loc) · 1.24 KB
/
client.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
const Net = require('net');
class AudioClient {
constructor(audioEngine) {
let client = new Net.Socket();
client.on('error', this.constructor.error.bind(this));
client.on('connect', this.constructor.clientConnected.bind(this));
client.on('end', this.constructor.clientDisconnected.bind(this));
this.engine = audioEngine;
this.connection = client;
}
connect(host, port) {
this.connection.connect(port, host);
}
disconnect() {
this.engine.quit();
this.connection.end();
}
static clientConnected() {
// NOTE: executed from within the context of a class instance.
let host = this.connection.address().address;
let port = this.connection.address().port;
console.log('CLIENT: connected to', host, 'via local port', port);
this.engine.pipe(this.connection);
this.engine.start();
}
static clientDisconnected() {
// NOTE: executed from within the context of a class instance.
let host = this.connection.address().address;
let port = this.connection.address().port;
console.log('CLIENT: connected to', host, 'on port', port);
}
static error(err) {
// NOTE: executed from within the context of a class instance.
throw err;
}
}
module.exports = AudioClient;