I'm now building a tcp server, use https://github.com/ugorji/go to
serilize/deserialize all messages between clients and server, I have some
question on converting the decoded "interface" to correct types.
here's the example:
// client will send following 2 kinds of message to server
type HeartbeatMsg struct {
Msgid int
Seq int
}
type LoginMsg struct {
Msgid int
Username string
Password string
}
// handle hearbeat msg
func handlHeartbeat(msg *HeartbeatMsg , conn net.Conn) {
}
// handle login msg
func handleLogin(msg *LoginMsg,conn net.Conn) {
}
func handleMsg(conn net.Conn) {
var MsgpackHandle codec.MsgpackHandle
// create a decoder for conn
d := codec.NewDecoder(conn, &golib.MsgpackHandle)
for {
// use interface because we don't know what exactely client send to
us
var imsg interface{}
err := d.Decode(&msg)
// what can I do here to convert imsg to HeartbeatMsg or LoginMsg?
}
}
Currently, I know there is Msgid of int at the begining of client message,
I can access imsg with reflect to get that value, then make a deep copy of
imsg to destination.
But there are cases that client messages don't share a COMMON HEADER, there
should be a clean way to archive this.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.