ApiServer: add AsyncWebSocket at /ws; accept JSON {event, payload} (string or object) and dispatch via ctx.fire; mirror all local events to clients using NodeContext::onAny.\nNodeContext: add onAny subscriber API.\nNeoPatternService: add api/neopattern/color event to set solid color.\nCluster: centralize cluster/broadcast sending in core; services delegate.\nAPI: add generic /api/node/event and /api/cluster/event endpoints in respective services.\nTests: add ws-color-client, ws-cluster-broadcast-color, http-cluster-broadcast-color.\nDocs: add StreamingAPI.md; update README and test/README.\nFixes: robust WS JSON parsing on ESP8266 and payload handling.
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
// Simple WebSocket client to test streaming API color changes
|
|
// Usage: node ws-color-client.js ws://<device-ip>/ws
|
|
|
|
const WebSocket = require('ws');
|
|
|
|
const url = process.argv[2] || 'ws://127.0.0.1/ws';
|
|
const ws = new WebSocket(url);
|
|
|
|
const colors = [
|
|
'#FF0000', // red
|
|
'#00FF00', // green
|
|
'#0000FF', // blue
|
|
'#FFFF00', // yellow
|
|
'#FF00FF', // magenta
|
|
'#00FFFF' // cyan
|
|
];
|
|
let idx = 0;
|
|
|
|
ws.on('open', () => {
|
|
console.log('Connected to', url);
|
|
// Send a message every 5 seconds to set solid color
|
|
setInterval(() => {
|
|
const color = colors[idx % colors.length];
|
|
idx++;
|
|
const payload = { color, brightness: 128 };
|
|
// Send payload as an object (server supports string or object)
|
|
const msg = { event: 'api/neopattern/color', payload };
|
|
ws.send(JSON.stringify(msg));
|
|
console.log('Sent color event', payload);
|
|
}, 5000);
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
try {
|
|
const msg = JSON.parse(data.toString());
|
|
console.log('Received:', msg);
|
|
} catch (e) {
|
|
console.log('Received raw:', data.toString());
|
|
}
|
|
});
|
|
|
|
ws.on('error', (err) => {
|
|
console.error('WebSocket error:', err.message);
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
console.log('WebSocket closed');
|
|
});
|