{"id":17323843,"url":"https://github.com/harlanc/jsonrpc2-rs","last_synced_at":"2025-04-14T16:22:41.706Z","repository":{"id":63808293,"uuid":"563200572","full_name":"harlanc/jsonrpc2-rs","owner":"harlanc","description":"A JSON-RPC 2.0 client/server library in rust.","archived":false,"fork":false,"pushed_at":"2023-02-03T00:32:14.000Z","size":84,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-28T05:06:22.269Z","etag":null,"topics":["json-rpc","json-rpc-client","json-rpc-server","json-rpc2","rpc","rust","websocket"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/harlanc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-08T05:32:59.000Z","updated_at":"2024-11-05T13:37:10.000Z","dependencies_parsed_at":"2023-02-18T01:46:16.866Z","dependency_job_id":null,"html_url":"https://github.com/harlanc/jsonrpc2-rs","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harlanc%2Fjsonrpc2-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harlanc%2Fjsonrpc2-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harlanc%2Fjsonrpc2-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harlanc%2Fjsonrpc2-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/harlanc","download_url":"https://codeload.github.com/harlanc/jsonrpc2-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248731568,"owners_count":21152835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-rpc","json-rpc-client","json-rpc-server","json-rpc2","rpc","rust","websocket"],"created_at":"2024-10-15T14:09:07.537Z","updated_at":"2025-04-14T16:22:41.672Z","avatar_url":"https://github.com/harlanc.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonrpc2-rs\n\n\n[![crates.io](https://img.shields.io/crates/v/jrpc2.svg)](https://crates.io/crates/jrpc2)\n\nA JSON-RPC 2.0 client/server library in rust.\n\n\n# How to use\n\n## Define the structed value types\n\nYou should define the types of the follwing three structed values:\n\n - Request Parameters [Reference](https://www.jsonrpc.org/specification#parameter_structures)\n - Response Result [Reference](https://www.jsonrpc.org/specification#response_object)\n - Error Data [Reference](https://www.jsonrpc.org/specification#error_object)\n\nFor example:\n        \n    type RequestParams = Vec\u003cu32\u003e;\n    type ResponseResult = u32;\n    type ErrorData = String;\n       \n## Define the handler\n\n   You should implement the THandler trait to define the server side logic:\n   \n    #[async_trait]\n    pub trait THandler\u003cS, R, E\u003e\n    where\n        S: Serialize,\n    {\n        async fn handle(\u0026self, conn: Arc\u003cJsonRpc2\u003cS, R, E\u003e\u003e, request: Request\u003cS\u003e);\n    } \n    \n For example:\n \n    struct Add {}\n\n    #[async_trait]\n    impl THandler\u003cRequestParams, ResponseResult, ErrorData\u003e for Add {\n        async fn handle(\n            \u0026self,\n            json_rpc2: Arc\u003cJsonRpc2\u003cRequestParams, ResponseResult, ErrorData\u003e\u003e,\n            request: Request\u003cRequestParams\u003e,\n        ) {\n            match request.method.as_str() {\n                \"add\" =\u003e {\n                    let params = request.params.unwrap();\n                    let add_res: u32 = params.iter().sum();\n                    let response = Response::new(request.id.unwrap(), Some(add_res), None);\n                    json_rpc2.response(response).unwrap();\n                }\n\n                _ =\u003e {\n                    log::info!(\"unknow method\");\n                }\n            }\n        }\n    }\n\n## Init JSON-RPC server\n\n    let addr = \"127.0.0.1:9002\";\n    let listener = TcpListener::bind(\u0026addr).await.expect(\"Can't listen\");\n\n    if let Ok((stream, _)) = listener.accept().await {\n        let server_stream = ServerObjectStream::accept(stream)\n            .await\n            .expect(\"cannot generate object stream\");\n        JsonRpc2::new(Box::new(server_stream), Some(Box::new(Add {}))).await;\n    }\n    \n## Init JSON-RPC client\n\n    let url = url::Url::parse(\"ws://127.0.0.1:9002/\").unwrap();\n    let client_stream = ClientObjectStream::connect(url)\n        .await\n        .expect(\"cannot generate object stream\");\n\n    let conn_arc =\n        JsonRpc2::\u003c_, ResponseResult, ErrorData\u003e::new(Box::new(client_stream), None).await;\n    \n## Call the function\n\n    match conn_arc.call(\"add\", Some(vec![2u32, 3u32, 4u32])).await {\n        Ok(response) =\u003e {\n            let result = response.result.unwrap();\n            assert_eq!(result, 9);\n        }\n        Err(err) =\u003e {\n            log::error!(\"call add error: {}\", err);\n        }\n    }\n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharlanc%2Fjsonrpc2-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharlanc%2Fjsonrpc2-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharlanc%2Fjsonrpc2-rs/lists"}