-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathwebsocket.rs
More file actions
43 lines (37 loc) · 1.42 KB
/
websocket.rs
File metadata and controls
43 lines (37 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::time::Duration;
use starknet::core::types::ConfirmedBlockId;
use starknet_tokio_tungstenite::{NewHeadsUpdate, TungsteniteStream};
#[tokio::main]
async fn main() {
let stream = TungsteniteStream::connect("ws://localhost:9545/rpc/v0_9", Duration::from_secs(5))
.await
.expect("WebSocket connection failed");
{
let mut new_heads_subscription = stream
.subscribe_new_heads(ConfirmedBlockId::Latest)
.await
.unwrap();
for _ in 0..5 {
match new_heads_subscription.recv().await {
Ok(NewHeadsUpdate::NewHeader(head)) => {
println!(
"Received new chain head #{}: {:#064x}",
head.block_number, head.block_hash
);
}
Ok(NewHeadsUpdate::Reorg(reorg)) => {
println!(
"Encountered reorg of segment: #{} -> #{}",
reorg.starting_block_number, reorg.ending_block_number
);
}
Err(err) => {
eprintln!("Failed to receieve update: {err}");
}
}
}
// `new_heads_subscription` goes out of scope. A `starknet_unsubscribe` request is
// automatically sent on drop.
}
// `stream` goes out of scope. WebSocket connection is automatically closed.
}