Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Last active September 18, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riebschlager/9f938a21cb6e7f53a15ddf7449ef53fd to your computer and use it in GitHub Desktop.
Save riebschlager/9f938a21cb6e7f53a15ddf7449ef53fd to your computer and use it in GitHub Desktop.
// 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