Skip to content

Conversation

jgraef
Copy link
Owner

@jgraef jgraef commented Jul 9, 2024

This implementation spawns a local task, which drives the websocket connection. The WebSocket struct that the user gets only contains channels to communicate with the task. This way the WebSocket is Send.

@jgraef jgraef added the enhancement New feature or request label Jul 9, 2024
@jgraef jgraef force-pushed the wasm-rewrite branch 2 times, most recently from 3e75043 to 1a059a9 Compare July 9, 2024 05:54
@jgraef jgraef marked this pull request as draft July 9, 2024 05:55
@jgraef jgraef mentioned this pull request Dec 23, 2024
@jgraef
Copy link
Owner Author

jgraef commented Dec 23, 2024

The new wasm-implementation seems to work. I added an assertion that WebSocket: Send + Sync, but this fails for native, as hyper::Upgraded is not Sync. Works for wasm now though :)

@imlk0
Copy link

imlk0 commented Jul 7, 2025

I got the same Send problem, hope to see this got merged.

@jgraef
Copy link
Owner Author

jgraef commented Jul 7, 2025

@imlk0

It might eventually get merged, but it's not on my todo list right now. The problem is that WebSocket will not be Sync on native due to limitations by hyper. It might be nice to have only the Send bound for both targets though.

The point is that the current implementation works, and I'm hesitant to switch it out with something that might break things.

What is the specific problem you have? I might be able to help with a workaround.

@imlk0
Copy link

imlk0 commented Jul 7, 2025

@imlk0

It might eventually get merged, but it's not on my todo list right now. The problem is that WebSocket will not be Sync on native due to limitations by hyper. It might be nice to have only the Send bound for both targets though.它最终可能会被合并,但这会儿不在我的待办事项列表中。问题在于由于 hyper 的限制,WebSocket 在原生环境下无法实现 Sync。不过,对于两个目标平台来说,也许最好都只保留 Send 约束。

The point is that the current implementation works, and I'm hesitant to switch it out with something that might break things.关键是当前的实现是有效的,我担心将其替换为可能会导致问题的新实现。

What is the specific problem you have? I might be able to help with a workaround.你遇到的具体问题是什么?我或许能帮忙提供一个变通方案。

Hi, @jgraef, thanks for your quick reply!

I'm try to turn a WebSocket struct into a AsyncRead + AsyncWrite, and I have write some code with StreamReader and SinkWriter and got problem, since I realized that I actually need AsyncRead + AsyncWrite + Send.

I will try the code of this branch and see if it solve my problem.

@jgraef
Copy link
Owner Author

jgraef commented Jul 7, 2025

This branch is a little bit out of date, but should work fine.

As far as I can tell StreamReader/StreamWriter doesn't have a Send bound. The requirement for a Send bound usually comes when you move a type across thread boundaries. But there are no threads in WASM. For example wasm_bindgen_futures::spawn_local spawns a task, but doesn't require that bound. Sometimes libraries require it though.

Feel free to reach out if you run into any problems with this branch. I might even bring it up-to-date with the master branch, if you need that. After all it's good to know that this feature is needed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants