doutangtan6386 2014-08-01 00:32
浏览 79
已采纳

使用Gorilla RPC进行批处理JSON-RPC

Okay, so I'm working a server. It serves webpages and provides other services.

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/rpc"
    "github.com/gorilla/rpc/json"
)

type Service struct {
    Name string
}

type ServiceArgs struct {
    Str string
    Val float64
}

type ServiceReply struct {
    Message string
}

func (e *Service) DoSomething(request *http.Request, args *ServiceArgs, reply *ServiceReply) error {
    fmt.Println("DoSomething called remotely!")
    reply.Message = "OMG. It works."

    return nil
}

func main() {
    rpcHandler := rpc.NewServer()
    rpcHandler.RegisterCodec(json.NewCodec(), "application/json")
    rpcHandler.RegisterService(new(Service), "")
    http.Handle("/rpc", rpcHandler)

    http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("."))))

    fmt.Println("Listening on port 8080.")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatalln(err)
    }
}

I have an API on the server side that I want normal clients to be able to access. For this, I started learning more about JSON-RPC. This is how I call Service.DoSomething from a web browser.

var log = document.getElementById("log");
var request = new XMLHttpRequest();

var command = {
    "jsonrpc":"2.0",
    "method":"Service.DoSomething",
    "params":[{
        "Str":"pie",
        "Val":3.14
    }],
    "id":1
};

request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
        var response = JSON.parse(request.responseText).result;
        log.textContent += response["Message"];
    } else {
        console.log(request.statusText);
    }
};

request.open("POST", "/rpc", true);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify(command));

This works fine. I see "OMG. It works." in my browser when I run that JavaScript. Next, I wanted to run multiple method calls. I found that there is a way to send a "batch" of calls to the server. So I tried this from JavaScript.

var command = [
    {"jsonrpc":"2.0","method":"Service.DoSomething","params":[{"Str":"pie","Val":3.14}],"id":1},
    {"jsonrpc":"2.0","method":"Service.DoSomething","params":[{"Str":"pie","Val":3.14}],"id":2}
];

request.send(JSON.stringify(command));

However, this gives me a 400 (Bad Request). Did I screw up the syntax somewhere? Or is this a problem with gorilla/rpc?

  • 写回答

1条回答 默认 最新

  • douxia1988 2014-08-01 04:00
    关注

    According to The Gorilla Toolkit JSON-RPC overview:

    This package follows the JSON-RPC 1.0 specification:

    According to The JSON-RPC spec, and the JSON-RPC Google group discussion, 'batch' is a JSON-RPC 2.0 feature.

    It looks like Gorilla JSON-RPC doesn't understand the batched queries.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常