pub struct LoroWebsocketClient { /* private fields */ }Expand description
A higher-level WebSocket client that manages rooms and applies updates to a LoroDoc.
Implementations§
Source§impl LoroWebsocketClient
impl LoroWebsocketClient
Sourcepub async fn connect(url: &str) -> Result<Self, ClientError>
pub async fn connect(url: &str) -> Result<Self, ClientError>
Connect and spawn reader/writer tasks with default config.
Examples found in repository?
12async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
13 // Args: role receiver|sender, url, room
14 let mut args = std::env::args().skip(1).collect::<Vec<_>>();
15 if args.len() < 3 {
16 eprintln!("usage: elo_index_client <receiver|sender> <ws_url> <room_id>");
17 std::process::exit(2);
18 }
19 let role = args.remove(0);
20 let url = args.remove(0);
21 let room = args.remove(0);
22
23 let client = LoroWebsocketClient::connect(&url).await?;
24 let doc = Arc::new(tokio::sync::Mutex::new(LoroDoc::new()));
25
26 match role.as_str() {
27 "sender" => {
28 // Prepare desired state before join so adaptor sends it as initial snapshot
29 {
30 let d = doc.lock().await;
31 d.get_text("t").insert(0, "hi").unwrap();
32 d.commit();
33 }
34 let _room = client
35 .join_elo_with_adaptor(&room, doc.clone(), "k1".to_string(), KEY)
36 .await?;
37 // Allow time for the async writer and server broadcast to complete.
38 // Align with JS wrapper's 800ms flush to avoid flakiness in cross-lang tests.
39 sleep(Duration::from_millis(800)).await;
40 Ok(())
41 }
42 "receiver" => {
43 let _room = client
44 .join_elo_with_adaptor(&room, doc.clone(), "k1".to_string(), KEY)
45 .await?;
46 // Poll document until expected content arrives
47 let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
48 loop {
49 {
50 let d = doc.lock().await;
51 let s = d.get_text("t").to_string();
52 if s == "hi" {
53 println!("INDEXED 1");
54 return Ok(());
55 }
56 }
57 if tokio::time::Instant::now() > deadline {
58 eprintln!("timeout waiting for content");
59 std::process::exit(1);
60 }
61 sleep(Duration::from_millis(50)).await;
62 }
63 }
64 _ => {
65 eprintln!("usage: elo_index_client <receiver|sender> <ws_url> <room_id>");
66 std::process::exit(2);
67 }
68 }
69}Sourcepub async fn connect_with_config(
url: &str,
config: ClientConfig,
) -> Result<Self, ClientError>
pub async fn connect_with_config( url: &str, config: ClientConfig, ) -> Result<Self, ClientError>
Connect and spawn reader/writer tasks with custom config.
Sourcepub async fn join_loro(
&self,
room_id: &str,
doc: Arc<Mutex<LoroDoc>>,
) -> Result<LoroWebsocketClientRoom, ClientError>
pub async fn join_loro( &self, room_id: &str, doc: Arc<Mutex<LoroDoc>>, ) -> Result<LoroWebsocketClientRoom, ClientError>
Join a Loro room for the given document. Returns a handle for sending updates.
Sourcepub fn ping(&self) -> Result<(), ClientError>
pub fn ping(&self) -> Result<(), ClientError>
Send a keepalive ping.
Sourcepub async fn join_with_adaptor(
&self,
room_id: &str,
adaptor: Box<dyn CrdtDocAdaptor + Send + Sync>,
) -> Result<LoroWebsocketClientRoom, ClientError>
pub async fn join_with_adaptor( &self, room_id: &str, adaptor: Box<dyn CrdtDocAdaptor + Send + Sync>, ) -> Result<LoroWebsocketClientRoom, ClientError>
Generic join with a CRDT adaptor. Returns a room handle.
Sourcepub async fn join_loro_with_adaptor(
&self,
room_id: &str,
doc: Arc<Mutex<LoroDoc>>,
) -> Result<LoroWebsocketClientRoom, ClientError>
pub async fn join_loro_with_adaptor( &self, room_id: &str, doc: Arc<Mutex<LoroDoc>>, ) -> Result<LoroWebsocketClientRoom, ClientError>
Convenience: join a Loro room using LoroDocAdaptor.
Sourcepub async fn join_elo_with_adaptor(
&self,
room_id: &str,
doc: Arc<Mutex<LoroDoc>>,
key_id: impl Into<String>,
key: [u8; 32],
) -> Result<LoroWebsocketClientRoom, ClientError>
pub async fn join_elo_with_adaptor( &self, room_id: &str, doc: Arc<Mutex<LoroDoc>>, key_id: impl Into<String>, key: [u8; 32], ) -> Result<LoroWebsocketClientRoom, ClientError>
Convenience: join an %ELO room using EloDocAdaptor (AES-256-GCM key).
Examples found in repository?
12async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
13 // Args: role receiver|sender, url, room
14 let mut args = std::env::args().skip(1).collect::<Vec<_>>();
15 if args.len() < 3 {
16 eprintln!("usage: elo_index_client <receiver|sender> <ws_url> <room_id>");
17 std::process::exit(2);
18 }
19 let role = args.remove(0);
20 let url = args.remove(0);
21 let room = args.remove(0);
22
23 let client = LoroWebsocketClient::connect(&url).await?;
24 let doc = Arc::new(tokio::sync::Mutex::new(LoroDoc::new()));
25
26 match role.as_str() {
27 "sender" => {
28 // Prepare desired state before join so adaptor sends it as initial snapshot
29 {
30 let d = doc.lock().await;
31 d.get_text("t").insert(0, "hi").unwrap();
32 d.commit();
33 }
34 let _room = client
35 .join_elo_with_adaptor(&room, doc.clone(), "k1".to_string(), KEY)
36 .await?;
37 // Allow time for the async writer and server broadcast to complete.
38 // Align with JS wrapper's 800ms flush to avoid flakiness in cross-lang tests.
39 sleep(Duration::from_millis(800)).await;
40 Ok(())
41 }
42 "receiver" => {
43 let _room = client
44 .join_elo_with_adaptor(&room, doc.clone(), "k1".to_string(), KEY)
45 .await?;
46 // Poll document until expected content arrives
47 let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
48 loop {
49 {
50 let d = doc.lock().await;
51 let s = d.get_text("t").to_string();
52 if s == "hi" {
53 println!("INDEXED 1");
54 return Ok(());
55 }
56 }
57 if tokio::time::Instant::now() > deadline {
58 eprintln!("timeout waiting for content");
59 std::process::exit(1);
60 }
61 sleep(Duration::from_millis(50)).await;
62 }
63 }
64 _ => {
65 eprintln!("usage: elo_index_client <receiver|sender> <ws_url> <room_id>");
66 std::process::exit(2);
67 }
68 }
69}Trait Implementations§
Source§impl Clone for LoroWebsocketClient
impl Clone for LoroWebsocketClient
Source§fn clone(&self) -> LoroWebsocketClient
fn clone(&self) -> LoroWebsocketClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for LoroWebsocketClient
impl !RefUnwindSafe for LoroWebsocketClient
impl Send for LoroWebsocketClient
impl Sync for LoroWebsocketClient
impl Unpin for LoroWebsocketClient
impl !UnwindSafe for LoroWebsocketClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);