https://www.jsonrpc.org/specification
-
start http server
import "github.com/czsilence/jsonrpc/jsonrpc2/server" svr := server.NewHttpServer("127.0.0.1", 9002, "rpc") svr.RegisterMethod("echo", func(val string) string { return fmt.Sprintf("you say: %s", val) }) svr.Serve()
-
start socket server
import "github.com/czsilence/jsonrpc/jsonrpc2/server" // listen on a tcp host svr := server.NewSocketServer("tcp4", "127.0.0.1:9003") // or linsten on a unix socket. The path to rpc.socket must be exist // svr := server.NewSocketServer("unix", "path/to/rpc.socket") svr.RegisterMethod("echo", func(val string) string { return fmt.Sprintf("you say: %s", val) }) svr.Serve()
-
test
-
- http
$ curl -d '{"jsonrpc": "2.0", "method": "echo", "params": ["42"], "id": 1}' http://127.0.0.1:9002/rpc
- http
-
- socket
$ nc 127.0.0.1 9003 {"jsonrpc": "2.0", "method": "echo", "params": ["42"], "id": 1}
- socket
-
- will get response if success:
{"id":1,"jsonrpc":"2.0","result":"you say: 42"}
- will get response if success: