Godot pitaya proto
在godot连接pitaya 使用Json的基础上添加proto的支持¶
修改不太大,需要服务端和客户端添加proto的处理¶
完整客户端+服务器代码,可以在我的github上下载:godot_pitaya_proto.7z
客户端 proto¶
- godot protobuff 在 github上面的链接Godot Protobuf GDScript 插件
- 可以自行下载,安装使用
服务端本身pitaya有这方面的提供,可以自行查找,安装golang的proto,这里不多说明¶
进入正题¶
特别提醒一句¶
// 服务器的
conf.Handler.Messages.Compression = false
// 这里是客户端没有提供压缩的方式只好关闭处理
// 虽然客户端添加了两个函数,但是是真没测试过在
// client.gd
// class Compression: 类下面
消息测试使用proto¶
gp_common.proto¶
syntax = "proto3";
package proto;
option go_package = ".";
enum ErrCode {
OK = 0;
ERR = -1;
}
message CmReqHello{
int32 key = 1;
string msg = 2;
}
message CmRepHello{
ErrCode err = 1;
int32 key = 2;
string msg = 3;
}
客户端代码增加的部分¶
const pb = preload("res://protos/gp_common.proto.gd");
func _on_rep_sayhello(succ:bool, msg:Variant, rt:String) -> void:
if (!succ):
print("err not get message rep:",msg, rt);
return;
var data:pb.CmRepHello = pb.CmRepHello.new();
data.ParseFromBytes(msg.Data);
print("err:",data.err," msg:",data.msg);
pass
func _on_btn_send_proto_pressed() -> void:
var data = pb.CmReqHello.new();
data.key = 1;
data.msg = "hello server i am client";
var bytes = data.SerializeToBytes()
client.SendRequest("room.sayhello", bytes, _on_rep_sayhello);
pass # Replace with function body.
服务器代码¶
main.go¶
// 增加的部分
func (r *Room) SayHello(ctx context.Context, msg []byte) ([]byte, error) {
req := &pb.CmReqHello{}
err := proto.Unmarshal(msg, req)
if err != nil {
fmt.Println("error parse msg fail:", err)
return nil, errors.New("parse req msg fail")
}
if req.GetKey() == 1 {
fmt.Println("get client sayhello:", req.GetMsg())
}
//&pb.Response{Code: 200, Msg: "ok"}
rep := &pb.CmRepHello{}
rep.Err = pb.ErrCode_OK
rep.Key = req.Key
rep.Msg = "hello world"
bytes, err := proto.Marshal(rep)
if err != nil {
fmt.Println("error parse msg fail:", err)
return nil, errors.New("fail to rep msg")
}
return bytes, nil
}