Files
spore/test/http-cluster-broadcast-color.js

53 lines
1.3 KiB
JavaScript

// Simple HTTP client to broadcast a neopattern color change to the cluster
// Usage: node cluster-broadcast-color.js 10.0.1.53
const http = require('http');
const host = process.argv[2] || '127.0.0.1';
const port = 80;
const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF'];
let idx = 0;
function postClusterEvent(event, payloadObj) {
const payload = encodeURIComponent(JSON.stringify(payloadObj));
const body = `event=${encodeURIComponent(event)}&payload=${payload}`;
const options = {
host,
port,
path: '/api/cluster/event',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body)
}
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
console.log('Response:', res.statusCode, data);
});
});
req.on('error', (err) => {
console.error('Request error:', err.message);
});
req.write(body);
req.end();
}
console.log(`Broadcasting color changes to http://${host}/api/cluster/event ...`);
setInterval(() => {
const color = colors[idx % colors.length];
idx++;
const payload = { color, brightness: 80 };
console.log('Broadcasting color:', payload);
postClusterEvent('api/neopattern/color', payload);
}, 5000);