rpc-core

Crates.iorpc-core
lib.rsrpc-core
version0.3.2
sourcesrc
created_at2023-11-28 08:38:21.613872
updated_at2024-01-04 06:57:52.671739
descriptiona tiny rpc library for rust
homepagehttps://github.com/shuai132/rpc_core/tree/master/rust
repositoryhttps://github.com/shuai132/rpc_core
max_upload_size
id1051705
size96,581
liushuai (shuai132)

documentation

README

rpc-core

Build Status Latest version Documentation License

Usage

Run the following Cargo command in your project directory:

cargo add rpc-core

Or add the following line to your Cargo.toml:

[dependencies]
rpc-core = { version = "0.3.2", features = ["net"] }

Example

See src/tests for details:

  • receiver

    fn subscribe() {
        rpc_s.subscribe("cmd", |msg: String| -> String {
            assert_eq!(msg, "hello");
            "world".to_string()
        });
    }
    
    
  • sender (callback)

    fn call() {
        rpc_c.cmd("cmd")
            .msg("hello")
            .rsp(|msg: String| {
                assert_eq!(msg, "world");
            })
            .call();
    }
    
  • sender (future)

    async fn call() {
        let result = rpc_c.cmd("cmd").msg("hello").future::<String>().await;
        assert_eq!(result.result.unwrap(), "world");
    }
    

Features

net

See examples for details: src/examples

  • server

    fn server() {
        let rpc = Rpc::new(None);
        rpc.subscribe("cmd", |msg: String| -> String {
            assert_eq!(msg, "hello");
            "world".to_string()
        });
      
        let server = rpc_server::RpcServer::new(6666, RpcConfigBuilder::new().rpc(Some(rpc.clone())).build());
        server.start();
    }
    
  • client

    async fn client() {
        let rpc = Rpc::new(None);
        let client = rpc_client::RpcClient::new(RpcConfigBuilder::new().rpc(Some(rpc.clone())).build());
        client.set_reconnect(1000);
        client.open("localhost", 6666);
    
        let result = rpc.cmd("cmd").msg("hello").future::<String>().await;
        assert_eq!(result.result.unwrap(), "world");
    }
    

License

This project is licensed under the MIT license.

Commit count: 228

cargo fmt