Skip to content

Commit

Permalink
Merge branch 'websockets-backend' into feature/wasm32-unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfl0wer committed Nov 19, 2023
2 parents 5a9a0d1 + d7ec422 commit 5141723
Show file tree
Hide file tree
Showing 11 changed files with 738 additions and 678 deletions.
65 changes: 65 additions & 0 deletions src/gateway/backend_tungstenite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use futures_util::{
stream::{SplitSink, SplitStream},
StreamExt,
};
use tokio::net::TcpStream;
use tokio_tungstenite::{
connect_async_tls_with_config, tungstenite, Connector, MaybeTlsStream, WebSocketStream,
};

use super::GatewayMessage;
use crate::errors::GatewayError;

#[derive(Debug, Clone)]
pub struct WebSocketBackend;

// These could be made into inherent associated types when that's stabilized
pub type WsSink = SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, tungstenite::Message>;
pub type WsStream = SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>;

impl WebSocketBackend {
pub async fn connect(
websocket_url: &str,
) -> Result<(WsSink, WsStream), crate::errors::GatewayError> {
let mut roots = rustls::RootCertStore::empty();
for cert in rustls_native_certs::load_native_certs().expect("could not load platform certs")
{
roots.add(&rustls::Certificate(cert.0)).unwrap();
}
let (websocket_stream, _) = match connect_async_tls_with_config(
websocket_url,
None,
false,
Some(Connector::Rustls(
rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth()
.into(),
)),
)
.await
{
Ok(websocket_stream) => websocket_stream,
Err(e) => {
return Err(GatewayError::CannotConnect {
error: e.to_string(),
})
}
};

Ok(websocket_stream.split())
}
}

impl From<GatewayMessage> for tungstenite::Message {
fn from(message: GatewayMessage) -> Self {
Self::Text(message.0)
}
}

impl From<tungstenite::Message> for GatewayMessage {
fn from(value: tungstenite::Message) -> Self {
Self(value.to_string())
}
}
189 changes: 0 additions & 189 deletions src/gateway/default/gateway.rs

This file was deleted.

131 changes: 0 additions & 131 deletions src/gateway/default/handle.rs

This file was deleted.

Loading

0 comments on commit 5141723

Please sign in to comment.