-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ClientConnector with default implementation
- Loading branch information
Showing
5 changed files
with
106 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::client::ClientConnector; | ||
use crate::Request; | ||
use enfync::TryAdopt; | ||
use tokio_tungstenite::tungstenite; | ||
|
||
/// Implementation of [`ClientConnector`] for tokio runtimes. | ||
#[derive(Clone)] | ||
pub struct ClientConnectorTokio { | ||
handle: enfync::builtin::native::TokioHandle, | ||
} | ||
|
||
impl ClientConnectorTokio { | ||
pub fn new(handle: tokio::runtime::Handle) -> Self { | ||
Self { handle: handle.into() } | ||
} | ||
} | ||
|
||
impl Default for ClientConnectorTokio { | ||
fn default() -> Self { | ||
let handle = enfync::builtin::native::TokioHandle::try_adopt() | ||
.expect("ClientConnectorTokio::default() only works inside a tokio runtime; use ClientConnectorTokionew() instead"); | ||
Self { handle } | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl ClientConnector for ClientConnectorTokio { | ||
type Handle = enfync::builtin::native::TokioHandle; | ||
type Message = tungstenite::Message; | ||
type WSError = tungstenite::error::Error; | ||
type Socket = tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>; | ||
type ConnectError = tungstenite::error::Error; | ||
|
||
/// Get the connector's runtime handle. | ||
fn handle(&self) -> Self::Handle { | ||
self.handle.clone() | ||
} | ||
|
||
/// Connect to a websocket server. | ||
/// | ||
/// Returns `Err` if the request is invalid. | ||
async fn connect(&self, request: Request) -> Result<Self::Socket, Self::ConnectError> { | ||
let (socket, _) = tokio_tungstenite::connect_async(request).await?; | ||
Ok(socket) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
#[cfg(feature = "native_client")] | ||
pub mod client_connector_tokio; | ||
|
||
//#[cfg(feature = "wasm_client")] | ||
//pub mod client_connector_wasm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters