loupedeck icon indicating copy to clipboard operation
loupedeck copied to clipboard

How to terminate a connection?

Open elementalTIMING opened this issue 1 year ago • 9 comments
trafficstars

Hi,

today I tried to programatically close the connection to the Loupedeck CT. So I used:

loupedeck.close();

Unfortunately this results in:

TypeError: Failed to execute 'close' on 'SerialPort': Cannot cancel a locked stream
    at web-serial.js:87:25
    at Generator.next (<anonymous>)

Do you have any idea or fix for that?

Thx and cheers, Lars

elementalTIMING avatar Feb 20 '24 09:02 elementalTIMING

I believe WebSerial connections are managed by your browser, so you can't manually close those.

What are you trying to accomplish?

foxxyz avatar Feb 21 '24 20:02 foxxyz

I believe WebSerial connections are managed by your browser, so you can't manually close those.

What are you trying to accomplish?

Currently, I'm trying to bring your software into an Angular project. Here I would like to allow (via a button) to turn on/off the "loupedeck interface". So it would be nice to open/close the connection.

BTW: I also discovered an issue with the serial connection. Everything works fine in the original Google Chrome. But not for any other browsers, like Brave (Chromium) or Firefox:

if (typeof navigator !== 'undefined' && navigator.serial) {
	-> CHROME
	SerialConnection = require('./connections/web-serial');
} else {
	-> OTHER BROWSERS
	// this is used for all browsrs other than Chrome but fails
	// SerialConnection = require('./connections/serial');
	// WSConnection = require('./connections/ws');
}

here 'OTHER BROWSERS' fail with errors. You can test it easily by yourself if you try to start the 'simple' demo, e.g. in the Brave browser.

elementalTIMING avatar Feb 21 '24 21:02 elementalTIMING

Generally only Chromium and its derivatives support webserial https://caniuse.com/web-serial

And while brave is chromium based, it explicitly disables webserial https://github.com/brave/brave-browser/wiki/Deviations-from-Chromium-(features-we-disable-or-remove)#what-chromium-features-are-removed-for-privacysecurity-reasons

Julusian avatar Feb 21 '24 21:02 Julusian

Generally only Chromium and its derivatives support webserial https://caniuse.com/web-serial

And while brave is chromium based, it explicitly disables webserial https://github.com/brave/brave-browser/wiki/Deviations-from-Chromium-(features-we-disable-or-remove)#what-chromium-features-are-removed-for-privacysecurity-reasons

yes, I know this. But from what I understood you have build in a fallback, didn't you? And this fallback doesn't work. I found that you used stream and serialport, but there are some polyfills available to be able to use this in a browser. The main problem seems to come from

class MagicByteLengthParser extends Transform

Here it is not possible to extend Transform. So I'm wondering if you could provide a version that works on all major browsers, too.

elementalTIMING avatar Feb 21 '24 21:02 elementalTIMING

hmm.. that fallback to me looks like that it is the nodejs mode, not for other browsers

Julusian avatar Feb 21 '24 21:02 Julusian

hmm.. that fallback to me looks like that it is the nodejs mode, not for other browsers

True, but wouldn't it be a good idea/possible/better :-) to make the code more general to make it working in nodejs but also for browsers?

elementalTIMING avatar Feb 21 '24 21:02 elementalTIMING

thanks @Julusian

@elementalPRESS I think there's a misunderstanding.

Unlike a general purpose programming environment (I.E. NodeJS), browsers are generally sandboxed execution environments that only allow access to low-level protocols that they create/enable API's for. This means that it is not possible to access serial ports in browsers that do not support or implement WebSerial, and there is no way to get around that.

foxxyz avatar Feb 21 '24 22:02 foxxyz

However, getting back on topic - I do think that .close() should execute correctly, even in browser, and if the port can't be closed by the application it's likely a bug on our side.

Reading this issue about closing a WebSerial connection makes me think we might need to release a lock somewhere before closing the connection. I will look into it.

Thanks for reporting!

foxxyz avatar Feb 21 '24 22:02 foxxyz

thanks @Julusian

@elementalPRESS I think there's a misunderstanding.

Unlike a general purpose programming environment (I.E. NodeJS), browsers are generally sandboxed execution environments that only allow access to low-level protocols that they create/enable API's for. This means that it is not possible to access serial ports in browsers that do not support or implement WebSerial, and there is no way to get around that.

Well, I guess it is possible using the polyfill versions of 'stream' and 'serial'.

web-serial-polyfill
@stardazed/streams-polyfill

I was already able to include it, but stopped because I had to change your package to a module. This migration failed doe to my missing knowledge of nodejs.

elementalTIMING avatar Feb 22 '24 06:02 elementalTIMING