From d9b1c382b70e0226da22402697b070048775f623 Mon Sep 17 00:00:00 2001 From: kozabrada123 <“kozabrada123@users.noreply.github.com”> Date: Sun, 28 May 2023 17:41:50 +0200 Subject: [PATCH] Add simple gateway examples --- examples/gateway_observers.rs | 58 +++++++++++++++++++++++++++++++++++ examples/gateway_simple.rs | 31 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 examples/gateway_observers.rs create mode 100644 examples/gateway_simple.rs diff --git a/examples/gateway_observers.rs b/examples/gateway_observers.rs new file mode 100644 index 0000000..e4c78e1 --- /dev/null +++ b/examples/gateway_observers.rs @@ -0,0 +1,58 @@ +use chorus::{ + self, + gateway::{Gateway, Observer}, + types::{GatewayIdentifyPayload, GatewayReady}, +}; +use std::sync::Arc; +use tokio::{self, sync::Mutex}; + +// This example creates a simple gateway connection and a basic observer struct + +// Due to certain limitations all observers must impl debug +#[derive(Debug)] +pub struct ExampleObserver {} + +// This struct can observe GatewayReady events when subscribed, because it implements the trait Observer. +// The Observer trait can be implemented for a struct for a given websocketevent to handle observing it +// One struct can be an observer of multiple websocketevents, if needed +impl Observer for ExampleObserver { + // After we subscribe to an event this function is called every time we receive it + fn update(&self, data: &GatewayReady) { + println!("Observed Ready!"); + } +} + +#[tokio::main] +async fn main() { + // Find the gateway websocket url of the server we want to connect to + let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string(); + + // Initiate the gateway connection + let gateway = Gateway::new(websocket_url_spacebar).await.unwrap(); + + // Create an instance of our observer + let observer = ExampleObserver {}; + + // Because observers have to reside in between the main and gateway thread, (they have to be shared between both) we need to put them in an Arc + let shared_observer = Arc::new(Mutex::new(observer)); + + // Subscribe our observer to the Ready event on this gateway + // From now on observer.update(data) will be called every time we receive the Ready event + gateway + .events + .lock() + .await + .session + .ready + .subscribe(shared_observer) + .unwrap(); + + // Authenticate so we will receive any events + let token = "SecretToken".to_string(); + let mut identify = GatewayIdentifyPayload::common(); + identify.token = token; + gateway.send_identify(identify).await; + + // Do something on the main thread so we don't quit + loop {} +} diff --git a/examples/gateway_simple.rs b/examples/gateway_simple.rs new file mode 100644 index 0000000..19de9bb --- /dev/null +++ b/examples/gateway_simple.rs @@ -0,0 +1,31 @@ +use chorus::{self, gateway::Gateway, types::GatewayIdentifyPayload}; +use tokio; + +/// This example creates a simple gateway connection and a session with an Identify event +#[tokio::main] +async fn main() { + // Find the gateway websocket url of the server we want to connect to + let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string(); + + // Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another + let gateway = Gateway::new(websocket_url_spacebar).await.unwrap(); + + // At this point, we are connected to the server and are sending heartbeats, however we still haven't authenticated + + // Get a token for an account on the server + let token = "SecretToken".to_string(); + + // Create an identify event + // An Identify event is how the server authenticates us and gets info about our os and browser, along with our intents / capabilities + // (Chorus never sends real telemetry data about your system to servers, always just using the most common option or a custom set one) + // By default the capabilities requests all the data of a regular client + let mut identify = GatewayIdentifyPayload::common(); + + identify.token = token; + + // Send off the event + gateway.send_identify(identify).await; + + // Do something on the main thread so we don't quit + loop {} +}