chorus/examples/gateway_observers.rs

80 lines
2.6 KiB
Rust
Raw Permalink Normal View History

2024-01-30 17:19:34 +01:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This example showcase how to properly use gateway observers.
//
// To properly run it, you will need to change the token below.
const TOKEN: &str = "";
/// Find the gateway websocket url of the server we want to connect to
const GATEWAY_URL: &str = "wss://gateway.old.server.spacebar.chat/";
2023-07-11 19:20:27 +02:00
use async_trait::async_trait;
2023-11-19 19:12:29 +01:00
use chorus::gateway::Gateway;
2023-05-28 17:41:50 +02:00
use chorus::{
self,
gateway::Observer,
2023-05-28 17:41:50 +02:00
types::{GatewayIdentifyPayload, GatewayReady},
};
2023-06-19 10:27:32 +02:00
use std::{sync::Arc, time::Duration};
use tokio::{self};
2023-05-28 17:41:50 +02:00
2024-01-19 15:14:50 +01:00
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
2024-01-19 15:14:50 +01:00
#[cfg(target_arch = "wasm32")]
2024-01-19 15:31:40 +01:00
use wasmtimer::tokio::sleep;
2024-01-19 15:14:50 +01:00
2023-05-28 17:41:50 +02:00
// 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<GatewayReady>.
// 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
2023-07-11 19:20:27 +02:00
#[async_trait]
2023-05-28 17:41:50 +02:00
impl Observer<GatewayReady> for ExampleObserver {
// After we subscribe to an event this function is called every time we receive it
2023-07-11 19:20:27 +02:00
async fn update(&self, _data: &GatewayReady) {
2023-05-28 17:41:50 +02:00
println!("Observed Ready!");
}
}
#[tokio::main(flavor = "current_thread")]
2023-05-28 17:41:50 +02:00
async fn main() {
let gateway_websocket_url = GATEWAY_URL.to_string();
2023-05-28 17:41:50 +02:00
// Initiate the gateway connection
let gateway = Gateway::spawn(gateway_websocket_url).await.unwrap();
2023-05-28 17:41:50 +02:00
// Create an instance of our observer
let observer = ExampleObserver {};
2023-06-20 14:42:50 +02:00
// Share ownership of the observer with the gateway
let shared_observer = Arc::new(observer);
2023-05-28 17:41:50 +02:00
// 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
2023-06-20 14:42:50 +02:00
.subscribe(shared_observer);
2023-05-28 17:41:50 +02:00
// Authenticate so we will receive any events
let token = TOKEN.to_string();
2023-05-28 17:41:50 +02:00
let mut identify = GatewayIdentifyPayload::common();
identify.token = token;
gateway.send_identify(identify).await;
2024-01-19 15:14:50 +01:00
2023-05-28 17:41:50 +02:00
// Do something on the main thread so we don't quit
2023-06-19 10:27:32 +02:00
loop {
sleep(Duration::from_secs(3600)).await;
2023-06-19 10:27:32 +02:00
}
2023-05-28 17:41:50 +02:00
}