feat(streaming): introduce WebSocket Streaming API bridging event bus
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.
This commit is contained in:
46
test/ws-cluster-broadcast-color.js
Normal file
46
test/ws-cluster-broadcast-color.js
Normal file
@@ -0,0 +1,46 @@
|
||||
// WebSocket client to broadcast neopattern color changes across the cluster
|
||||
// Usage: node ws-cluster-broadcast-color.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', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF'];
|
||||
let idx = 0;
|
||||
|
||||
ws.on('open', () => {
|
||||
console.log('Connected to', url);
|
||||
// Broadcast color change every 5 seconds via cluster/broadcast
|
||||
setInterval(() => {
|
||||
const color = colors[idx % colors.length];
|
||||
idx++;
|
||||
const payload = { color, brightness: 128 };
|
||||
const envelope = {
|
||||
event: 'api/neopattern/color',
|
||||
data: payload // server will serialize object payloads
|
||||
};
|
||||
const msg = { event: 'cluster/broadcast', payload: envelope };
|
||||
ws.send(JSON.stringify(msg));
|
||||
console.log('Broadcasted 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');
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user