lonng/starx

How to use protobuf protocol?

edmand46 opened this issue · 6 comments

How to use protobuf protocol?

lonng commented

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?

lonng commented

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?

lonng commented

Seems like you struct with wrong tags

Try this:

type Message struct {
	Data string `protobuf:"bytes,1,name=data"`
}