-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Allow to set custom websocket factory #24552
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,24 @@ addToLibrary({ | |
addr = result[1]; | ||
port = parseInt(result[2], 10); | ||
} | ||
} else { | ||
} | ||
#if expectToReceiveOnModule('websocket') | ||
else if (typeof SOCKFS.websocketArgs["factory"] === "function") { | ||
try { | ||
ws = SOCKFS.websocketArgs["factory"]({ | ||
addr, | ||
port, | ||
type: sock.type, | ||
family: sock.family, | ||
protocol: sock.protocol | ||
}); | ||
ws.binaryType = 'arraybuffer'; | ||
} catch (e) { | ||
throw new FS.ErrnoError({{{ cDefs.EHOSTUNREACH }}}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be enough to just replace Then the rest of the code in that block could be shared too? Or do you want to be able all the logic to do with Perhaps you could give more details about why / when these might be needed? Perhaps via and example and/or a test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think when I define ws factory, then I know url/subprotocol/etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, perhaps you can add a motivating example usage in the form of a test. Are we sure this cannot be achieved without modifying emscripten by monkey-patching the WebSocket global perhaps? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure a test will test anything related to sockets functionality. The change is aimed for post-emscripten module integration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we certainly need some kind of test for this new functionality. How about adding a test that shows that you can inject logging this way? @juj what do you think of this change? Can / should we leave this up to normal monkey patching mechanisms or should we provide this hook? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me this looks ok. It would indeed allow runtime resolution of websocket connections, as opposed to needing to know at compile time how to connect. Definitely would be great to have a test in sockets for this functionality, to ensure that the hook/dispatch mechanism is kept functional.
Patching WebSocket global is a bit tricky because it could then leak to other Emscripten WebSocket backends, altering their functionality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH I'm not sure I can add proper test. Maybe you can advice some existing test, which I can alter/modify? |
||
} | ||
#endif | ||
else { | ||
// create the actual websocket object and connect | ||
try { | ||
// The default value is 'ws://' the replace is needed because the compiler replaces '//' comments with '#' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason that this side of code is overwriting the .binaryType? Could the factory side do it before returning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emscripten/src/lib/libsockfs.js
Line 226 in bf5d63c
I guess it's required for TextEncoder/Decoder and logic in recv shim