-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
64 lines (53 loc) · 1.49 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
57
58
59
60
61
62
63
64
const net = require('net');
function sendCommand(command) {
const client = new net.Socket();
const host = 'localhost';
const port = 12345;
client.connect(port, host, () => {
console.log('Connected to server');
client.write(JSON.stringify(command));
});
client.on('data', (data) => {
const response = JSON.parse(data.toString());
console.log('Received:', response);
client.destroy(); // Close the connection after receiving the response
});
client.on('close', () => {
console.log('Connection closed');
});
client.on('error', (err) => {
console.error('Error:', err.message);
});
}
// Run Python Code Command
const runPythonCodeCommand = {
command: 'run_python_code',
code: `
import asyncio
async def main(tickrate):
result = {
"tickrate": str(tickrate.tickrate),
"task": await tickrate.add_task(tickrate.example_task, False, 1, 2)
}
return result
`
};
// Get Server Status
const getServerStatusCommand = {
command: 'get_server_status'
};
// Add Task Command
const addTaskCommand = {
command: 'add_task',
task_name: 'example_task',
args: [1, 2]
};
// Stop Server Command
const stopServerCommand = {
command: 'stop_server'
};
// Send Commands to Test
sendCommand(runPythonCodeCommand);
setTimeout(() => sendCommand(getServerStatusCommand), 2000);
setTimeout(() => sendCommand(addTaskCommand), 4000);
setTimeout(() => sendCommand(stopServerCommand), 6000);