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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
//! A rich Spectacles Gateway client for Rust. //! //! ## Features //! - Asynchronous websocket message handling. //! - Zero-Downtime shard spawning. //! - Integrates seamlessly with the spectacles-brokers package. //! //! ## About //! This crate allows you to interact with the Discord gateway. Please refer to the [Discord Gateway Docs](https://discordapp.com/developers/docs/topics/gateway) for more background on how to use this message. //! //! ## Example //! ```rust,norun //! #[macro_use] extern crate log; //! use std::env::var; //! use tokio::runtime::current_thread; //! use spectacles_gateway::{ShardManager, ShardStrategy, ManagerOptions, EventHandler, Shard}; //! use spectacles_model::gateway::ReceivePacket; //! use futures::future::Future; //! //! fn main() { //! env_logger::init(); //! let token = var("DISCORD_TOKEN").expect("No Discord Token was provided."); //! // tokio.run() boostraps our Tokio application. //! current_thread:run({ //! // calling new() here return a new instance of the shard manager. //! ShardManager::new(token, ManagerOptions { //! strategy: ShardStrategy::Recommended, //! handler: Handler //! }) //! .and_then(|mut manager| manager.spawn()) // Begins spawning of shards. //! .map_err(|err| error!("An error occured while processing shards: {:?}", err)) //! }); //! } //! /// Here we define our Handler struct, which we implement the EventHandler trait for. //! /// The on_packet() trait method will be called when a packet is received from the Discord gateway. //! struct Handler; //! impl EventHandler for Handler { //! fn on_packet(&self, shard: &Shard, pkt: ReceivePacket) { //! println!("Received Gateway Packet from Shard {:?} - {:?}", shard.info, pkt); //! // Do other things with message, such as sending it to a message broker. //! } //! } //! ``` #[macro_use] extern crate log; #[warn(rust_2018_idioms)] pub use errors::{Error, Result}; pub use manager::*; pub use shard::Shard; use spectacles_model::gateway::ReceivePacket; mod manager; mod shard; mod constants; mod errors; mod queue; /// Options for Creating a new shard manager. pub struct ManagerOptions<H> where H: EventHandler + Send + Sync { /// The struct which contains callbacks for websocket packets. pub handler: H, /// The strategy in which you want to spawn shards. pub strategy: ShardStrategy, } /// The event handler trait, useful for receiving events from the websocket. pub trait EventHandler { /// Executed whenever a raw packet is received. fn on_packet(&self, _shard: &mut Shard, _pkt: ReceivePacket); }