How to use protobuf protocol?
edmand46 opened this issue · 6 comments
How to use protobuf protocol?
All you should do is that using protobuf protocol serializer before you call starx.Run()
starx.SetSerializer(protobuf.NewProtobufSerializer())
In addition, the second parameter of your handler method should be a struct that could be serialized/deserialized by protobuf.
func (f *Foo)Bar(s *session.Session, m *proto.SomeProtobufStruct) error {
//you game logic
s.Response(&proto.AnotherProtobufStruct{})
}
That all.
Thanks for answer!
Serializer can by only one? I can`t combine JSON for infrequently and Protobuf for often data sending?
i try to change your chat example
Server
https://gist.github.com/Edisoni/f759b9c9c076f0f31990cea943c0c64e
Client
https://gist.github.com/Edisoni/dc0f0a89b397bfec0d78d56bee23047f
Error:
https://gist.github.com/Edisoni/e42736c493ca7757f56912fad8238906
You can't combine JSON and Protobuf in the current version of StarX, but could combine for different server type.
starx.Set("server-type", func(){
starx.SetSerializer(json.NewJsonSerializer())
})
starx.Set("server-type-2", func() {
starx.SetSerializer(protobuf.NewProtobufSerializer())
})
Okay thanks, but what i doing wrong in message upper?
Seems like you struct with wrong tags
Try this:
type Message struct {
Data string `protobuf:"bytes,1,name=data"`
}