-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket-server.ts
More file actions
70 lines (60 loc) · 1.99 KB
/
websocket-server.ts
File metadata and controls
70 lines (60 loc) · 1.99 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
/**
* Mock Robot WebSocket Server
*
* This script creates a mock WebSocket server that simulates communication
* with a robot. It listens for robot_action messages and sends back
* simulated responses.
*
* Installation and Usage with pnpm:
* 1. Install dependencies using pnpm:
* pnpm install
* 2. Run the WebSocket server:
* node nodes/websocket-server.ts
* OR
* npx tsx nodes/websocket-server.ts
*
* Optional: Build the project with pnpm before running:
* pnpm build
*
* The server will:
* - Listen for connections on port 8080
* - Accept robot_action messages in the expected format
* - Simulate processing and send back action_response messages
*/
import WebSocket, { WebSocketServer } from 'ws';
// Create WebSocket server listening on port 8080
const wss = new WebSocketServer({ port: 8080 });
console.log('Mock Robot WebSocket Server running on port 8080');
wss.on('connection', (ws) => {
console.log('Robot connected to mock server');
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('Received from robot client:', message);
// Log the action received
if (message.type === 'robot_action') {
console.log(`Action received:`, message.data);
console.log(`Original query:`, message.original_query);
// Echo back a simulated response after a short delay
setTimeout(() => {
const response = {
type: 'action_response',
status: 'executed',
action: message.data,
timestamp: new Date().toISOString()
};
ws.send(JSON.stringify(response));
console.log('Sent response back to client:', response);
}, 1000);
}
} catch (error) {
console.error('Error parsing message:', error);
}
});
ws.on('close', () => {
console.log('Robot disconnected from mock server');
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});