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: 80 };
|
|
// 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');
|
|
});
|