其他分享
首页 > 其他分享> > go 语言 HTTP协议上提供jsonrpc服务

go 语言 HTTP协议上提供jsonrpc服务

作者:互联网

 

 

package main

import (
    "io"
    "net/http"
    "net/rpc"
    "net/rpc/jsonrpc"
)

type HelloService struct {
}

func (p *HelloService) Hello(request string, reply *string) error {
    *reply = "Hello:" + request
    return nil

}

func main() {
    rpc.RegisterName("HelloService", new(HelloService))
    http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {

        var conn io.ReadWriteCloser = struct {
            io.Writer
            io.ReadCloser
        }{
            w,
            r.Body,
        }

        rpc.ServeRequest(jsonrpc.NewServerCodec(conn))

    })

    http.ListenAndServe(":7777", nil)

}

 

 

标签:jsonrpc,http,rpc,HelloService,io,go,HTTP,net
来源: https://www.cnblogs.com/zexin88/p/16228913.html