chorus/examples/gateway_simple.rs

60 lines
2.3 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/.
Primitive voice implementation (feature/voice) (#457) * Add Webrtc Identify & Ready * Add more webrtc typings * Attempt an untested voice gateway implementation * fmt * Merge with main * Same allow as for voice as normal gateway * Test error observer * Minor updates * More derives * Even more derives * Small types update * e * Minor doc fixes * Modernise voice gateway * Add default impl for voicegatewayerror * Make voice event fields pub * Event updates via the scientific method * ?? * Fix bad request in voice gateway init * Voice gateway updates * Fix error failing to 'deserialize' properly * Update voice identify * Clarify FIXME related to #430 * Update to v7 * Create seperate voice_gateway.rs and voice_udp.rs * Restructure voice to new module * fix: deserialization error in speaking bitflags * feat: kinda janky ip discovery impl * feat: return ip discovery data + minor update * feat: packet parsing! * fix: voice works again * feat: add voice_media_sink_wants (comitting uncommited changes to merge) * chore: rename events/webrtc to events/voice_gateway * Add UdpHandle * chore: clippy + other misc updates * fix: attempt to fix failing wasm build * chore: yes clippy, that is indeed an unneeded return statement * feat: add VoiceData struct * feat: add VoiceData reference to UdpHandler * feat: decryption? * chore: formatting * feat: add ssrc definition (op 12) * feat: add untested sending & asbtract nonce generation * feat: Public api! (sorta) * small updates * feat: add sequence number * chore: yes * feat: merge VoiceHandler into official development * chore: yes clippy, you are special * fix: duplicated gateway events * feat: first try at vgw wasm compat * fix: blunder * fix: gateway connect using wrong url * fix: properly using encrypted data, bad practice for buffer creation * chore: split voice udp * feat: udp error handling, create udp/backends * fix: its the same * chore: clarify UDP on WASM * api: split voice gateway and udp features, test for voice gateway in WASM * feat: new encryption modes, minor code quality * docs: document voice encryption modes * chore: unused imports * chore: update getrandom version to match wasm version * chore: update on packet size FIXME * drop buf asap * Okay can't do that actually * tests: add nonce test * normal tests work? * docs: fix doc warning, fix incorrect refrences to 'webrtc' * chore: json isn't a doc test * tests: better gateway auth test * testing tests * update voice heartbeat, fix the new test issue * committed too much * fix: unused import * fix: use ip discovery address as string, not as Vec<u8> * chore: less obnoxious logging * chore: better unimplemented voice modes handling * chore: remove unused variable * chore: use matches macro * add voice examples, make gateway ones clearer * rename voice example * chore: remove unused VoiceHandler * fix: implement gateway Reconnect and InvalidSession * Typo Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * Fix a bunch of typos Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * fix: error handling while loading native certs * fix: guh * use be for nonce bytes * fix: refactor gw and vgw closures * remove outdated docs --------- Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com>
2024-04-16 17:18:21 +02:00
// This example showcases how to initiate a gateway connection manually
// (e. g. not through ChorusUser or Instance)
Primitive voice implementation (feature/voice) (#457) * Add Webrtc Identify & Ready * Add more webrtc typings * Attempt an untested voice gateway implementation * fmt * Merge with main * Same allow as for voice as normal gateway * Test error observer * Minor updates * More derives * Even more derives * Small types update * e * Minor doc fixes * Modernise voice gateway * Add default impl for voicegatewayerror * Make voice event fields pub * Event updates via the scientific method * ?? * Fix bad request in voice gateway init * Voice gateway updates * Fix error failing to 'deserialize' properly * Update voice identify * Clarify FIXME related to #430 * Update to v7 * Create seperate voice_gateway.rs and voice_udp.rs * Restructure voice to new module * fix: deserialization error in speaking bitflags * feat: kinda janky ip discovery impl * feat: return ip discovery data + minor update * feat: packet parsing! * fix: voice works again * feat: add voice_media_sink_wants (comitting uncommited changes to merge) * chore: rename events/webrtc to events/voice_gateway * Add UdpHandle * chore: clippy + other misc updates * fix: attempt to fix failing wasm build * chore: yes clippy, that is indeed an unneeded return statement * feat: add VoiceData struct * feat: add VoiceData reference to UdpHandler * feat: decryption? * chore: formatting * feat: add ssrc definition (op 12) * feat: add untested sending & asbtract nonce generation * feat: Public api! (sorta) * small updates * feat: add sequence number * chore: yes * feat: merge VoiceHandler into official development * chore: yes clippy, you are special * fix: duplicated gateway events * feat: first try at vgw wasm compat * fix: blunder * fix: gateway connect using wrong url * fix: properly using encrypted data, bad practice for buffer creation * chore: split voice udp * feat: udp error handling, create udp/backends * fix: its the same * chore: clarify UDP on WASM * api: split voice gateway and udp features, test for voice gateway in WASM * feat: new encryption modes, minor code quality * docs: document voice encryption modes * chore: unused imports * chore: update getrandom version to match wasm version * chore: update on packet size FIXME * drop buf asap * Okay can't do that actually * tests: add nonce test * normal tests work? * docs: fix doc warning, fix incorrect refrences to 'webrtc' * chore: json isn't a doc test * tests: better gateway auth test * testing tests * update voice heartbeat, fix the new test issue * committed too much * fix: unused import * fix: use ip discovery address as string, not as Vec<u8> * chore: less obnoxious logging * chore: better unimplemented voice modes handling * chore: remove unused variable * chore: use matches macro * add voice examples, make gateway ones clearer * rename voice example * chore: remove unused VoiceHandler * fix: implement gateway Reconnect and InvalidSession * Typo Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * Fix a bunch of typos Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * fix: error handling while loading native certs * fix: guh * use be for nonce bytes * fix: refactor gw and vgw closures * remove outdated docs --------- Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com>
2024-04-16 17:18:21 +02:00
//
// To properly run it, you will need to modify 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-06-19 10:27:32 +02:00
use std::time::Duration;
use chorus::gateway::{Gateway, GatewayOptions};
2023-11-19 19:12:29 +01:00
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() {
Primitive voice implementation (feature/voice) (#457) * Add Webrtc Identify & Ready * Add more webrtc typings * Attempt an untested voice gateway implementation * fmt * Merge with main * Same allow as for voice as normal gateway * Test error observer * Minor updates * More derives * Even more derives * Small types update * e * Minor doc fixes * Modernise voice gateway * Add default impl for voicegatewayerror * Make voice event fields pub * Event updates via the scientific method * ?? * Fix bad request in voice gateway init * Voice gateway updates * Fix error failing to 'deserialize' properly * Update voice identify * Clarify FIXME related to #430 * Update to v7 * Create seperate voice_gateway.rs and voice_udp.rs * Restructure voice to new module * fix: deserialization error in speaking bitflags * feat: kinda janky ip discovery impl * feat: return ip discovery data + minor update * feat: packet parsing! * fix: voice works again * feat: add voice_media_sink_wants (comitting uncommited changes to merge) * chore: rename events/webrtc to events/voice_gateway * Add UdpHandle * chore: clippy + other misc updates * fix: attempt to fix failing wasm build * chore: yes clippy, that is indeed an unneeded return statement * feat: add VoiceData struct * feat: add VoiceData reference to UdpHandler * feat: decryption? * chore: formatting * feat: add ssrc definition (op 12) * feat: add untested sending & asbtract nonce generation * feat: Public api! (sorta) * small updates * feat: add sequence number * chore: yes * feat: merge VoiceHandler into official development * chore: yes clippy, you are special * fix: duplicated gateway events * feat: first try at vgw wasm compat * fix: blunder * fix: gateway connect using wrong url * fix: properly using encrypted data, bad practice for buffer creation * chore: split voice udp * feat: udp error handling, create udp/backends * fix: its the same * chore: clarify UDP on WASM * api: split voice gateway and udp features, test for voice gateway in WASM * feat: new encryption modes, minor code quality * docs: document voice encryption modes * chore: unused imports * chore: update getrandom version to match wasm version * chore: update on packet size FIXME * drop buf asap * Okay can't do that actually * tests: add nonce test * normal tests work? * docs: fix doc warning, fix incorrect refrences to 'webrtc' * chore: json isn't a doc test * tests: better gateway auth test * testing tests * update voice heartbeat, fix the new test issue * committed too much * fix: unused import * fix: use ip discovery address as string, not as Vec<u8> * chore: less obnoxious logging * chore: better unimplemented voice modes handling * chore: remove unused variable * chore: use matches macro * add voice examples, make gateway ones clearer * rename voice example * chore: remove unused VoiceHandler * fix: implement gateway Reconnect and InvalidSession * Typo Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * Fix a bunch of typos Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * fix: error handling while loading native certs * fix: guh * use be for nonce bytes * fix: refactor gw and vgw closures * remove outdated docs --------- Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com>
2024-04-16 17:18:21 +02:00
let gateway_websocket_url = GATEWAY_URL.to_string();
// These options specify the encoding format, compression, etc
//
// For most cases the defaults should work, though some implementations
// might only support some formats or not support compression
let options = GatewayOptions::default();
2023-05-28 17:41:50 +02:00
// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
let gateway = Gateway::spawn(gateway_websocket_url, options).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
Primitive voice implementation (feature/voice) (#457) * Add Webrtc Identify & Ready * Add more webrtc typings * Attempt an untested voice gateway implementation * fmt * Merge with main * Same allow as for voice as normal gateway * Test error observer * Minor updates * More derives * Even more derives * Small types update * e * Minor doc fixes * Modernise voice gateway * Add default impl for voicegatewayerror * Make voice event fields pub * Event updates via the scientific method * ?? * Fix bad request in voice gateway init * Voice gateway updates * Fix error failing to 'deserialize' properly * Update voice identify * Clarify FIXME related to #430 * Update to v7 * Create seperate voice_gateway.rs and voice_udp.rs * Restructure voice to new module * fix: deserialization error in speaking bitflags * feat: kinda janky ip discovery impl * feat: return ip discovery data + minor update * feat: packet parsing! * fix: voice works again * feat: add voice_media_sink_wants (comitting uncommited changes to merge) * chore: rename events/webrtc to events/voice_gateway * Add UdpHandle * chore: clippy + other misc updates * fix: attempt to fix failing wasm build * chore: yes clippy, that is indeed an unneeded return statement * feat: add VoiceData struct * feat: add VoiceData reference to UdpHandler * feat: decryption? * chore: formatting * feat: add ssrc definition (op 12) * feat: add untested sending & asbtract nonce generation * feat: Public api! (sorta) * small updates * feat: add sequence number * chore: yes * feat: merge VoiceHandler into official development * chore: yes clippy, you are special * fix: duplicated gateway events * feat: first try at vgw wasm compat * fix: blunder * fix: gateway connect using wrong url * fix: properly using encrypted data, bad practice for buffer creation * chore: split voice udp * feat: udp error handling, create udp/backends * fix: its the same * chore: clarify UDP on WASM * api: split voice gateway and udp features, test for voice gateway in WASM * feat: new encryption modes, minor code quality * docs: document voice encryption modes * chore: unused imports * chore: update getrandom version to match wasm version * chore: update on packet size FIXME * drop buf asap * Okay can't do that actually * tests: add nonce test * normal tests work? * docs: fix doc warning, fix incorrect refrences to 'webrtc' * chore: json isn't a doc test * tests: better gateway auth test * testing tests * update voice heartbeat, fix the new test issue * committed too much * fix: unused import * fix: use ip discovery address as string, not as Vec<u8> * chore: less obnoxious logging * chore: better unimplemented voice modes handling * chore: remove unused variable * chore: use matches macro * add voice examples, make gateway ones clearer * rename voice example * chore: remove unused VoiceHandler * fix: implement gateway Reconnect and InvalidSession * Typo Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * Fix a bunch of typos Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com> * fix: error handling while loading native certs * fix: guh * use be for nonce bytes * fix: refactor gw and vgw closures * remove outdated docs --------- Co-authored-by: Flori <39242991+bitfl0wer@users.noreply.github.com>
2024-04-16 17:18:21 +02:00
let token = TOKEN.to_string();
2023-05-28 17:41:50 +02:00
// 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
}