当前位置: 首页>>代码示例>>Golang>>正文


Golang PublishMessage.PacketId方法代码示例

本文整理汇总了Golang中github.com/surgemq/message.PublishMessage.PacketId方法的典型用法代码示例。如果您正苦于以下问题:Golang PublishMessage.PacketId方法的具体用法?Golang PublishMessage.PacketId怎么用?Golang PublishMessage.PacketId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/surgemq/message.PublishMessage的用法示例。


在下文中一共展示了PublishMessage.PacketId方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handlePendingMessage

// 判断消息是否已读
func (this *service) handlePendingMessage(msg *message.PublishMessage, pending_status *PendingStatus) {
	// 如果QOS=0,则无需等待直接返回
	if string(msg.Topic()) == OnlineStatusChannel {
		return
	}

	// 将msg按照pkt_id,存入pending队列
	pkt_id := msg.PacketId()
	//   pending_msg := NewPendingMessage(msg)

	select {
	case <-pending_status.Done:
	// 消息已成功接收,不再等待
	case <-time.After(time.Second * MsgPendingTime):
		// 重试一次
		Log.Debugc(func() string {
			return fmt.Sprintf("(%s) receive ack %d timeout. try to resend. topic: %s", this.cid(), msg.PacketId(), msg.Topic())
		})
		this.retryPublish(msg)

		select {
		case <-pending_status.Done:
			// 重发的消息已成功接收,不再等待
		case <-time.After(time.Second * MsgPendingTime):
			// 没有回ack,放到离线队列里
			Log.Debugc(func() string {
				return fmt.Sprintf("(%s) receive ack %d timeout. send msg to offline msg queue.topic: %s", this.cid(), msg.PacketId(), msg.Topic())
				//       return fmt.Sprintf("(%s) receive ack timeout. send msg to offline msg queue.topic: %s, payload: %s", this.cid(), msg.Topic(),msg.Payload())
			})
			OfflineTopicQueueProcessor <- msg
		}
	}
	PendingQueue[pkt_id] = nil
}
开发者ID:nagae-memooff,项目名称:surgemq,代码行数:35,代码来源:process.go

示例2: processPublish

// For PUBLISH message, we should figure out what QoS it is and process accordingly
// If QoS == 0, we should just take the next step, no ack required
// If QoS == 1, we should send back PUBACK, then take the next step
// If QoS == 2, we need to put it in the ack queue, send back PUBREC
func (this *service) processPublish(msg *message.PublishMessage) error {
	switch msg.QoS() {
	case message.QosExactlyOnce:
		this.sess.Pub2in.Wait(msg, nil)

		resp := message.NewPubrecMessage()
		resp.SetPacketId(msg.PacketId())

		_, err := this.writeMessage(resp)

		err = this.preDispatchPublish(msg)
		return err

	case message.QosAtLeastOnce:
		resp := message.NewPubackMessage()
		resp.SetPacketId(msg.PacketId())

		if _, err := this.writeMessage(resp); err != nil {
			return err
		}

		err := this.preDispatchPublish(msg)
		return err
	case message.QosAtMostOnce:
		err := this.preDispatchPublish(msg)
		return err
	default:
		fmt.Printf("default: %d\n", msg.QoS())
	}

	return fmt.Errorf("(%s) invalid message QoS %d.", this.cid(), msg.QoS())
}
开发者ID:nagae-memooff,项目名称:surgemq,代码行数:36,代码来源:process.go

示例3: postPublish

// processPublish() is called when the server receives a PUBLISH message AND have completed
// the ack cycle. This method will get the list of subscribers based on the publish
// topic, and publishes the message to the list of subscribers.
func (this *service) postPublish(msg *message.PublishMessage) (err error) {
	//   if msg.Retain() {
	//     if err = this.topicsMgr.Retain(msg); err != nil {
	//       Log.Errorc(func() string{ return fmt.Sprintf("(%s) Error retaining message: %v", this.cid(), err)})
	//     }
	//   }

	//   var subs []interface{}
	topic := string(msg.Topic())

	if !IsOnline(topic) {
		Log.Debugc(func() string {
			return fmt.Sprintf("(%s) this client is offline, send %d to offline queue.", this.cid(), msg.PacketId())
		})
		OfflineTopicQueueProcessor <- msg
		return nil
	}

	subs := _get_temp_subs()
	defer _return_temp_subs(subs)

	err = this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &subs, nil)
	if err != nil {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) Error retrieving subscribers list: %v", this.cid(), err)
		})
		return err
	}

	//   Log.Errorc(func() string{ return fmt.Sprintf("(%s) Publishing to topic %q and %d subscribers", this.cid(), string(msg.Topic()), len(this.subs))})
	//   fmt.Printf("value: %v\n", config.GetModel())
	//   done := make(chan bool)
	pending_status := NewPendingStatus(topic, msg)
	pkt_id := msg.PacketId()
	PendingQueue[pkt_id] = pending_status

	go this.handlePendingMessage(msg, pending_status)

	for _, s := range subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				Log.Errorc(func() string {
					return fmt.Sprintf("Invalid onPublish Function: %T", s)
				})
				return fmt.Errorf("Invalid onPublish Function")
			} else {
				(*fn)(msg)
				//         Log.Errorc(func() string{ return fmt.Sprintf("OfflineTopicQueue[%s]: %v, len is: %d\n", msg.Topic(), OfflineTopicQueue[string(msg.Topic())], len(OfflineTopicQueue[string(msg.Topic())]))})
			}
		}
	}

	return nil
}
开发者ID:nagae-memooff,项目名称:surgemq,代码行数:58,代码来源:process.go

示例4: retryPublish

func (this *service) retryPublish(msg *message.PublishMessage) (err error) {
	//   if msg.Retain() {
	//     if err = this.topicsMgr.Retain(msg); err != nil {
	//       Log.Errorc(func() string{ return fmt.Sprintf("(%s) Error retaining message: %v", this.cid(), err)})
	//     }
	//   }

	//   var subs []interface{}
	topic := string(msg.Topic())

	subs := _get_temp_subs()
	defer _return_temp_subs(subs)

	err = this.topicsMgr.Subscribers(msg.Topic(), msg.QoS(), &subs, nil)
	if err != nil {
		Log.Errorc(func() string {
			return fmt.Sprintf("(%s) Error retrieving subscribers list: %v", this.cid(), err)
		})
		return err
	}

	pending_status := NewPendingStatus(topic, msg)
	pkt_id := msg.PacketId()
	PendingQueue[pkt_id] = pending_status

	for _, s := range subs {
		if s != nil {
			fn, ok := s.(*OnPublishFunc)
			if !ok {
				Log.Errorc(func() string {
					return fmt.Sprintf("Invalid onPublish Function: %T", s)
				})
				return fmt.Errorf("Invalid onPublish Function")
			} else {
				(*fn)(msg)
			}
		}
	}

	return nil
}
开发者ID:nagae-memooff,项目名称:surgemq,代码行数:41,代码来源:process.go

示例5: preDispatchPublish

// 预投递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
}
开发者ID:nagae-memooff,项目名称:surgemq,代码行数:22,代码来源:process.go


注:本文中的github.com/surgemq/message.PublishMessage.PacketId方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。