-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-websocket-client.js
More file actions
55 lines (46 loc) · 1.46 KB
/
test-websocket-client.js
File metadata and controls
55 lines (46 loc) · 1.46 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
/**
* WebSocket Client Test Script
*
* This script connects to the mock robot WebSocket server and tests the communication.
* The server should be running on localhost:8080 before executing this script.
*
* Installation and Usage with pnpm:
* 1. Install dependencies using pnpm:
* pnpm install
* 2. Build the project (optional but recommended):
* pnpm run build
* 3. Make sure the WebSocket server is running:
* node nodes/websocket-server.ts
* 4. Run this test script:
* node nodes/test-websocket-client.js
*
* The script will:
* - Connect to the WebSocket server
* - Send a test message in the expected format
* - Listen for and log the response from the server
*/
const WebSocket = require('ws');
// Connect to the WebSocket server
const ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open() {
console.log('Connected to WebSocket server');
// Send a test message in the format expected by the server
const testMessage = {
type: 'robot_action',
data: {
move: { direction: 'forward' }
},
original_query: 'Move the robot forward'
};
console.log('Sending test message:', testMessage);
ws.send(JSON.stringify(testMessage));
});
ws.on('message', function message(data) {
console.log('Received response from server:', data.toString());
});
ws.on('close', function close() {
console.log('Disconnected from server');
});
ws.on('error', function error(err) {
console.error('WebSocket error:', err);
});