starx icon indicating copy to clipboard operation
starx copied to clipboard

How to use protobuf protocol?

Open edmand46 opened this issue 8 years ago • 6 comments
trafficstars

How to use protobuf protocol?

edmand46 avatar May 06 '17 15:05 edmand46

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.

lonng avatar May 07 '17 12:05 lonng

Thanks for answer! Serializer can by only one? I can`t combine JSON for infrequently and Protobuf for often data sending?

edmand46 avatar May 07 '17 13:05 edmand46

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

edmand46 avatar May 07 '17 14:05 edmand46

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())
})

lonng avatar May 08 '17 00:05 lonng

Okay thanks, but what i doing wrong in message upper?

edmand46 avatar May 08 '17 04:05 edmand46

Seems like you struct with wrong tags

Try this:

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

lonng avatar May 08 '17 11:05 lonng