Skip to content

Instantly share code, notes, and snippets.

@rondagdag
Created December 26, 2015 12:13
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 rondagdag/05facb8aede80a4d9c87 to your computer and use it in GitHub Desktop.
Save rondagdag/05facb8aede80a4d9c87 to your computer and use it in GitHub Desktop.
var ws = require("nodejs-websocket")
// Scream server example: "hi" -> "HI!!!"
var server = ws.createServer(function (conn) {
console.log("New connection")
conn.on("text", function (str) {
console.log("Received "+str)
conn.sendText(str.toUpperCase()+"!!!")
})
conn.on("close", function (code, reason) {
console.log("Connection closed")
})
}).listen(8001)
function broadcast(str) {
server.connections.forEach(function (connection) {
connection.sendText(str)
})
}
var five = require("johnny-five"),
board, button, slider;
board = new five.Board();
board.on("ready", function() {
// Create a new `button` hardware instance.
// This example allows the button module to
// create a completely default instance
button = new five.Button(0);
slider = new five.Sensor("A0");
// "slide" is an alias for "change"
slider.scale([0, 100]).on("slide", function() {
console.log("slide", this.value);
broadcast("slide:" + this.value);
});
// Inject the `button` hardware into
// the Repl instance's context;
// allows direct command line access
board.repl.inject({
button: button,
slider: slider
});
// Button Event API
// "down" the button is pressed
button.on("down", function() {
console.log("down");
broadcast("down");
});
// "hold" the button is pressed for specified time.
// defaults to 500ms (1/2 second)
// set
button.on("hold", function() {
console.log("hold");
broadcast("hold");
});
// "up" the button is released
button.on("up", function() {
console.log("up");
broadcast("up");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment