本文整理汇总了Golang中github.com/surgemq/message.PublishMessage.Encode方法的典型用法代码示例。如果您正苦于以下问题:Golang PublishMessage.Encode方法的具体用法?Golang PublishMessage.Encode怎么用?Golang PublishMessage.Encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/surgemq/message.PublishMessage
的用法示例。
在下文中一共展示了PublishMessage.Encode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: rinsert
func (this *rnode) rinsert(topic []byte, msg *message.PublishMessage) error {
// If there's no more topic levels, that means we are at the matching rnode.
if len(topic) == 0 {
l := msg.Len()
// Let's reuse the buffer if there's enough space
if l > cap(this.buf) {
this.buf = make([]byte, l)
} else {
this.buf = this.buf[0:l]
}
if _, err := msg.Encode(this.buf); err != nil {
return err
}
// Reuse the message if possible
if this.msg == nil {
this.msg = message.NewPublishMessage()
}
if _, err := this.msg.Decode(this.buf); err != nil {
return err
}
return nil
}
// Not the last level, so let's find or create the next level snode, and
// recursively call it's insert().
// ntl = next topic level
ntl, rem, err := nextTopicLevel(topic)
if err != nil {
return err
}
level := string(ntl)
// Add snode if it doesn't already exist
n, ok := this.rnodes[level]
if !ok {
n = newRNode()
this.rnodes[level] = n
}
return n.rinsert(rem, msg)
}
示例2: RetainMessage
func (this *Session) RetainMessage(msg *message.PublishMessage) error {
this.mu.Lock()
defer this.mu.Unlock()
this.rbuf = make([]byte, msg.Len())
this.Retained = message.NewPublishMessage()
if _, err := msg.Encode(this.rbuf); err != nil {
return err
}
if _, err := this.Retained.Decode(this.rbuf); err != nil {
return err
}
return nil
}