Problems using broadway.js and raspi-camera over websocket - websocket

What I wanna do is use the PI to control a small vehicle I build. I want to control this over a webinterface and also want to use the PI-camera to get a livestream when driving.
I am using nodejs on the PI with this module: https://www.npmjs.com/package/raspivid-stream to get the stream from the PI-camera. Here is my nodejs-code:
var raspividStream = require('raspivid-stream');
var stream = raspividStream();
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
stream.on('data', (data) => {
ws.send(data, { binary: true }, (error) => { if (error) console.error(error);
});
});
and here the index.html:
...htmlstuff...
<canvas id="stream"></canvas>
<script src="decoder.js"></script>
<script src="player.js"></script>
<script>
var connection = new WebSocket('ws://raspberrypi:8443');
var p = new Player();
p.canvas = document.getElementById("stream"); // the canvas - p$
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to $
};
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
connection.onmessage = function (e) {
console.log(e.data);
p.decode(e);
};
</script>
...htmlstuff...
So far I get this errors in the browser console:
wasm streaming compile failed: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
falling back to ArrayBuffer instantiation
failed to asynchronously prepare wasm: CompileError: AsyncCompile: Wasm decoding failed: expected magic word 00 61 73 6d, found 3c 21 44 4f #+0
CompileError: AsyncCompile: Wasm decoding failed: expected magic word 00 61 73 6d, found 3c 21 44 4f #+0
Uncaught (in promise) abort({}). Build with -s ASSERTIONS=1 for more info.
Anybody got an idea how to solve this?

There’s a static file asset in the latest version of Broadway called avc.wasm. You need to get your server to deliver it with the correct MIME Content-type, application/wasm so the decoder can load it.
Or you can try an older version of broadway using asm.js rather than WebAssembly. It’s here.

Related

Electron - Uncaught (in promise) Error: An object could not be cloned

