refactor(common/cool/coolconfig): 修改RPC配置字段类型

将RPC字段从uint16类型更改为string类型的Address字段,
以支持更灵活的地址配置。同时更新了配置初始化逻辑,
从server.rpc改为server.address作为配置键。
```
This commit is contained in:
昔念
2026-01-25 03:40:29 +08:00
parent 392061df04
commit 32f57732fe
20 changed files with 166 additions and 196 deletions

View File

@@ -7,7 +7,6 @@ import (
"context"
"fmt"
"log"
"net/http"
config "blazing/modules/config/service"
@@ -15,13 +14,11 @@ import (
"github.com/gogf/gf/v2/util/gconv"
)
var rpcport = gconv.String(cool.Config.RPC)
// Define the server handler
type ServerHandler struct{}
// 实现踢人
func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
func (*ServerHandler) Kick(_ context.Context, userid uint32) error {
cool.Logger.Info(context.TODO(), "服务器收到踢人")
useid1, err := share.ShareManager.GetUserOnline(userid)
@@ -41,7 +38,7 @@ func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
}
// 注册logic服务器
func (h *ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error {
func (*ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error {
cool.Logger.Debug(context.Background(), "注册logic服务器", id, port)
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
@@ -62,18 +59,28 @@ func (h *ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) erro
}
func StartServer() {
// func StartServer() {
// // create a new server instance
// rpcServer := jsonrpc.NewServer(jsonrpc.WithReverseClient[cool.ClientHandler](""))
// rpcServer.Register("", &ServerHandler{})
// cool.Logger.Debug(context.Background(), "jsonrpc server start", rpcport)
// // go time.AfterFunc(3000, func() {
// // testjsonrpc()
// // })
// err := http.ListenAndServe("0.0.0.0:"+rpcport, rpcServer)
// cool.Logger.Debug(context.Background(), "jsonrpc server fail", err)
// }
func CServer() *jsonrpc.RPCServer {
// create a new server instance
rpcServer := jsonrpc.NewServer(jsonrpc.WithReverseClient[cool.ClientHandler](""))
rpcServer.Register("", &ServerHandler{})
cool.Logger.Debug(context.Background(), "jsonrpc server start", rpcport)
// go time.AfterFunc(3000, func() {
// testjsonrpc()
// })
err := http.ListenAndServe("0.0.0.0:"+rpcport, rpcServer)
cool.Logger.Debug(context.Background(), "jsonrpc server fail", err)
return rpcServer
// err := http.ListenAndServe("0.0.0.0:"+rpcport, rpcServer)
// // cool.Logger.Debug(context.Background(), "jsonrpc server fail", err)
}
var closer jsonrpc.ClientCloser
@@ -84,10 +91,10 @@ func StartClient(id, port uint16, callback any) *struct {
RegisterLogic func(uint16, uint16) error
} {
var rpcaddr = cool.Config.File.Domain
var rpcaddr = "ws://" + cool.Config.File.Domain + gconv.String(cool.Config.Address) + "/rpc"
//rpcaddr = "127.0.0.1"
closer1, err := jsonrpc.NewMergeClient(context.Background(),
"ws://"+rpcaddr+":"+rpcport, "", []interface{}{
rpcaddr, "", []interface{}{
&RPCClient,
}, nil, jsonrpc.WithClientHandler("", callback),
jsonrpc.WithReconnFun(func() { RPCClient.RegisterLogic(id, port) }),
@@ -97,19 +104,13 @@ func StartClient(id, port uint16, callback any) *struct {
}
//if port != 0 { //注册logic
RPCClient.RegisterLogic(id, port)
defer RPCClient.RegisterLogic(id, port)
//}
closer = closer1
return &RPCClient
}
// 关闭客户端
func CloseClient() {
if closer != nil {
closer()
}
return &RPCClient
}
// Setup RPCClient with reverse call handler