This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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