Skip to content

chenz85/jsonrpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Go implement of JSON-RPC

What is JSON-RPC

https://www.jsonrpc.org/specification

Server

  • 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
    • socket
      $ nc 127.0.0.1 9003
      {"jsonrpc": "2.0", "method": "echo", "params": ["42"], "id": 1}
    • will get response if success:
      {"id":1,"jsonrpc":"2.0","result":"you say: 42"}

Client

TBD