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


Golang ClientConn.Publish方法代码示例

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


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

示例1: receiver

func receiver(rs rs, cc *mqtt.ClientConn, mu *sync.Mutex) error {
	for {
		if ok := rs.Advance(); !ok {
			return rxerr(rs.Err())
		}
		m1 := rs.Value()

		mu.Lock()
		cc.Publish(&proto.Publish{
			Header:    proto.Header{Retain: false},
			TopicName: string(m1.Topic),
			Payload:   proto.BytesPayload(m1.Payload),
		})
		mu.Unlock()
	}
}
开发者ID:jeffallen,项目名称:mqtt,代码行数:16,代码来源:service.go

示例2: pub

func pub(i int) {
	topic := fmt.Sprintf("loadtest/%v", i)

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

	for i := 0; i < *messages; i++ {
		cc.Publish(&proto.Publish{
			Header:    proto.Header{QosLevel: proto.QosAtMostOnce},
			TopicName: topic,
			Payload:   proto.BytesPayload([]byte("loadtest payload")),
		})
	}

	cc.Disconnect()
}
开发者ID:rexpie,项目名称:mqtt,代码行数:18,代码来源:main.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.Publish方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。