【发布时间】:2018-05-02 19:36:11
【问题描述】:
我尝试在 php 和 go 之间通信 JSON-RPC。
此示例中的服务器 GO https://golang.org/pkg/net/rpc/
package main
import (
"errors"
"net/rpc"
"net"
"log"
"net/http"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
func main() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":4444")
if e != nil {
log.Fatal("listen error:", e)
}
// go http.Serve(l, nil)
err:= http.Serve(l, nil)
if err != nil {
log.Fatalf("Error serving: %s", err)
}
}
和来自此存储库的示例中的 php 客户端 https://github.com/ptcx/jsonrpc-client:
require 'vendor/autoload.php';
$client = new JRClient\Client('tcp', '127.0.0.1:4444');
sleep(5);
$result = $client->call('Arith.Multiply', ['A' => 3, "B" => 4], 1000);
if ($result['error']) {
echo $result['errorMsg'] . "\n";
} else {
var_dump($result['data']);
}
Bur final 我有错误:HTTP/1.1 400 Bad Request
我也试过在 php connect 之后写 sleep(5) 但没有结果?我还尝试从 false 上的 true 更改为函数 stream_set_blocking($this->sockfp, false) https://github.com/ptcx/jsonrpc-client/blob/master/src/Connection/TcpConnection.php#L69 - 没有结果。
我编写了 GO 客户端 - 它可以正常工作。
请帮助我使用我的 php 客户端
【问题讨论】:
-
一个 400 响应意味着有一个错误的请求和一个 响应,所以它不是连接或阻塞。您是否查看过每个客户的实际请求以了解它们有何不同?
-
@JimB 好主意。但是我该怎么做会有所不同,哪种方式?
-
我不确定我是否理解。这些是相当简单的请求,您应该能够通过查看它们直观地发现任何差异。