Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// The lighting controller expects a JSON-formatted array of lights states in the following format:
[
{
"lightId": Number,
"state": Boolean
}
];
// Sample usage: Turning on lights 1, 2 and 3. Turning off light 4.
[
{
"lightId": 1,
"state": true
},
{
"lightId": 2,
"state": true
},
{
"lightId": 3,
"state": true
},
{
"lightId": 4,
"state": false
}
];
// Note: Any lights not specifically addressed in the message will not change state.
// If light 7 was on before the message above was sent, it will still be on afterwards.
// Example Server Implementation
const app = require('http').createServer();
const io = require('socket.io')(app);
app.listen(8000);
io.on('connection', function(socket) {
// Send random light data every second
setInterval(() => {
let lightData = [];
for (let i = 0; i < 14; i++) {
lightData.push({
lightId: i,
state: Math.random() > 0.5 ? true : false
});
}
socket.emit('message', lightData);
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.