Skip to content

Instantly share code, notes, and snippets.

@jedahan
Created June 22, 2014 19:50
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 jedahan/5ca1e7602504a7ad6e7a to your computer and use it in GitHub Desktop.
Save jedahan/5ca1e7602504a7ad6e7a to your computer and use it in GitHub Desktop.
<style>
body { background: #333355; }
body.connected { background: #620042; }
</style>
<script src="/socket.io/socket.io.js"></script>
<body>
<script>
var socket = io();
socket.on('change', function(val){
console.log('change ', val);
console.log('we got a change event');
if(val===1){
document.getElementsByTagName('body')[0].style.backgroundColor="#336633";
} else {
document.getElementsByTagName('body')[0].style.backgroundColor="#ff00ff";
}
});
</script>
</body>
var gpio = require("gpio");
var gpio4 = gpio.export(4, {
direction: "in",
ready: function() {
console.log('gpio has been exported');
}
});
var sockets = [];
console.log('woohoo');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
gpio4.on("change", function(val) {
console.log("CHANGE", val);
for(var i=0; i<sockets.length; i++){
sockets[i].emit('change',val);
}
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
socket.on('hi',function(val){
console.log(val);
});
sockets.push(socket);
console.log('a user connected');
socket.emit('hi','world');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment