php-golang-jsonrpc2.0 rpc-codec/jsonrpc2和tivoka/tivoka实践

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 mapstringany, res *Res) error {

res.Code = 200

res.Msg = "成功"

var rmp = make(mapstringany, 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);

}

}

相关推荐
ZJU_统一阿萨姆6 分钟前
【算子开发】扫描(Scan)与前缀和
开发语言·arm开发·架构·系统架构·硬件架构
-凌凌漆-8 分钟前
【C语言】结构体typedef struct与struct的区别
c语言·开发语言
for_ever_love__36 分钟前
PySpark学习: 数据输出
开发语言·python·学习·spark
言乐61 小时前
Python实现自动拉人脚本示例
开发语言·windows·python·django·pygame
Sagittarius_A*1 小时前
【好靶场】PHP反序列化入门练习
安全·php·web·反序列化
啊啊啊啊啊!!!!1 小时前
【c++】二叉搜索树
开发语言·c++
2601_965798471 小时前
How to Build a Scalable Classified Directory with Classima Theme
php·theme
归去来 兮1 小时前
Python爬虫爬取数据案例
开发语言·爬虫·python
ShineWinsu1 小时前
对于 C++:C++20中Concept(概念) 与 Coroutine(协程)的解析
linux·开发语言·网络·c++·c++20·epoll
ly76891 小时前
C 语言从入门到进阶:语法、指针、内存管理与工程实践详解
c语言·开发语言