leapjs
Version:
JavaScript client for the Leap Motion Controller
41 lines (37 loc) • 1.04 kB
JavaScript
var Frame = require('./frame')
var Event = function(data) {
this.type = data.type;
this.state = data.state;
};
var chooseProtocol = exports.chooseProtocol = function(header) {
var protocol;
switch(header.version) {
case 1:
case 2:
case 3:
case 4:
protocol = JSONProtocol(header.version, function(data) {
return data.event ? new Event(data.event) : new Frame(data);
});
protocol.sendBackground = function(connection, state) {
connection.send(protocol.encode({background: state}));
}
protocol.sendFocused = function(connection, state) {
connection.send(protocol.encode({focused: state}));
}
break;
default:
throw "unrecognized version";
}
return protocol;
}
var JSONProtocol = function(version, cb) {
var protocol = cb;
protocol.encode = function(message) {
return JSON.stringify(message);
}
protocol.version = version;
protocol.versionLong = 'Version ' + version;
protocol.type = 'protocol';
return protocol;
};