Skip to content

Commit

Permalink
Rewrite websocket example using sipper
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 11, 2025
1 parent 05618ea commit f37d068
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 68 deletions.
98 changes: 38 additions & 60 deletions examples/websocket/src/echo.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,63 @@
pub mod server;

use iced::futures;
use iced::stream;
use iced::task::{sipper, Never, Sipper};
use iced::widget::text;

use futures::channel::mpsc;
use futures::sink::SinkExt;
use futures::stream::{Stream, StreamExt};
use futures::stream::StreamExt;

use async_tungstenite::tungstenite;
use std::fmt;

pub fn connect() -> impl Stream<Item = Event> {
stream::channel(100, |mut output| async move {
let mut state = State::Disconnected;

pub fn connect() -> impl Sipper<Never, Event> {
sipper(|mut output| async move {
loop {
match &mut state {
State::Disconnected => {
const ECHO_SERVER: &str = "ws://127.0.0.1:3030";
const ECHO_SERVER: &str = "ws://127.0.0.1:3030";

match async_tungstenite::tokio::connect_async(ECHO_SERVER)
.await
{
Ok((websocket, _)) => {
let (sender, receiver) = mpsc::channel(100);
let (mut websocket, mut input) =
match async_tungstenite::tokio::connect_async(ECHO_SERVER).await
{
Ok((websocket, _)) => {
let (sender, receiver) = mpsc::channel(100);

let _ = output
.send(Event::Connected(Connection(sender)))
.await;
let _ = output
.send(Event::Connected(Connection(sender)))
.await;

state = State::Connected(websocket, receiver);
}
Err(_) => {
tokio::time::sleep(
tokio::time::Duration::from_secs(1),
)
(websocket.fuse(), receiver)
}
Err(_) => {
tokio::time::sleep(tokio::time::Duration::from_secs(1))
.await;

let _ = output.send(Event::Disconnected).await;
}
let _ = output.send(Event::Disconnected).await;

continue;
}
}
State::Connected(websocket, input) => {
let mut fused_websocket = websocket.by_ref().fuse();

futures::select! {
received = fused_websocket.select_next_some() => {
match received {
Ok(tungstenite::Message::Text(message)) => {
let _ = output.send(Event::MessageReceived(Message::User(message))).await;
}
Err(_) => {
let _ = output.send(Event::Disconnected).await;

state = State::Disconnected;
}
Ok(_) => continue,
};

loop {
futures::select! {
received = websocket.select_next_some() => {
match received {
Ok(tungstenite::Message::Text(message)) => {
let _ = output.send(Event::MessageReceived(Message::User(message))).await;
}
Err(_) => {
let _ = output.send(Event::Disconnected).await;
break;
}
Ok(_) => continue,
}
}

message = input.select_next_some() => {
let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
message = input.select_next_some() => {
let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;

if result.is_err() {
let _ = output.send(Event::Disconnected).await;

state = State::Disconnected;
}
if result.is_err() {
let _ = output.send(Event::Disconnected).await;
}
}
}
Expand All @@ -76,18 +66,6 @@ pub fn connect() -> impl Stream<Item = Event> {
})
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum State {
Disconnected,
Connected(
async_tungstenite::WebSocketStream<
async_tungstenite::tokio::ConnectStream,
>,
mpsc::Receiver<Message>,
),
}

#[derive(Debug, Clone)]
pub enum Event {
Connected(Connection),
Expand Down
11 changes: 5 additions & 6 deletions futures/src/backend/native/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ impl crate::Executor for Executor {
pub mod time {
//! Listen and react to time.
use crate::core::time::{Duration, Instant};
use crate::stream;
use crate::subscription::Subscription;
use crate::MaybeSend;

use futures::SinkExt;
use futures::stream;
use std::future::Future;

/// Returns a [`Subscription`] that produces messages at a set interval.
Expand Down Expand Up @@ -66,12 +65,12 @@ pub mod time {
let f = *f;
let interval = *interval;

stream::channel(1, move |mut output| async move {
loop {
let _ = output.send(f().await).await;

stream::unfold(0, move |i| async move {
if i > 0 {
tokio::time::sleep(interval).await;
}

Some((f().await, i + 1))
})
})
}
Expand Down
1 change: 0 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub mod backend;
pub mod event;
pub mod executor;
pub mod keyboard;
pub mod stream;
pub mod subscription;

pub use executor::Executor;
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ use iced_winit::core;
use iced_winit::runtime;

pub use iced_futures::futures;
pub use iced_futures::stream;

#[cfg(feature = "highlighter")]
pub use iced_highlighter as highlighter;
Expand Down

0 comments on commit f37d068

Please sign in to comment.