// client will send following 2 kinds of message to servertype HeartbeatMsg struct { Msgid int Seq int}
type LoginMsg struct { Msgid int Username string Password string}
// handle hearbeat msgfunc handlHeartbeat(msg *HeartbeatMsg , conn net.Conn) { }
// handle login msgfunc 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 a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
switch m := msg.(type) {
case HeartbeatMsg:
// m is a HeartbeatMsg
case LoginMsg:
// m is a LoginMsg
default:
// m is some other type that we didn't name.
}
It will print "slice" NOT "struct". so I can't do type assertion.
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.