func pub(c *service.Client, msg *message.PublishMessage) error { log.Printf("on publish: topic=%s payload=%s\n", string(msg.Topic()), string(msg.Payload())) pub := message.NewPublishMessage() pub.SetTopic([]byte("a/b/c/response")) pub.SetQoS(0) pub.SetPayload([]byte("Hi MQTT Server")) return c.Publish(pub, nil) }
//根据指定ID查询客户端在线状态,并推送消息 func (this *service) checkOnlineStatus(msg *message.PublishMessage) { client_id := string(msg.Payload()) online, lasttime, _ := GetOnlineStatus(client_id) payload := []byte(fmt.Sprintf(`{"client_id": "%s", "status": "%s", "since": "%s"}`, client_id, online, lasttime)) msg.SetPayload(payload) this.postPublish(msg) }
func onPublish(msg *message.PublishMessage) error { pr := &netx.PingResult{} if err := pr.GobDecode(msg.Payload()); err != nil { log.Printf("Error decoding ping result: %v\n", err) return err } log.Println(pr) return nil }
func (this *memTopics) Retain(msg *message.PublishMessage) error { this.rmu.Lock() defer this.rmu.Unlock() // So apparently, at least according to the MQTT Conformance/Interoperability // Testing, that a payload of 0 means delete the retain message. // https://eclipse.org/paho/clients/testing/ if len(msg.Payload()) == 0 { return this.rroot.rremove(msg.Topic()) } return this.rroot.rinsert(msg.Topic(), msg) }
func (this *service) onReceiveBadge(msg *message.PublishMessage) (err error) { var badge_message BadgeMessage datas := strings.Split(string(msg.Payload()), ":") // datas := strings.Split(fmt.Sprintf("%s", msg.Payload()), ":") if len(datas) != 2 { Log.Errorc(func() string { return fmt.Sprintf("(%s) invalid message payload: %s", this.cid(), msg.Payload()) }) return errors.New(fmt.Sprintf("invalid message payload: %s", msg.Payload())) } account_id := datas[0] payload_base64 := datas[1] if payload_base64 == "" { return errors.New(fmt.Sprintf("(%s) blank base64 payload, abort. %s", this.cid(), msg.Payload())) } payload_bytes, err := base64.StdEncoding.DecodeString(payload_base64) if err != nil { Log.Errorc(func() string { return fmt.Sprintf("(%s) can't decode payload: %s", this.cid(), payload_base64) }) } err = ffjson.Unmarshal([]byte(payload_bytes), &badge_message) if err != nil { Log.Errorc(func() string { return fmt.Sprintf("(%s) can't parse badge json: account_id: %s, payload: %s", this.cid(), account_id, payload_bytes) }) return } // Log.Infoc(func() string{ return fmt.Sprintf("badge: %v, type: %T\n", badge_message.Data, badge_message.Data)}) go this.processBadge(account_id, &badge_message) return }
// 预投递publish类型的消息,如果是特殊频道特殊处理,否则正常处理 func (this *service) preDispatchPublish(msg *message.PublishMessage) (err error) { switch string(msg.Topic()) { case BroadCastChannel: go OnGroupPublish(msg, this) case SendChannel: go this.onReceiveBadge(msg) case ApnPushChannel: go onAPNsPush(msg, this) case ApnInvalidTokensChannel: go getInvalidApnTokens(this) case OnlineStatusChannel: go this.checkOnlineStatus(msg) default: msg.SetPacketId(getNextPktId()) Log.Infoc(func() string { return fmt.Sprintf("(%s) process private message.pkt_id: %d, payload size: %d", this.cid(), msg.PacketId(), len(msg.Payload())) }) go this.postPublish(msg) } return }
func onPublishFunc(msg *message.PublishMessage) error { jklog.L().Infoln("Recevied on publish func") jklog.L().Infoln("name: ", msg.Name()) jklog.L().Infoln("payload: ", string(msg.Payload())) return nil }
func assertPublishMessage(t *testing.T, msg *message.PublishMessage, qos byte) { require.Equal(t, "abc", string(msg.Payload())) require.Equal(t, qos, msg.QoS()) }