From a45b061952f4cef6eda62c0d3418bd1fa376d3d5 Mon Sep 17 00:00:00 2001 From: Adriaan Date: Sat, 8 Dec 2018 03:55:30 +0100 Subject: [PATCH] websocket --- package.json | 5 ++- ws-test/index.html | 53 ++++++++++++++++++++++++++ ws-test/ws.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 22 +++++++++++ 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 ws-test/index.html create mode 100644 ws-test/ws.js diff --git a/package.json b/package.json index aff0bda..3f59fa3 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,10 @@ "dependencies": { "cors": "^2.8.5", "express": "^4.16.4", + "fs": "^0.0.1-security", + "http": "^0.0.0", "node-webcam": "^0.4.6", - "pi-servo-blaster.js": "0.0.3" + "pi-servo-blaster.js": "0.0.3", + "ws": "^6.1.2" } } diff --git a/ws-test/index.html b/ws-test/index.html new file mode 100644 index 0000000..6308c8c --- /dev/null +++ b/ws-test/index.html @@ -0,0 +1,53 @@ + + + + + + + + + Websocket node-webcam example + + + + + + + +
+ + + +
+ + + + + + diff --git a/ws-test/ws.js b/ws-test/ws.js new file mode 100644 index 0000000..287bfde --- /dev/null +++ b/ws-test/ws.js @@ -0,0 +1,94 @@ +/** + * Websocket server app + * + * Will use base64 return to the websocket clients and + * in memory capturing without saving + * + */ +"use strict"; + +var port = 9090; + +var HTTP = require( "http" ); + +var FS = require( "fs" ); + +var HTML_CONTENT = FS.readFileSync( __dirname + "/index.html" ); + +var WS = require( "ws" ); + +var WSS = new WS.Server({ port: 9091 }); + +// Broadcast to all. + +WSS.broadcast = function broadcast( data ) { + + WSS.clients.forEach( function each( client ) { + + client.send( data ); + + }); + +}; + +var NodeWebcam = require( "./../../" ); + +var Webcam = NodeWebcam.create({ + callbackReturn: "base64", + saveShots: false +}); + + +// Main + +init(); + +function init() { + + setupHTTP(); + + setupWebcam(); + + console.log( "Visit localhost:9090" ); + +} + +function setupHTTP() { + + var server = HTTP.createServer(); + + server.on( "request", function( request, response ) { + + response.write( HTML_CONTENT ); + + response.end(); + + }); + + server.listen( port ); + +} + +function setupWebcam() { + + function capture() { + + Webcam.capture( "picture", function( err, data ) { + + if( err ) { + + throw err; + + } + + WSS.broadcast( data ); + + setTimeout( capture, 25 ); + + }); + + } + + capture(); + +} diff --git a/yarn.lock b/yarn.lock index b1a357d..df9270f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -20,6 +20,11 @@ array-flatten@1.1.1: resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2" integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI= +async-limiter@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" + integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg== + body-parser@1.18.3: version "1.18.3" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.3.tgz#5b292198ffdd553b3a0f20ded0592b956955c8b4" @@ -165,6 +170,11 @@ fresh@0.5.2: resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac= +fs@^0.0.1-security: + version "0.0.1-security" + resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" + integrity sha1-invTcYa23d84E/I4WLV+yq9eQdQ= + http-errors@1.6.3, http-errors@~1.6.2, http-errors@~1.6.3: version "1.6.3" resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.3.tgz#8b55680bb4be283a0b5bf4ea2e38580be1d9320d" @@ -175,6 +185,11 @@ http-errors@1.6.3, http-errors@~1.6.2, http-errors@~1.6.3: setprototypeof "1.1.0" statuses ">= 1.4.0 < 2" +http@^0.0.0: + version "0.0.0" + resolved "https://registry.yarnpkg.com/http/-/http-0.0.0.tgz#86e6326d29c5d039de9fac584a45689f929f4f72" + integrity sha1-huYybSnF0Dnen6xYSkVon5KfT3I= + iconv-lite@0.4.23: version "0.4.23" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63" @@ -398,3 +413,10 @@ vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= + +ws@^6.1.2: + version "6.1.2" + resolved "https://registry.yarnpkg.com/ws/-/ws-6.1.2.tgz#3cc7462e98792f0ac679424148903ded3b9c3ad8" + integrity sha512-rfUqzvz0WxmSXtJpPMX2EeASXabOrSMk1ruMOV3JBTBjo4ac2lDjGGsbQSyxj8Odhw5fBib8ZKEjDNvgouNKYw== + dependencies: + async-limiter "~1.0.0"