Closed stratecide closed 1 month ago
I guess it should be a signal then, so you can easly get it in an effect, right?
That would also work, yes. So far I check ready_state in an effect and then (try to) send a message:
create_effect(move |_| {
match ready_state.get() {
ConnectionReadyState::Open => {
ws.send(...);
}
_ => ()
}
});
Why don't you use the send method that is returned? This is the intended way to use this function.
The kind of data my client sends to the server is different than the data sent by the server to the client. So I have created two enums: One for messages sent from the client to the server and one for messages sent from the server to the client.
The problem with that is that use_websocket forces the use of the same type for sending and receiving, since
pub message: Signal<Option<T>>,
is used for receiving messages, while
SendFn: Fn(&T) + Clone + 'static
is used for sending. Both use the same generic type "T".
My problem could be sidestepped by having the send function use a different generic type than the message signal.
Another way to sidestep the problem would be using the same type for sending and receiving. I'd prefer avoiding that since my two enums really have little in common.
I'll work on supporting different types for sending and receiving
I also encountered this, I need WebSocket access to get buffered_amount
for which there is no signal in use_websocket. I think we can return a signal to websocket or StoredValue itself.
This is now fixed and released in version 0.13.4
Thanks!
I just tested the update and it works perfectly. Thank you :)
I'm trying to get access to the websocket so I can send and receive different kinds of messages. Seeing that UseWebSocketReturn contains
Option<WebSocket>
, I assumed that was the way to access the raw websocket.Expected behavior
I expected
ws
to beSome(WebSocket)
, so the above code should print "websocket: true".Actual behavior
Instead, "websocket: false" is printed. In my browser's Network tab I can see that a connection request is made and the server responds with
101 Switching Protocols
. So websockets should be supported.Possible fix
I had a look at the source code for
use_websocket_with_options
(which is called byuse_websocket
). It returnsbefore ws_ref gets set. ws_ref gets set later, when
open()
is called inside a create_effect or by the user.Maybe ws_ref could be returned directly, without calling "get_value()"? That would change
pub ws: Option<WebSocket>,
topub ws: StoredValue<Option<WebSocket>>,
in UseWebSocketReturn.ws.get_value()
should then return the websocket once it has been opened for the first time. I have not tested if this works, yet!