chorus/examples/gateway_simple.rs

45 lines
1.8 KiB
Rust
Raw 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/.
2023-06-19 10:27:32 +02:00
use std::time::Duration;
2023-11-19 19:12:29 +01:00
use chorus::gateway::Gateway;
use chorus::{self, types::GatewayIdentifyPayload};
2023-06-11 13:54:08 +02:00
2024-01-19 15:21:53 +01:00
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
2024-01-19 15:21:53 +01:00
#[cfg(target_arch = "wasm32")]
2024-01-19 15:31:40 +01:00
use wasmtimer::tokio::sleep;
2024-01-19 15:21:53 +01:00
2023-05-28 17:41:50 +02:00
/// This example creates a simple gateway connection and a session with an Identify event
#[tokio::main(flavor = "current_thread")]
2023-05-28 17:41:50 +02:00
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
2024-01-19 15:21:53 +01:00
let gateway = Gateway::spawn(websocket_url_spacebar).await.unwrap();
2023-05-28 17:41:50 +02:00
// 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
2024-01-21 17:07:30 +01:00
gateway.send_identify(identify).await;
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
}