Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to terminate a connection? #31

Closed
elementalTIMING opened this issue Feb 20, 2024 · 9 comments
Closed

How to terminate a connection? #31

elementalTIMING opened this issue Feb 20, 2024 · 9 comments

Comments

@elementalTIMING
Copy link

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

@foxxyz
Copy link
Owner

foxxyz commented Feb 21, 2024

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

What are you trying to accomplish?

@elementalTIMING
Copy link
Author

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.

@Julusian
Copy link
Contributor

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

@elementalTIMING
Copy link
Author

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.

@Julusian
Copy link
Contributor

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

@elementalTIMING
Copy link
Author

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?

@foxxyz
Copy link
Owner

foxxyz commented Feb 21, 2024

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
Copy link
Owner

foxxyz commented Feb 21, 2024

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!

@elementalTIMING
Copy link
Author

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.

@foxxyz foxxyz closed this as completed in 96365ea Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants