-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplay.js
More file actions
89 lines (64 loc) · 1.82 KB
/
play.js
File metadata and controls
89 lines (64 loc) · 1.82 KB
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
/*
play.js -
A utility to playback packets to JS8Assistant that were recorded from
JS8Call using the record.js utility. Useful for testing.
This does not do any processing, it just sends the recorded packets one
after another with a delay given by delayMs defined below.
Usage:
$ node play.js filename
*/
const fs = require('fs')
const delayMs = 200; // time between sent packets
var args = process.argv.slice(2);
//console.log('args: ', args);
if(args.length != 1)
{
console.log("Usage: node play.js filename");
process.exit();
}
filename = args[0];
packets = [];
const loadData = (path) => {
try {
return fs.readFileSync(path, 'utf8')
} catch (err) {
console.error(err)
return false
}
}
console.log("testing...");
packets = JSON.parse(loadData(filename));
console.log(packets.length);
const net = require('net');
const server = new net.createServer();
//server.listen(2442, '127.0.0.1');
server.listen(2442, '0.0.0.0');
function sleep(millis)
{
return new Promise(resolve => setTimeout(resolve, millis));
}
async function sendPackets(socket)
{
for(i = 0; i < packets.length; i++)
{
socket.write(JSON.stringify(packets[i]) + '\n');
console.log(packets[i]);
console.log("packet: " + i);
await sleep(delayMs);
}
console.log("Packets sent...");
}
server.on('connection', function(socket)
{
console.log('A new connection has been established.');
// set a timer to start send the packets in 2 seconds (give JS8Assistant time to open its window)
setTimeout(sendPackets, 2000, socket);
socket.on('end', function()
{
console.log('Closing connection with the client');
});
socket.on('error', function(err)
{
console.log(`Error: ${err}`);
});
});