pub struct WsClient { /* private fields */ }Expand description
WebSocket client implementation
Implementations§
Source§impl WsClient
impl WsClient
Sourcepub fn from_url(url: &str) -> Result<Self, Box<dyn Error>>
pub fn from_url(url: &str) -> Result<Self, Box<dyn Error>>
Create a new WebSocket client from URL string
Sourcepub fn new<A: ToSocketAddrs>(addr: A) -> Result<Self, Box<dyn Error>>
pub fn new<A: ToSocketAddrs>(addr: A) -> Result<Self, Box<dyn Error>>
Create a new WebSocket client
Sourcepub fn blocking_session(
&self,
token: Option<String>,
) -> Result<WsBlockingSession, Error>
pub fn blocking_session( &self, token: Option<String>, ) -> Result<WsBlockingSession, Error>
Create a blocking session
Examples found in repository?
examples/blocking_ws.rs (line 12)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7 // Connect to ReifyDB server - various ways to specify address:
8 // Using tuple (address, port):
9 let client = Client::ws(("127.0.0.1", 8090))?;
10
11 // Create a blocking session with authentication
12 let mut session = client.blocking_session(Some("mysecrettoken".to_string()))?;
13
14 // Execute a command to create a table
15 let command_result =
16 session.command("CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }", None)?;
17 println!("Command executed: {} frames returned", command_result.frames.len());
18
19 // Execute a query
20 let query_result = session.query("MAP { x: 42, y: 'hello' }", None)?;
21
22 println!("Query executed: {} frames returned", query_result.frames.len());
23
24 // Print first frame if available
25 if let Some(frame) = query_result.frames.first() {
26 println!("First frame:\n{}", frame);
27 }
28
29 Ok(())
30}Sourcepub fn callback_session(
&self,
token: Option<String>,
) -> Result<WsCallbackSession, Error>
pub fn callback_session( &self, token: Option<String>, ) -> Result<WsCallbackSession, Error>
Create a callback-based session
Examples found in repository?
examples/callback_ws.rs (line 13)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // Connect to ReifyDB server
10 let client = Client::ws(("127.0.0.1", 8090))?;
11
12 // Create a callback session with authentication
13 let session = client.callback_session(Some("mysecrettoken".to_string()))?;
14
15 // Execute a command to create a table
16 let command_id = session.command(
17 "CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }",
18 None,
19 |result| match result {
20 Ok(data) => println!("Command executed: {} frames returned", data.frames.len()),
21 Err(e) => println!("Command failed: {}", e),
22 },
23 )?;
24 println!("Command sent with ID: {}", command_id);
25
26 // Execute a query
27 let query_id = session.query("MAP { x: 42, y: 'hello' }", None, |result| {
28 match result {
29 Ok(data) => {
30 println!("Query executed: {} frames returned", data.frames.len());
31 // Print first frame if available
32 if let Some(frame) = data.frames.first() {
33 println!("First frame:\n{}", frame);
34 }
35 }
36 Err(e) => println!("Query failed: {}", e),
37 }
38 })?;
39 println!("Query sent with ID: {}", query_id);
40
41 // Wait for callbacks to complete
42 thread::sleep(Duration::from_millis(500));
43
44 Ok(())
45}Sourcepub fn channel_session(
&self,
token: Option<String>,
) -> Result<(WsChannelSession, Receiver<ResponseMessage>), Error>
pub fn channel_session( &self, token: Option<String>, ) -> Result<(WsChannelSession, Receiver<ResponseMessage>), Error>
Create a channel-based session
Examples found in repository?
examples/channel_ws.rs (line 13)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // Connect to ReifyDB server
10 let client = Client::ws(("127.0.0.1", 8090))?;
11
12 // Create a channel session with authentication
13 let (session, receiver) = client.channel_session(Some("mysecrettoken".to_string()))?;
14
15 // Consume authentication response
16 if let Ok(msg) = receiver.recv_timeout(Duration::from_millis(100)) {
17 if let Ok(ChannelResponse::Auth {
18 request_id,
19 }) = msg.response
20 {
21 println!("Authenticated with ID: {}", request_id);
22 }
23 }
24
25 // Execute a command to create a table
26 let command_id =
27 session.command("CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }", None)?;
28 println!("Command sent with ID: {}", command_id);
29
30 // Execute a query
31 let query_id = session.query("MAP { x: 42, y: 'hello' }", None)?;
32 println!("Query sent with ID: {}", query_id);
33
34 // Receive responses
35 let mut received = 0;
36 while received < 2 {
37 match receiver.recv_timeout(Duration::from_secs(1)) {
38 Ok(msg) => {
39 match msg.response {
40 Ok(ChannelResponse::Command {
41 request_id,
42 result,
43 }) => {
44 println!(
45 "Command {} executed: {} frames returned",
46 request_id,
47 result.frames.len()
48 );
49 received += 1;
50 }
51 Ok(ChannelResponse::Query {
52 request_id,
53 result,
54 }) => {
55 println!(
56 "Query {} executed: {} frames returned",
57 request_id,
58 result.frames.len()
59 );
60 // Print first frame if
61 // available
62 if let Some(frame) = result.frames.first() {
63 println!("First frame:\n{}", frame);
64 }
65 received += 1;
66 }
67 Ok(ChannelResponse::Auth {
68 ..
69 }) => {
70 // Already handled above
71 }
72 Err(e) => {
73 println!("Request {} failed: {}", msg.request_id, e);
74 received += 1;
75 }
76 }
77 }
78 Err(_) => {
79 println!("Timeout waiting for responses");
80 break;
81 }
82 }
83 }
84
85 Ok(())
86}Trait Implementations§
Auto Trait Implementations§
impl Freeze for WsClient
impl RefUnwindSafe for WsClient
impl Send for WsClient
impl Sync for WsClient
impl Unpin for WsClient
impl UnwindSafe for WsClient
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
Mutably borrows from an owned value. Read more