I've this error when I click on my button:
Uncaught (in promise) Error: An object could not be cloned.
I start with electron, and I would like to take advantage of this topic to know the difference between invoke and send (I tried both, with the same error)
Main.js
ipcMain.handle('savePdf', (event, pdfFile) => {
console.log(event);
console.log(pdfFile);
return pdfFile;
}
preload.js
contextBridge.exposeInMainWorld('pdf', {
savePdf: (file) => ipcRenderer.invoke('savePdf', file),
})
rendered.js
const pdfFileInput = document.getElementById('input-file'); // OK
const pdfFile = pdfFileInput.files[0]; // OK
console.log(pdfFile); // OK
await pdf.savePdf(pdfFile);

Coinbase-pro for Node.js - Websocket connection breaking with error: read ECONNRESET

I'm currently stuck with an issue I'm getting with the coinbase-pro-node npm package (https://github.com/coinbase/coinbase-pro-node). I'm connecting to the matches channel and listening for messages there but the connection with the Websocket breaks after a few hours without telling me much. I can't trace back the problem and it doesn't happen on the same intervals. Sometimes it breaks just minutes after I run the script. Thanks for the help.
The code:
const CoinbasePro = require('coinbase-pro');
var coinbaseWs = '';
function connect() {
coinbaseWs = new CoinbasePro.WebsocketClient(
['BTC-USD'],
'wss://ws-feed.pro.coinbase.com',
{
key: 'xxxx',
secret: 'xxxx',
passphrase: 'xxxx',
},
{ channels: ['matches'] }
);
coinbaseWs.on('message', async data => {
console.log(data)
});
coinbaseWs.on('error', err => {
console.error("Connection with Coinbase websocket failed with error: " + err);
console.log("Error stack trace: " + err.stack);
});
coinbaseWs.on('close', () => {
console.error("Connection with Coinbase websocket closed!");
});
}
connect();
Error stack:
Error: read ECONNRESET
File "internal/stream_base_commons.js", line 167, in TLSWrap.onStreamRead
it does break from time to time for no apparent reason. All you can do is listen for the heartbeat messages and use those to decide whether to re-initiate a new websocket feed. I raised a similar query directly with the coinbase pro/gdax customer support.

Can I use https.request in vscode extension

I am creating a VSCode Extension.
I have added MSTranslator as a node module because I want to call the Microsoft Translator API
but when I run the example either nothing happens,
or I get a Error Connect EACCES. I get this error 2 or 3 times but now it has stopped returning anything, it just jumps the code and continues
I am running this on a MacBook
if (!api_key) {
console.log('missing api_key');
}
var params = {
text: 'How\'s it going?',
from: 'en',
to: 'es'
};
var client = new MsTranslator({ api_key: api_key });
client.initialize_token(function (err) {
if (err) {
console.log("initialize_token", err);
return;
}
client.translate(params, function (err, data) {
if (err) console.log('error:' + err.message);
console.log(data);
process.exit();
});
});
and when it enters client.initialize_token it reaches this line in MsTranslator and does nothing, just jumps out of function.
var req = https.request(self.options, function(res) {
My Radio Silence firewall is switched off, and the network monitor shows no activity.
If I call the same URL using the same details in self.options using Postman, I get back a token as expected.
Is there a problem running https.request within a VSCode extension, or do I have a different problem?

Intern ajax testing with node.js

I'm using intern with node.js, trying to set up intern for ajax testing. The server code below serves direct GET requests, but XHR request by Intern doesn't seem to reach to it. I suspect the problem is something to do with proxyUrl setup for the Node.
server/main.js: Node/express
...
app.get('/data', function(request, response, next){
if(request.xhr)
{
var data = dfs.readFileSync("src/server/data.json");
response.render(data, {
root: root,
error: new Error('Cant read file')
});
}
else
next();
});
...
app.listen(8001);
http_proxy.createServer(function(req,res, proxy){
proxy.proxyRequest(req,res, {host:'localhost', port:8001});
}).listen(9000);
intern.hello.js: (unit test code)
...
'async test': function () {
var dfd = this.async(1000);
request('/data').then(dfd.callback(function (data) {
assert.strictEqual(data, 'hello world');
}, dfd.reject.bind(dfd)));
}
intern config:
...
proxyPort:9000,
proxyUrl: 'http://localhost:8001/',
...
intern error:
Warning: FAIL: async test (1015ms)
Error: Timeout reached on main - wait - async test
at Error (<anonymous>)
at new ErrorCtor (.../node_modules/intern/node_modules/dojo/errors/create.js:13:21)
at null._onTimeout (.../node_modules/intern/lib/Test.js:164:21)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
2/3 tests passed
2/3 tests passed Use --force to continue.

socket.io join/leave

I'm on the socket.io wiki looking into using rooms but join and leave are not working, i'm wondering if they may have changed up a few things but not had the chance to update the wiki?
socket.join("room-"+data.meid);
socket.leave("room-"+meid);
cause im getting console errors:
Uncaught TypeError: Object #<SocketNamespace> has no method 'leave'
Uncaught TypeError: Object #<SocketNamespace> has no method 'join'
It looks like you had the socket.join on the client side. Its a server side function.
Put this on the server:
io.sockets.on('connection', function (socket) {
socket.on('subscribe', function(data) { socket.join(data.room); })
socket.on('unsubscribe', function(data) { socket.leave(data.room); })
});
setInterval(function(){
io.sockets.in('global').emit('roomChanged', { chicken: 'tasty' });
}, 1000);
And this on the client:
var socket = io.connect();
socket.emit("subscribe", { room: "global" });
socket.on("roomChanged", function(data) {
console.log("roomChanged", data);
});
You're probably not declaring 'socket' correctly either that of you haven't installed Socket-io correctly. Try the following...
var io = require("socket.io");
var socket = io.listen(80);
socket.join('room');
socket.leave('room');
There's a useful executable example here.

Resources