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


Golang ClientConn.Subscribe方法代码示例

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


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

示例1: sub

func sub(i int, wg *sync.WaitGroup) {
	topic := fmt.Sprintf("loadtest/%v", i)

	var cc *mqtt.ClientConn
	if cc = connect(); cc == nil {
		return
	}

	ack := cc.Subscribe([]proto.TopicQos{
		{topic, proto.QosAtLeastOnce},
	})
	if *dump {
		fmt.Printf("suback: %#v\n", ack)
	}

	go func() {
		ok := false
		count := 0
		for range cc.Incoming {
			count++
			if count == *messages {
				cc.Disconnect()
				ok = true
			}
		}

		if !ok {
			bad <- i
		}

		wg.Done()
	}()
}
开发者ID:rexpie,项目名称:mqtt,代码行数:33,代码来源:main.go

示例2: transmitter

func transmitter(topics []ifc.Topic, ss ss, cc *mqtt.ClientConn, mu *sync.Mutex) error {
	mu.Lock()
	suback := cc.Subscribe(tq(topics))
	mu.Unlock()

	if len(suback.TopicsQos) != len(topics) {
		return txerr(fmt.Errorf("suback has topic list length %v", len(suback.TopicsQos)))
	}

	// Since cc.Incoming is a channel, concurrent access to it is ok.
	for m1 := range cc.Incoming {
		if bp, ok := m1.Payload.(proto.BytesPayload); !ok {
			return txerr(fmt.Errorf("payload type %T not handled", m1.Payload))
		} else {
			m2 := ifc.Message{
				Topic:   m1.TopicName,
				Payload: []byte(bp),
			}
			err := ss.Send(m2)
			if err != nil {
				return txerr(err)
			}
		}
	}
	return nil
}
开发者ID:jeffallen,项目名称:mqtt,代码行数:26,代码来源:service.go

示例3: decode

func decode(cc *mqtt.ClientConn, src *net.UDPAddr, pkt []byte) {
	// Parse packet
	code := pkt[0]
	groupId := pkt[1]
	nodeId := pkt[2] & 0x1f
	ack := 0 // really need to decode pkt[2]
	data := pkt[3:]

	// Record the groupId -> addr mapping
	newGroup := saveGroupToAddr(groupId, src)

	// If this is a new group subscribe to the topic
	if newGroup {
		sub := []proto.TopicQos{
			{Topic: fmt.Sprintf("/rf/%d/+/tx", groupId), Qos: proto.QosAtMostOnce},
			{Topic: fmt.Sprintf("/rf/%d/+/tb", groupId), Qos: proto.QosAtMostOnce},
		}
		cc.Subscribe(sub)
	}

	// Create the topic
	if code > RF_Debug && code != RF_BootReply {
		log.Printf("Dropping UDP packet due to unprocessable code=%d", code)
		log.Printf("%#v", pkt[0:9])
		return
	}
	// handle boot protocol
	rxrb := "rx"
	kind := ""
	switch code {
	case RF_Pairing:
		rxrb = "rb"
		kind = "pairing"
	case RF_BootReq:
		rxrb = "rb"
		kind = "boot"
	}
	// handle packets with no source node id
	switch code {
	case RF_DataPush, RF_DataReq, RF_AckBcast, RF_BootReply, RF_Debug:
		nodeId = 0
	}
	// finally the topic
	topic := fmt.Sprintf("/rf/%d/%d/%s", groupId, nodeId, rxrb)

	// Create the payload
	payload, _ := json.Marshal(RFMessage{
		AsOf:   time.Now().UnixNano() / 1000000, // Javascript time: milliseconds
		Base64: base64.StdEncoding.EncodeToString(data),
		Kind:   kind,
	})

	// Send it off
	cc.Publish(&proto.Publish{
		Header:    proto.Header{QosLevel: proto.QosLevel(ack)},
		TopicName: topic,
		Payload:   proto.BytesPayload(payload),
	})

	// Log message, if appropriate
	if code == 9 {
		log.Printf("JeeUDP: %s", data)
	}
	log.Printf("MQTT PUB %s code=%d len=%d", topic, code, len(pkt))
}
开发者ID:kaaLabs15,项目名称:widuino,代码行数:65,代码来源:udpgw.go


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