Skip to content

Commit

Permalink
websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianMrn committed Dec 8, 2018
1 parent e8cc4b2 commit a45b061
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
53 changes: 53 additions & 0 deletions ws-test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

<title>Websocket node-webcam example</title>

<style>
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#container {
width: 100%;
height: 100%;
position: absolute;
}
#img {
width: 100%;
display: block;
}
</style>

</head>

<body>

<div id="container">

<img id="img"></img>

</div>

<script>
(function() {
"use strict";
var container = document.getElementById( "container" );
var img = document.getElementById( "img" );
var ws = new WebSocket( "ws://localhost:9091" );
ws.onmessage = function( data ) {
img.src = data.data;
};
})();
</script>

</body>

</html>
94 changes: 94 additions & 0 deletions ws-test/ws.js
Original file line number Diff line number Diff line change
@@ -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();

}
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"

0 comments on commit a45b061

Please sign in to comment.