同步代码
This commit is contained in:
46
omc/omc_pack.go
Normal file
46
omc/omc_pack.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package omc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MsgBody struct {
|
||||
UID string
|
||||
RawData []byte
|
||||
MsgName string
|
||||
Msg map[string]string
|
||||
Keys []string
|
||||
}
|
||||
|
||||
// Decode
|
||||
// reqLoginAlarm;user=yiy;key=qw#$@;type=msg
|
||||
func (o *MsgBody) Decode() error {
|
||||
multi := strings.Split(string(o.RawData), ";")
|
||||
if len(multi) < 1 {
|
||||
return errors.New("invalid msg body")
|
||||
}
|
||||
for i := 1; i < len(multi); i++ {
|
||||
m := strings.Split(multi[i], "=")
|
||||
if len(m) != 2 {
|
||||
return errors.New("invalid msg body")
|
||||
}
|
||||
o.Msg[m[0]] = m[1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pack
|
||||
// reqLoginAlarm;user=yiy;key=qw#$@;type=msg
|
||||
func (o *MsgBody) Pack() error {
|
||||
var multi []string
|
||||
multi = append(multi, o.MsgName)
|
||||
for _, key := range o.Keys {
|
||||
item := fmt.Sprintf("%s=%s", key, o.Msg[key])
|
||||
multi = append(multi, item)
|
||||
}
|
||||
raw := strings.Join(multi, ";")
|
||||
o.RawData = []byte(raw)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user