|
| 1 | +use { |
| 2 | + relay_client::{ |
| 3 | + Client, |
| 4 | + CloseFrame, |
| 5 | + ConnectionHandler, |
| 6 | + ConnectionOptions, |
| 7 | + Error, |
| 8 | + PublishedMessage, |
| 9 | + }, |
| 10 | + relay_rpc::{ |
| 11 | + auth::{ed25519_dalek::Keypair, rand, AuthToken}, |
| 12 | + domain::{AuthSubject, Topic}, |
| 13 | + }, |
| 14 | + std::{sync::Arc, time::Duration}, |
| 15 | + structopt::StructOpt, |
| 16 | +}; |
| 17 | + |
| 18 | +#[derive(StructOpt)] |
| 19 | +struct Args { |
| 20 | + /// Specify WebSocket address. |
| 21 | + #[structopt(short, long, default_value = "wss://relay.walletconnect.com")] |
| 22 | + address: String, |
| 23 | + |
| 24 | + /// Specify WalletConnect project ID. |
| 25 | + #[structopt(short, long, default_value = "3cbaa32f8fbf3cdcc87d27ca1fa68069")] |
| 26 | + project_id: String, |
| 27 | +} |
| 28 | + |
| 29 | +struct Handler { |
| 30 | + name: &'static str, |
| 31 | +} |
| 32 | + |
| 33 | +impl Handler { |
| 34 | + fn new(name: &'static str) -> Self { |
| 35 | + Self { name } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl ConnectionHandler for Handler { |
| 40 | + fn connected(&mut self) { |
| 41 | + println!("[{}] connection open", self.name); |
| 42 | + } |
| 43 | + |
| 44 | + fn disconnected(&mut self, frame: Option<CloseFrame<'static>>) { |
| 45 | + println!("[{}] connection closed: frame={frame:?}", self.name); |
| 46 | + } |
| 47 | + |
| 48 | + fn message_received(&mut self, message: PublishedMessage) { |
| 49 | + println!( |
| 50 | + "[{}] inbound message: topic={} message={}", |
| 51 | + self.name, message.topic, message.message |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + fn inbound_error(&mut self, error: Error) { |
| 56 | + println!("[{}] inbound error: {error}", self.name); |
| 57 | + } |
| 58 | + |
| 59 | + fn outbound_error(&mut self, error: Error) { |
| 60 | + println!("[{}] outbound error: {error}", self.name); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn create_conn_opts(address: &str, project_id: &str) -> ConnectionOptions { |
| 65 | + let key = Keypair::generate(&mut rand::thread_rng()); |
| 66 | + |
| 67 | + let auth = AuthToken::new(AuthSubject::generate()) |
| 68 | + .aud(address) |
| 69 | + .ttl(Duration::from_secs(60 * 60)) |
| 70 | + .as_jwt(&key) |
| 71 | + .unwrap(); |
| 72 | + |
| 73 | + ConnectionOptions::new(project_id, auth).with_address(address) |
| 74 | +} |
| 75 | + |
| 76 | +#[tokio::main] |
| 77 | +async fn main() -> anyhow::Result<()> { |
| 78 | + let args = Args::from_args(); |
| 79 | + |
| 80 | + let client1 = Client::new(Handler::new("client1")); |
| 81 | + client1 |
| 82 | + .connect(create_conn_opts(&args.address, &args.project_id)) |
| 83 | + .await?; |
| 84 | + |
| 85 | + let client2 = Client::new(Handler::new("client2")); |
| 86 | + client2 |
| 87 | + .connect(create_conn_opts(&args.address, &args.project_id)) |
| 88 | + .await?; |
| 89 | + |
| 90 | + let topic = Topic::generate(); |
| 91 | + |
| 92 | + let subscription_id = client1.subscribe(topic.clone()).await?; |
| 93 | + println!("[client1] subscribed: topic={topic} subscription_id={subscription_id}"); |
| 94 | + |
| 95 | + client2 |
| 96 | + .publish( |
| 97 | + topic.clone(), |
| 98 | + Arc::from("Hello WalletConnect!"), |
| 99 | + 0, |
| 100 | + Duration::from_secs(60), |
| 101 | + ) |
| 102 | + .await?; |
| 103 | + |
| 104 | + println!("[client2] published message with topic: {topic}"); |
| 105 | + |
| 106 | + drop(client1); |
| 107 | + drop(client2); |
| 108 | + |
| 109 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 110 | + |
| 111 | + println!("clients disconnected"); |
| 112 | + |
| 113 | + Ok(()) |
| 114 | +} |
0 commit comments