How to create a websocket connection and always read the data from the server to render when the data changes? #3315
Unanswered
callmeitachi
asked this question in
Q&A
Replies: 1 comment
-
You should use triple-backticks (```) around code on GitHub so that it is formatted as code. Right now, it's very hard to read. I see a place where you set a signal. I don't see anywhere where you read from the signal. Are you reading it correctly/reactively? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My English is not very good,and the questions I want to ask are like the title narrative,may be wrong.
I have the following code:
...
let (data,set_data)=signal(Vec::::new());
let (mut write, mut read) = ws.split();
spawn_local(async move {
write.send(Message::Text(String::from("test"))).await.unwrap();
write.send(Message::Text(String::from("test 2"))).await.unwrap();
});
spawn_local(async move {
while let Some(Ok(msg)) = read.next().await {
log!("got it!");
if let Text(data)=msg {
let json_data:Value=serde_json::from_str(data.as_str()).unwrap();
let json_data_data=json_data["data"].as_array().unwrap();
let mut new_data=Vec::new();
for i in json_data_data {
let alarm_info=AlarmData {
alarm_id:i["alarm_id"].to_string(),
prob_cause:i["prob_cause"].to_string(),
system_seg_name:i["system_seg_name"].to_string(),
..Default::default()
};
new_data.push(alarm_info);
}
set_data.set(new_data);
}
}
log!("WebSocket Closed");
});
..........
It only renderer once at the beginning and did not render when the server is read and changes.
Beta Was this translation helpful? Give feedback.
All reactions