-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1198 from iced-rs/subscription-helpers
`websocket` example and helpers to create custom subscriptions
- Loading branch information
Showing
10 changed files
with
615 additions
and
137 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
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,22 @@ | ||
[package] | ||
name = "websocket" | ||
version = "0.1.0" | ||
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
iced = { path = "../..", features = ["tokio", "debug"] } | ||
iced_native = { path = "../../native" } | ||
iced_futures = { path = "../../futures" } | ||
|
||
[dependencies.async-tungstenite] | ||
version = "0.16" | ||
features = ["tokio-rustls-webpki-roots"] | ||
|
||
[dependencies.tokio] | ||
version = "1" | ||
features = ["time"] | ||
|
||
[dependencies.warp] | ||
version = "0.3" |
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,17 @@ | ||
## Websocket | ||
|
||
A simple example that keeps a WebSocket connection open to an echo server. | ||
|
||
The example consists of 3 modules: | ||
- [`main`] contains the `Application` logic. | ||
- [`echo`] implements a WebSocket client for the [`echo::server`] with `async-tungstenite`. | ||
- [`echo::server`] implements a simple WebSocket echo server with `warp`. | ||
|
||
You can run it with `cargo run`: | ||
``` | ||
cargo run --package websocket | ||
``` | ||
|
||
[`main`]: src/main.rs | ||
[`echo`]: src/echo.rs | ||
[`echo::server`]: src/echo/server.rs |
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,146 @@ | ||
pub mod server; | ||
|
||
use iced_futures::futures; | ||
use iced_native::subscription::{self, Subscription}; | ||
|
||
use futures::channel::mpsc; | ||
use futures::sink::SinkExt; | ||
use futures::stream::StreamExt; | ||
|
||
use async_tungstenite::tungstenite; | ||
|
||
pub fn connect() -> Subscription<Event> { | ||
struct Connect; | ||
|
||
subscription::unfold( | ||
std::any::TypeId::of::<Connect>(), | ||
State::Disconnected, | ||
|state| async move { | ||
match state { | ||
State::Disconnected => { | ||
const ECHO_SERVER: &str = "ws://localhost:3030"; | ||
|
||
match async_tungstenite::tokio::connect_async(ECHO_SERVER) | ||
.await | ||
{ | ||
Ok((websocket, _)) => { | ||
let (sender, receiver) = mpsc::channel(100); | ||
|
||
( | ||
Some(Event::Connected(Connection(sender))), | ||
State::Connected(websocket, receiver), | ||
) | ||
} | ||
Err(_) => { | ||
let _ = tokio::time::sleep( | ||
tokio::time::Duration::from_secs(1), | ||
) | ||
.await; | ||
|
||
(Some(Event::Disconnected), State::Disconnected) | ||
} | ||
} | ||
} | ||
State::Connected(mut websocket, mut input) => { | ||
let mut fused_websocket = websocket.by_ref().fuse(); | ||
|
||
futures::select! { | ||
received = fused_websocket.select_next_some() => { | ||
match received { | ||
Ok(tungstenite::Message::Text(message)) => { | ||
( | ||
Some(Event::MessageReceived(Message::User(message))), | ||
State::Connected(websocket, input) | ||
) | ||
} | ||
Ok(_) => { | ||
(None, State::Connected(websocket, input)) | ||
} | ||
Err(_) => { | ||
(Some(Event::Disconnected), State::Disconnected) | ||
} | ||
} | ||
} | ||
|
||
message = input.select_next_some() => { | ||
let result = websocket.send(tungstenite::Message::Text(String::from(message))).await; | ||
|
||
if result.is_ok() { | ||
(None, State::Connected(websocket, input)) | ||
} else { | ||
(Some(Event::Disconnected), State::Disconnected) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
) | ||
} | ||
|
||
#[derive(Debug)] | ||
enum State { | ||
Disconnected, | ||
Connected( | ||
async_tungstenite::WebSocketStream< | ||
async_tungstenite::tokio::ConnectStream, | ||
>, | ||
mpsc::Receiver<Message>, | ||
), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Event { | ||
Connected(Connection), | ||
Disconnected, | ||
MessageReceived(Message), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Connection(mpsc::Sender<Message>); | ||
|
||
impl Connection { | ||
pub fn send(&mut self, message: Message) { | ||
let _ = self | ||
.0 | ||
.try_send(message) | ||
.expect("Send message to echo server"); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Message { | ||
Connected, | ||
Disconnected, | ||
User(String), | ||
} | ||
|
||
impl Message { | ||
pub fn new(message: &str) -> Option<Self> { | ||
if message.is_empty() { | ||
None | ||
} else { | ||
Some(Self::User(message.to_string())) | ||
} | ||
} | ||
|
||
pub fn connected() -> Self { | ||
Message::Connected | ||
} | ||
|
||
pub fn disconnected() -> Self { | ||
Message::Disconnected | ||
} | ||
} | ||
|
||
impl From<Message> for String { | ||
fn from(message: Message) -> Self { | ||
match message { | ||
Message::Connected => String::from("Connected successfully!"), | ||
Message::Disconnected => { | ||
String::from("Connection lost... Retrying...") | ||
} | ||
Message::User(message) => message, | ||
} | ||
} | ||
} |
Oops, something went wrong.