JSON RPC客户端Go [英] JSON RPC Client Go

查看:203
本文介绍了JSON RPC客户端Go的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过JSON-RPC提供响应的python服务器.这是来自服务器的示例响应.

'{"jsonrpc": "2.0", "result": "Peer 1: local 10.10.0.2  remote 10.10.0.1  state CONNECT\\nPeer 2: local 10.10.0.18  remote 10.10.0.17  state ESTABLISHED\\nPeer 3: local 10.10.0.10  remote 10.10.0.9  state ESTABLISHED", "id": "839732f9-cf36-46ff-8b9b-6120250d9ce5"}'

这是我需要发送到服务器的请求:

'{"method":"echo","jsonrpc":"2.0","params":["test_params"],"id":"839732f9-cf36-46ff-8b9b-6120250d9ce5"}'

这是我使用go语言的客户:

package main

import (
    "fmt"
    "log"
    "net"
    "net/rpc/jsonrpc"
)
type Args struct {
    jsonrpc, id string
}

func main() {
    conn, err := net.Dial("tcp", "11.21.22.221:8080")
    if err != nil {
        panic(err)
    }
    defer conn.Close()

    args := Args{"2.0", "d87198f0-af92-49f8-9a7d-ab8bed5c4d17"}
    var reply string

    c := jsonrpc.NewClient(conn)

    err = c.Call("echo", args, &reply)
    if err != nil {
        log.Fatal("error:", err)
    }
    fmt.Printf("Response: %d", reply)
}

但是,当我运行此客户端时,它没有在参数中发送任何内容.相反,它发送的是这样的空参数:

'{"method":"echo","params":[{}],"id":0}\n

有人可以帮助我说出我在犯什么错误吗?我是上语言的新手.

谢谢.

解决方案

我认为使用go提供的客户端无法实现您的操作,因为私有clientRequest结构当前定义为:

    type clientRequest struct {
        Method string         `json:"method"`
        Params [1]interface{} `json:"params"`
        Id     uint64         `json:"id"`
    }

您将作为args传递给Call的内容固定在Params中,并注意该结构内部没有"Version`json:"jsonrpc"`".

AFAICT(可能是错误的,这是我第一次阅读此代码),您需要实现自己的 ClientCodec .您可能可以避免从stdlib中复制大部分(全部)部分,并将字段添加到上面的clientRequest中. ;-)

I have a python server serving response through JSON-RPC. Here is a sample response from the server.

'{"jsonrpc": "2.0", "result": "Peer 1: local 10.10.0.2  remote 10.10.0.1  state CONNECT\\nPeer 2: local 10.10.0.18  remote 10.10.0.17  state ESTABLISHED\\nPeer 3: local 10.10.0.10  remote 10.10.0.9  state ESTABLISHED", "id": "839732f9-cf36-46ff-8b9b-6120250d9ce5"}'

Here is the request I need to send to the server:

'{"method":"echo","jsonrpc":"2.0","params":["test_params"],"id":"839732f9-cf36-46ff-8b9b-6120250d9ce5"}'

Here is my client with go language:

package main

import (
    "fmt"
    "log"
    "net"
    "net/rpc/jsonrpc"
)
type Args struct {
    jsonrpc, id string
}

func main() {
    conn, err := net.Dial("tcp", "11.21.22.221:8080")
    if err != nil {
        panic(err)
    }
    defer conn.Close()

    args := Args{"2.0", "d87198f0-af92-49f8-9a7d-ab8bed5c4d17"}
    var reply string

    c := jsonrpc.NewClient(conn)

    err = c.Call("echo", args, &reply)
    if err != nil {
        log.Fatal("error:", err)
    }
    fmt.Printf("Response: %d", reply)
}

But, when I run this client, it is not sending anything in the params. Instead it is sending empty params like this:

'{"method":"echo","params":[{}],"id":0}\n

Can somebody help me telling what mistake I am making? I am a newbie to go language.

Thanks.

解决方案

I don't think what you are doing is possible using the client as provided by go because the private clientRequest struct is currently defined as:

    type clientRequest struct {
        Method string         `json:"method"`
        Params [1]interface{} `json:"params"`
        Id     uint64         `json:"id"`
    }

What you pass into Call as args is stuck into Params and note how there is no "Version `json:"jsonrpc"`" inside of that struct.

AFAICT (which may be wrong, this is my first time reading through this code) you would need to implement your own ClientCodec. You could probably get away with copying most (all) of the parts out of the stdlib and add the field to the clientRequest above. ;-)

这篇关于JSON RPC客户端Go的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