package go_chat_api_util

import "context"
import "github.com/smallnest/rpcx/client"
var rpcClient client.XClient

func InitRpc(addr string) error {
	d, err := client.NewPeer2PeerDiscovery("tcp@"+addr, "")
	if err != nil {
		return err
	}
	// 超时设置
	option := client.DefaultOption
	rpcClient = client.NewXClient("crmRpcService", client.Failtry, client.RandomSelect, d, option)
	return nil
}

type RespData struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func RpcCall(funcName string, body interface{}) (*RespData, error) {
	resp := new(RespData)
	err := rpcClient.Call(context.Background(), funcName, body, resp)
	if err != nil {
		log.Error("funcName:"+funcName, " err:"+err.Error(), " body:", body)
		return resp, err
	}
	return resp, nil
}

func CloseRpc() {
	rpcClient.Close()
}