-
Notifications
You must be signed in to change notification settings - Fork 3
/
PacketSender.js
43 lines (39 loc) · 1.21 KB
/
PacketSender.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
import Packet from './Packet';
import { Socket } from 'net';
export default class PacketSender {
constructor(port, ip, onError) {
this.port = port;
this.ip = ip;
this.onError = onError;
this.currentPacket = new Packet();
this.socket = new Socket();
}
connect() {
this.socket = new Socket();
this.socket.setTimeout(0);
this.socket.on('data', function (data) { console.log('Received: ' + data); });
const connectError = () => {
console.log('Can\'t connect to TCP socket. (' + this.ip + ':' + this.port + ')');
if (this.onError) this.onError();
}
this.socket.on('timeout', connectError);
this.socket.on('error', connectError);
this.socket.connect(this.port, this.ip);
this.intervalSender = setInterval(() => {
try {
this.socket.write(this.currentPacket.getBuffer());
} catch (e) {
console.log('TCP socket disconnected. (' + this.ip + ':' + this.port + ')');
if (this.onError) this.onError();
}
}, 100);
}
updatePacket(parameters) {
this.currentPacket = new Packet(parameters);
}
disconnect() {
clearInterval(this.intervalSender);
this.currentPacket = new Packet();
this.socket.end();
}
}