start implementing wasm gateway
This commit is contained in:
parent
5785d73eea
commit
9d88f50bf7
|
@ -205,6 +205,7 @@ dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"log",
|
"log",
|
||||||
"native-tls",
|
"native-tls",
|
||||||
|
"pharos",
|
||||||
"poem",
|
"poem",
|
||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
|
|
|
@ -69,6 +69,8 @@ hostname = "0.3.1"
|
||||||
getrandom = { version = "0.2.11", features = ["js"] }
|
getrandom = { version = "0.2.11", features = ["js"] }
|
||||||
tokio-tungstenite = { version = "0.20.1", default-features = false }
|
tokio-tungstenite = { version = "0.20.1", default-features = false }
|
||||||
ws_stream_wasm = "0.7.4"
|
ws_stream_wasm = "0.7.4"
|
||||||
|
pharos = "0.5.3"
|
||||||
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
|
|
@ -2,6 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use super::events::Events;
|
use super::events::Events;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use pharos::*;
|
||||||
use ws_stream_wasm::*;
|
use ws_stream_wasm::*;
|
||||||
|
|
||||||
use crate::types::{self, WebSocketEvent};
|
use crate::types::{self, WebSocketEvent};
|
||||||
|
@ -14,3 +15,41 @@ pub struct WasmGateway {
|
||||||
store: GatewayStore,
|
store: GatewayStore,
|
||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl GatewayCapable<WsMessage, WsStream> for WasmGateway {
|
||||||
|
fn get_events(&self) -> Arc<Mutex<Events>> {
|
||||||
|
self.events.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_websocket_send(&self) -> Arc<Mutex<SplitSink<WsStream, WsMessage>>> {
|
||||||
|
self.websocket_send.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_store(&self) -> GatewayStore {
|
||||||
|
self.store.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_url(&self) -> String {
|
||||||
|
self.url.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn spawn<G: GatewayHandleCapable<WsMessage, WsStream>>(
|
||||||
|
websocket_url: String,
|
||||||
|
) -> Result<G, GatewayError> {
|
||||||
|
let (mut websocket_stream, _) = match WsMeta::connect(websocket_url, None).await {
|
||||||
|
Ok(ws) => Ok(ws),
|
||||||
|
Err(e) => Err(GatewayError::CannotConnect {
|
||||||
|
error: e.to_string(),
|
||||||
|
}),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
let mut event = match websocket_stream
|
||||||
|
.observe(ObserveConfig::channel(self, Channel::Unbounded))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(ok) => Ok(ok),
|
||||||
|
Err(e) => Err(GatewayError::CannotConnect { error: e }),
|
||||||
|
}?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
use super::*;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::gateway::GatewayHandleCapable;
|
||||||
|
use ws_stream_wasm::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct WasmGatewayHandle {
|
||||||
|
pub url: String,
|
||||||
|
pub events: Arc<Mutex<Events>>,
|
||||||
|
pub websocket_send: Arc<Mutex<SplitSink<WsStream, WsMessage>>>,
|
||||||
|
/// Tells gateway tasks to close
|
||||||
|
pub(super) kill_send: tokio::sync::broadcast::Sender<()>,
|
||||||
|
pub(crate) store: GatewayStore,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl GatewayHandleCapable<WsMessage, WsStream> for WasmGatewayHandle {
|
||||||
|
fn new(
|
||||||
|
url: String,
|
||||||
|
events: Arc<Mutex<Events>>,
|
||||||
|
websocket_send: Arc<Mutex<SplitSink<WsStream, WsMessage>>>,
|
||||||
|
kill_send: tokio::sync::broadcast::Sender<()>,
|
||||||
|
store: GatewayStore,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
url,
|
||||||
|
events,
|
||||||
|
websocket_send,
|
||||||
|
kill_send,
|
||||||
|
store,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
pub mod gateway;
|
pub mod gateway;
|
||||||
|
pub mod handle;
|
||||||
pub mod heartbeat;
|
pub mod heartbeat;
|
||||||
use super::*;
|
use super::*;
|
||||||
pub use gateway::*;
|
pub use gateway::*;
|
||||||
|
pub use handle::*;
|
||||||
pub use heartbeat::*;
|
pub use heartbeat::*;
|
||||||
use ws_stream_wasm::WsMessage;
|
use ws_stream_wasm::WsMessage;
|
||||||
|
|
||||||
|
@ -27,4 +29,8 @@ impl crate::gateway::MessageCapable for WsMessage {
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Self {
|
||||||
|
WsMessage::Text(s.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue