Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 17, 2015 05:09
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 joyrexus/5555728 to your computer and use it in GitHub Desktop.
Save joyrexus/5555728 to your computer and use it in GitHub Desktop.
Pipe/save a specified number of frames streamed from the Leap's websocket.
#!/usr/bin/env coffee
###
leap.rec - save a set of frames streamed from the Leap's websocket.
USAGE
leap.rec.coffee [no.-of-frames [file.json]]
leap.rec.coffee # stream 100 frames to stdout
leap.rec.coffee 1000 # stream 1000 frames to stdout
leap.rec.coffee 1000 hand.json # stream 1000 frames to hand.json
###
WebSocket = require 'ws'
ws = new WebSocket 'ws://localhost:6437'
fs = require 'fs'
max = parseInt process.argv[2]
max or= 100
file = process.argv[3]
out = if file then fs.createWriteStream(file) else process.stdout
print = console.log
i = 0 # frames seen
ws.on 'open', ->
print ' leap socket opened' if file
ws.send JSON.stringify {enableGestures: true}
out.write '[\n' # opening bracket for JSON payload
ws.on 'close', (code, reason) ->
out.write ']\n' # closing bracket for JSON payload
if file
print ' leap socket closed'
print " #{ws.bytesReceived} bytes received"
print " #{max} frames written to #{file}"
print reason if reason
ws.on 'error', (err) -> print err
ws.on 'message', (d) ->
if i == 0 # first frame of stream
print " version #{JSON.parse(d).version}" # print (but not write) stream version
else if max > i
out.write "#{d},\n" # include trailing comma for JSON
else if max == i
out.write "#{d}\n" # exclude trailing comma for JSON
else
ws.close()
i += 1 # increment frame count
#!/usr/bin/env node
/*
leap.rec.js - save a set of frames streamed from the Leap's websocket.
USAGE
leap.rec.js [no.-of-frames [file.json]]
leap.rec.js # stream 100 frames to stdout
leap.rec.js 1000 # stream 1000 frames to stdout
leap.rec.js 1000 hand.json # stream 1000 frames to hand.json
*/
(function() {
var WebSocket, file, fs, i, max, out, print, ws;
WebSocket = require('ws');
ws = new WebSocket('ws://localhost:6437');
fs = require('fs');
max = parseInt(process.argv[2]);
max || (max = 100);
file = process.argv[3];
out = file ? fs.createWriteStream(file) : process.stdout;
print = console.log;
i = 0;
ws.on('open', function() {
if (file) {
print(' leap socket opened');
}
ws.send(JSON.stringify({
enableGestures: true
}));
return out.write('[\n');
});
ws.on('close', function(code, reason) {
out.write(']\n');
if (file) {
print(' leap socket closed');
print(" " + ws.bytesReceived + " bytes received");
print(" " + max + " frames written to " + file);
if (reason) {
return print(reason);
}
}
});
ws.on('error', function(err) {
return print(err);
});
ws.on('message', function(d) {
if (i === 0) {
print(" version " + (JSON.parse(d).version));
} else if (max > i) {
out.write("" + d + ",\n");
} else if (max === i) {
out.write("" + d + "\n");
} else {
ws.close();
}
return i += 1;
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment