pub struct SignalRClient { /* private fields */ }
Expand description
A client for connecting to and interacting with a SignalR hub.
The SignalRClient
can be used to invoke methods on the hub, send messages, and register callbacks.
The client can be cloned and used freely across different parts of your application.
§Examples
// Connect to the SignalR server with custom configuration
let mut client = SignalRClient::connect_with("localhost", "test", |c| {
c.with_port(5220); // Set the port to 5220
c.unsecure(); // Use an unsecure (HTTP) connection
}).await.unwrap();
// Invoke the "SingleEntity" method and assert the result
let re = client.invoke::<TestEntity>("SingleEntity".to_string()).await;
assert!(re.is_ok());
// Unwrap the result and assert the entity's text
let entity = re.unwrap();
assert_eq!(entity.text, "test".to_string());
// Log the entity's details
info!("Entity {}, {}", entity.text, entity.number);
// Enumerate "HundredEntities" and log each entity
let mut he = client.enumerate::<TestEntity>("HundredEntities".to_string()).await;
while let Some(item) = he.next().await {
info!("Entity {}, {}", item.text, item.number);
}
info!("Finished fetching entities, calling pushes");
// Invoke the "PushEntity" method with arguments and assert the result
let push1 = client.invoke_with_args::<bool, _>("PushEntity".to_string(), |c| {
c.argument(TestEntity {
text: "push1".to_string(),
number: 100,
});
}).await;
assert!(push1.unwrap());
// Clone the client and invoke the "PushTwoEntities" method with arguments
let mut secondclient = client.clone();
let push2 = secondclient.invoke_with_args::<TestEntity, _>("PushTwoEntities".to_string(), |c| {
c.argument(TestEntity {
text: "entity1".to_string(),
number: 200,
}).argument(TestEntity {
text: "entity2".to_string(),
number: 300,
});
}).await;
assert!(push2.is_ok());
// Unwrap the result and assert the merged entity's number
let entity = push2.unwrap();
assert_eq!(entity.number, 500);
info!("Merged Entity {}, {}", entity.text, entity.number);
// Drop the second client
drop(secondclient);
// Register callbacks for "callback1" and "callback2"
let c1 = client.register("callback1".to_string(), |ctx| {
let result = ctx.argument::<TestEntity>(0);
if result.is_ok() {
let entity = result.unwrap();
info!("Callback results entity: {}, {}", entity.text, entity.number);
}
});
let c2 = client.register("callback2".to_string(), |mut ctx| {
let result = ctx.argument::<TestEntity>(0);
if result.is_ok() {
let entity = result.unwrap();
info!("Callback2 results entity: {}, {}", entity.text, entity.number);
let e2 = entity.clone();
spawn(async move {
info!("Completing callback2");
let _ = ctx.complete(e2).await;
});
}
});
// Trigger the callbacks
info!("Calling callback1");
_ = client.send_with_args("TriggerEntityCallback".to_string(), |c| {
c.argument("callback1".to_string());
}).await;
info!("Calling callback2");
let succ = client.invoke_with_args::<bool, _>("TriggerEntityResponse".to_string(), |c| {
c.argument("callback2".to_string());
}).await;
assert!(succ.unwrap());
// Measure the time taken to fetch a million entities
let now = Instant::now();
{
let mut me = client.enumerate::<TestEntity>("MillionEntities".to_string()).await;
while let Some(_) = me.next().await {}
}
let elapsed = now.elapsed();
info!("1 million entities fetched in: {:.2?}", elapsed);
// Unregister the callbacks and disconnect the client
c1.unregister();
c2.unregister();
client.disconnect();
Implementations§
Source§impl SignalRClient
impl SignalRClient
Sourcepub async fn connect(domain: &str, hub: &str) -> Result<Self, String>
pub async fn connect(domain: &str, hub: &str) -> Result<Self, String>
Connects to a SignalR hub using the default connection configuration.
§Arguments
domain
- A string slice that holds the domain of the SignalR server.hub
- A string slice that holds the name of the hub to connect to.
§Returns
Result<Self, String>
- On success, returns an instance ofSelf
. On failure, returns an error message as aString
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
Sourcepub async fn connect_with<F>(
domain: &str,
hub: &str,
options: F,
) -> Result<Self, String>where
F: FnMut(&mut ConnectionConfiguration),
pub async fn connect_with<F>(
domain: &str,
hub: &str,
options: F,
) -> Result<Self, String>where
F: FnMut(&mut ConnectionConfiguration),
Connects to a SignalR hub with custom connection properties.
§Arguments
domain
- A string slice that holds the domain of the SignalR server.hub
- A string slice that holds the name of the hub to connect to.options
- A closure that allows the user to configure the connection properties.
§Returns
Result<Self, String>
- On success, returns an instance ofSelf
. On failure, returns an error message as aString
.
§Examples
let client = SignalRClient::connect_with("localhost", "test", |c| {
c.with_port(5220);
c.unsecure();
}).await.unwrap();
Sourcepub fn register(
&mut self,
target: String,
callback: impl Fn(InvocationContext) + 'static,
) -> impl CallbackHandler
pub fn register( &mut self, target: String, callback: impl Fn(InvocationContext) + 'static, ) -> impl CallbackHandler
Registers a callback that can be called by the SignalR hub.
§Arguments
target
- AString
specifying the name of the target method to register the callback for.callback
- A closure that takes anInvocationContext
as an argument and defines the callback logic.
§Returns
impl CallbackHandler
- Returns an implementation ofCallbackHandler
that can be used to manage the callback. TheCallbackHandler
can be used to unregister the callback using itsunregister
method.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let handler = client.register("callback1".to_string(), |ctx| {
let result = ctx.argument::<TestEntity>(0);
if result.is_ok() {
let entity = result.unwrap();
info!("Callback results entity: {}, {}", entity.text, entity.number);
}
});
// Unregister the callback when it's no longer needed
handler.unregister();
Sourcepub async fn invoke<T: 'static + DeserializeOwned + Unpin>(
&mut self,
target: String,
) -> Result<T, String>
pub async fn invoke<T: 'static + DeserializeOwned + Unpin>( &mut self, target: String, ) -> Result<T, String>
Invokes a specific target method on the SignalR hub and waits for the response.
§Arguments
target
- AString
specifying the name of the target method to invoke on the hub.
§Returns
Result<T, String>
- On success, returns the response of typeT
. On failure, returns an error message as aString
.
§Type Parameters
T
- The type of the response, which must implementDeserializeOwned
andUnpin
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let response: Result<TestEntity, String> = client.invoke("SingleEntity".to_string()).await;
match response {
Ok(entity) => {
info!("Received entity: {}, {}", entity.text, entity.number);
}
Err(e) => {
error!("Failed to invoke method: {}", e);
}
}
Sourcepub async fn invoke_with_args<T: 'static + DeserializeOwned + Unpin, F>(
&mut self,
target: String,
configuration: F,
) -> Result<T, String>where
F: FnMut(&mut ArgumentConfiguration),
pub async fn invoke_with_args<T: 'static + DeserializeOwned + Unpin, F>(
&mut self,
target: String,
configuration: F,
) -> Result<T, String>where
F: FnMut(&mut ArgumentConfiguration),
Invokes a specific target method on the SignalR hub with custom arguments and waits for the response.
§Arguments
target
- AString
specifying the name of the target method to invoke on the hub.configuration
- A mutable closure that allows the user to configure the arguments for the method call.
§Returns
Result<T, String>
- On success, returns the response of typeT
. On failure, returns an error message as aString
.
§Type Parameters
T
- The type of the response, which must implementDeserializeOwned
andUnpin
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let response: Result<TestEntity, String> = client.invoke_with_args("PushTwoEntities".to_string(), |c| {
c.argument(TestEntity {
text: "entity1".to_string(),
number: 200,
}).argument(TestEntity {
text: "entity2".to_string(),
number: 300,
});
}).await;
match response {
Ok(entity) => {
info!("Merged Entity {}, {}", entity.text, entity.number);
}
Err(e) => {
error!("Failed to invoke method: {}", e);
}
}
Sourcepub async fn send(&mut self, target: String) -> Result<(), String>
pub async fn send(&mut self, target: String) -> Result<(), String>
Calls a specific target method on the SignalR hub without waiting for the response.
§Arguments
target
- AString
specifying the name of the target method to call on the hub.
§Returns
Result<(), String>
- On success, returnsOk(())
. On failure, returns an error message as aString
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let result = client.send("TriggerEntityCallback".to_string()).await;
match result {
Ok(_) => {
info!("Method called successfully");
}
Err(e) => {
error!("Failed to call method: {}", e);
}
}
Sourcepub async fn send_with_args<F>(
&mut self,
target: String,
configuration: F,
) -> Result<(), String>where
F: FnMut(&mut ArgumentConfiguration),
pub async fn send_with_args<F>(
&mut self,
target: String,
configuration: F,
) -> Result<(), String>where
F: FnMut(&mut ArgumentConfiguration),
Calls a specific target method on the SignalR hub with custom arguments without waiting for the response.
§Arguments
target
- AString
specifying the name of the target method to call on the hub.configuration
- A closure that allows the user to configure the arguments for the method call.
§Returns
Result<(), String>
- On success, returnsOk(())
. On failure, returns an error message as aString
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let result = client.send_with_args("TriggerEntityCallback".to_string(), |c| {
c.argument("callback1".to_string());
}).await;
match result {
Ok(_) => {
info!("Method called successfully");
}
Err(e) => {
error!("Failed to call method: {}", e);
}
}
Sourcepub async fn enumerate<T: 'static + DeserializeOwned + Unpin>(
&mut self,
target: String,
) -> impl Stream<Item = T>
pub async fn enumerate<T: 'static + DeserializeOwned + Unpin>( &mut self, target: String, ) -> impl Stream<Item = T>
Calls a specific target method on the SignalR hub and returns a stream for receiving data asynchronously.
The target method on the hub should return an IAsyncEnumerable
to send back data asynchronously.
§Arguments
target
- AString
specifying the name of the target method to call on the hub.
§Returns
impl Stream<Item = T>
- Returns a stream of items of typeT
.
§Type Parameters
T
- The type of the items in the stream, which must implementDeserializeOwned
andUnpin
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let mut stream = client.enumerate::<TestEntity>("HundredEntities".to_string()).await;
while let Some(entity) = stream.next().await {
info!("Received entity: {}, {}", entity.text, entity.number);
}
Sourcepub async fn enumerate_with_args<T: 'static + DeserializeOwned + Unpin, F>(
&mut self,
target: String,
configuration: F,
) -> impl Stream<Item = T>where
F: FnMut(&mut ArgumentConfiguration),
pub async fn enumerate_with_args<T: 'static + DeserializeOwned + Unpin, F>(
&mut self,
target: String,
configuration: F,
) -> impl Stream<Item = T>where
F: FnMut(&mut ArgumentConfiguration),
Calls a specific target method on the SignalR hub with custom arguments and returns a stream for receiving data asynchronously.
The target method on the hub should return an IAsyncEnumerable
to send back data asynchronously.
§Arguments
target
- AString
specifying the name of the target method to call on the hub.configuration
- A mutable closure that allows the user to configure the arguments for the method call.
§Returns
impl Stream<Item = T>
- Returns a stream of items of typeT
.
§Type Parameters
T
- The type of the items in the stream, which must implementDeserializeOwned
andUnpin
.
§Examples
let client = SignalRClient::connect("localhost", "test").await.unwrap();
let mut stream = client.enumerate_with_args::<TestEntity, _>("HundredEntities".to_string(), |c| {
c.argument("some_argument".to_string());
}).await;
while let Some(entity) = stream.next().await {
info!("Received entity: {}, {}", entity.text, entity.number);
}