Implement wasm Backend

This commit is contained in:
bitfl0wer 2023-11-19 22:23:24 +01:00
parent 8f0d8813eb
commit 1203e20358
2 changed files with 17 additions and 12 deletions

View File

@ -2,6 +2,7 @@
pub mod tungstenite; pub mod tungstenite;
#[cfg(all(not(target_arch = "wasm32"), feature = "client"))] #[cfg(all(not(target_arch = "wasm32"), feature = "client"))]
pub use tungstenite::*; pub use tungstenite::*;
#[cfg(all(target_arch = "wasm32", feature = "client"))] #[cfg(all(target_arch = "wasm32", feature = "client"))]
pub mod wasm; pub mod wasm;
#[cfg(all(target_arch = "wasm32", feature = "client"))] #[cfg(all(target_arch = "wasm32", feature = "client"))]

View File

@ -12,22 +12,19 @@ use crate::gateway::GatewayMessage;
pub struct WasmBackend; pub struct WasmBackend;
// These could be made into inherent associated types when that's stabilized // These could be made into inherent associated types when that's stabilized
pub type WasmSink = SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, tungstenite::Message>; pub type WasmSink = SplitSink<WsStream, WsMessage>;
pub type WasmStream = SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>; pub type WasmStream = SplitStream<WsStream>;
impl WasmBackend { impl WasmBackend {
pub async fn connect( pub async fn connect(
websocket_url: &str, websocket_url: &str,
) -> Result<(WasmSink, WasmStream), crate::errors::GatewayError> { ) -> Result<(WasmSink, WasmStream), crate::errors::GatewayError> {
let (websocket_stream, _) = match WsMeta::connect(); let (_, websocket_stream) = match WsMeta::connect(websocket_url, None).await {
{ Ok(stream) => Ok(stream),
Ok(websocket_stream) => websocket_stream, Err(e) => Err(GatewayError::CannotConnect {
Err(e) => { error: e.to_string(),
return Err(GatewayError::CannotConnect { }),
error: e.to_string(), }?;
})
}
};
Ok(websocket_stream.split()) Ok(websocket_stream.split())
} }
@ -41,6 +38,13 @@ impl From<GatewayMessage> for WsMessage {
impl From<WsMessage> for GatewayMessage { impl From<WsMessage> for GatewayMessage {
fn from(value: WsMessage) -> Self { fn from(value: WsMessage) -> Self {
Self(value.to_string()) match value {
WsMessage::Text(text) => Self(text),
WsMessage::Binary(bin) => {
let mut text = String::new();
let _ = bin.iter().map(|v| text.push_str(&v.to_string()));
Self(text)
}
}
} }
} }