-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
38 lines (29 loc) · 1.13 KB
/
example.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
var Osp = require('./index');
var osp = new Osp();
// Add plugin to handle packets with type 8
osp.plugin(8, function (packet, socket) {
console.log('Handler called for type 8 packet from: ' + socket.remoteAddress);
// Modify packet and/or data here
return packet;
});
// Add plugin to handle packets with type 2
osp.plugin(2, function (packet, socket) {
console.log('Handler called for type 2 packet with data: ' + JSON.stringify(packet.data));
if (packet.data.length < 2) {
console.log('Error');
return packet;
}
console.log('The device sent a response to COMMAND packet. CommandID: ' + packet.data.readUInt8(0) +
' Exit code: ' + packet.data.readUInt8(1));
return packet;
});
osp.on('tcpConnect', function (socket) {
console.log('TCP connection from: ' + socket.remoteAddress +':'+ socket.remotePort);
});
osp.on('message', function (packet, socket) {
console.log('Received packet with type ' + packet.type + ' from ' + socket.remoteAddress);
console.log(JSON.stringify(packet, null, 4));
});
osp.on('tcpDisConnect', function () {
console.log('TCP connection closed.');
});