add wasm impl for RawMessage

This commit is contained in:
kozabrada123 2024-06-16 08:47:38 +02:00
parent 2887794b0e
commit f3ee3a8d8a
6 changed files with 52 additions and 11 deletions

View File

@ -3,6 +3,8 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This example showcase how to properly use gateway observers.
// (This assumes you have a manually created gateway, if you created
// a ChorusUser by e.g. logging in, you can access the gateway with user.gateway)
//
// To properly run it, you will need to change the token below.
@ -12,7 +14,7 @@ const TOKEN: &str = "";
const GATEWAY_URL: &str = "wss://gateway.old.server.spacebar.chat/";
use async_trait::async_trait;
use chorus::gateway::Gateway;
use chorus::gateway::{Gateway, GatewayOptions};
use chorus::{
self,
gateway::Observer,
@ -47,8 +49,14 @@ impl Observer<GatewayReady> for ExampleObserver {
async fn main() {
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();
// Initiate the gateway connection
let gateway = Gateway::spawn(gateway_websocket_url).await.unwrap();
let gateway = Gateway::spawn(gateway_websocket_url, options).await.unwrap();
// Create an instance of our observer
let observer = ExampleObserver {};

View File

@ -3,7 +3,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This example showcases how to initiate a gateway connection manually
// (e. g. not through ChorusUser)
// (e. g. not through ChorusUser or Instance)
//
// To properly run it, you will need to modify the token below.
@ -14,7 +14,7 @@ const GATEWAY_URL: &str = "wss://gateway.old.server.spacebar.chat/";
use std::time::Duration;
use chorus::gateway::Gateway;
use chorus::gateway::{Gateway, GatewayOptions};
use chorus::{self, types::GatewayIdentifyPayload};
#[cfg(not(target_arch = "wasm32"))]
@ -26,9 +26,15 @@ use wasmtimer::tokio::sleep;
#[tokio::main(flavor = "current_thread")]
async fn main() {
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();
// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
let gateway = Gateway::spawn(gateway_websocket_url).await.unwrap();
let gateway = Gateway::spawn(gateway_websocket_url, options).await.unwrap();
// At this point, we are connected to the server and are sending heartbeats, however we still haven't authenticated

View File

@ -41,6 +41,15 @@ impl GatewayOptions {
let already_has_parameters = url.contains("?") && url.contains("=");
if !already_has_parameters {
// Insure it ends in a /, so we don't get a 400 error
if !url.ends_with('/') {
url.push('/');
}
// Lets hope that if it already has parameters the person knew to add '/'
}
for index in 0..parameters.len() {
if index == 0 && !already_has_parameters {
url = format!("{}?{}", url, parameters[index]);

View File

@ -23,3 +23,21 @@ impl From<WsMessage> for VoiceGatewayMessage {
}
}
}
impl From<RawGatewayMessage> for WsMessage {
fn from(message: RawGatewayMessage) -> Self {
match message {
RawGatewayMessage::Text(text) => tungstenite::Message::Text(text),
RawGatewayMessage::Bytes(bytes) => tungstenite::Message::Binary(bytes),
}
}
}
impl From<WsMessage> for RawGatewayMessage {
fn from(value: WsMessage) -> Self {
match value {
WsMessage::Binary(bytes) => RawGatewayMessage::Bytes(bytes),
WsMessage::Text(text) => RawGatewayMessage::Text(text),
}
}
}

View File

@ -4,7 +4,7 @@
use std::str::FromStr;
use chorus::gateway::Gateway;
use chorus::gateway::{Gateway, GatewayOptions};
use chorus::types::IntoShared;
use chorus::{
instance::{ChorusUser, Instance},
@ -50,7 +50,7 @@ impl TestBundle {
limits: self.user.limits.clone(),
settings: self.user.settings.clone(),
object: self.user.object.clone(),
gateway: Gateway::spawn(self.instance.urls.wss.clone())
gateway: Gateway::spawn(self.instance.urls.wss.clone(), GatewayOptions::default())
.await
.unwrap(),
}
@ -119,7 +119,7 @@ pub(crate) async fn setup() -> TestBundle {
let urls = UrlBundle::new(
"http://localhost:3001/api".to_string(),
"http://localhost:3001/api".to_string(),
"ws://localhost:3001".to_string(),
"ws://localhost:3001/".to_string(),
"http://localhost:3001".to_string(),
);
TestBundle {

View File

@ -30,7 +30,7 @@ use wasmtimer::tokio::sleep;
async fn test_gateway_establish() {
let bundle = common::setup().await;
let _: GatewayHandle = Gateway::spawn(bundle.urls.wss.clone()).await.unwrap();
let _: GatewayHandle = Gateway::spawn(bundle.urls.wss.clone(), GatewayOptions::default()).await.unwrap();
common::teardown(bundle).await
}
@ -52,7 +52,7 @@ impl Observer<GatewayReady> for GatewayReadyObserver {
async fn test_gateway_authenticate() {
let bundle = common::setup().await;
let gateway: GatewayHandle = Gateway::spawn(bundle.urls.wss.clone()).await.unwrap();
let gateway: GatewayHandle = Gateway::spawn(bundle.urls.wss.clone(), GatewayOptions::default()).await.unwrap();
let (ready_send, mut ready_receive) = tokio::sync::mpsc::channel(1);
@ -79,7 +79,7 @@ async fn test_gateway_authenticate() {
println!("Timed out waiting for event, failing..");
assert!(false);
}
// Sucess, we have received it
// Success, we have received it
Some(_) = ready_receive.recv() => {}
};