golang代码: package main import ( "context" "net" "net/rpc" "GitHub.com/powerman/rpc-codec/JSONrpc2" )
golang代码:
package main
import (
"context"
"net"
"net/rpc"
"GitHub.com/powerman/rpc-codec/JSONrpc2"
)
type App struct{}
type Res struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
func (*App) Hi(mp map[string]any, res *Res) error {
res.Code = 200
res.Msg = "成功"
var rmp = make(map[string]any, 0)
if v, ok := mp["name"].(string); ok {
rmp["name"] = "my name is " + v
} else {
rmp["name"] = "my name is unknown"
}
res.Data = rmp
return nil
}
type exampleContexTKEy string
var RemoteAddrContextKey exampleContextKey = "RemoteAddr"
func main() {
rpc.ReGISter(&App{})
//Server provide a tcp transport.
lnTCP, err := net.Listen("tcp", "127.0.0.1:6001")
if err != nil {
panic(err)
}
defer lnTCP.Close()
for {
conn, err := lnTCP.Accept()
if err != nil {
return
}
ctx := context.WithValue(context.Background(), RemoteAddrContextKey, conn.RemoteAddr())
Go jsonrpc2.ServeConnContext(ctx, conn)
}
}
PHP代码:
composer require tivoka/tivoka
php
namespace app\index\controller;
use app\BaseController;
use think\facade\View;
use Tivoka\Client;
class Index extends BaseController
{
public function index()
{
//tcp
$connection = Client::connect(array('host' => '127.0.0.1', 'port' => 6001));
$connection->useSpec('2.0');
$request = $connection->sendRequest('App.Hi', ['name'=>'ceshi222']);
dd($request->result);
}
}
来源地址:https://blog.csdn.net/qq_32421489/article/details/131959503
--结束END--
本文标题: php-golang-jsonrpc2.0 rpc-codec/jsonrpc2和tivoka/tivoka实践
本文链接: https://lsjlt.com/news/397376.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-05
2024-04-05
2024-04-05
2024-04-04
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0