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


Golang Client.Publish方法代码示例

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


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

示例1: Publish

// メッセージを送信する。
func Publish(client *MQTT.Client, topic string, qos byte, retain bool, message string) {
	token := client.Publish(topic, qos, retain, message)

	if token.Wait() && token.Error() != nil {
		fmt.Printf("Publish error: %s\n", token.Error())
	}
}
开发者ID:RouGang,项目名称:mqtt-bench,代码行数:8,代码来源:mqtt-bench.go

示例2: reportEvent

func (d *Device) reportEvent(client *MQTT.Client) {
	for {
		time.Sleep(3 * time.Second)

		event := protocol.Event{}

		params, err := tlv.MakeTLVs([]interface{}{"hello event."})
		if err != nil {
			fmt.Println(err)
			return
		}

		event.Params = params
		event.Head.No = 1
		event.Head.SubDeviceid = 1
		event.Head.ParamsCount = uint16(len(params))

		payload, err := event.Marshal()
		if err != nil {
			fmt.Println(err)
			return
		}

		client.Publish("e", 1, false, payload)

	}
}
开发者ID:lg0491986,项目名称:pando-cloud,代码行数:27,代码来源:device.go

示例3: loop

func loop(reader io.Reader, client *MQTT.Client) {
	channel := make(chan data.SensorData)
	scanner := bufio.NewScanner(reader)
	go func() {
		for scanner.Scan() {
			line := strings.SplitN(scanner.Text(), ":", 2)
			if nil == sensors.Sensors[line[0]] {
				jww.DEBUG.Println(scanner.Text())
			} else {
				d := sensors.Sensors[line[0]].Parse(line[0], line[1])
				channel <- d
			}
		}
		close(channel)
	}()

	for data := range channel {
		topic := "/gowx/sample"
		buf := new(bytes.Buffer)
		encoder := json.NewEncoder(buf)
		encoder.Encode(data)
		payload := buf.Bytes()
		if token := client.Publish(topic, 0, false, payload); token.Wait() && token.Error() != nil {
			jww.ERROR.Println("Failed to send message.", token.Error())
		}
		jww.DEBUG.Printf("Publishing %s -> %s\n", topic, buf.Bytes())
	}
}
开发者ID:geoffholden,项目名称:gowx,代码行数:28,代码来源:parser.go

示例4: publishMessage

func publishMessage(client *mqtt.Client) {
	if pubTopic != "" {
		token := client.Publish(pubTopic, qos, false, pubMessage)
		token.WaitTimeout(10 * time.Second)
		if token.Error() != nil {
			infoF("FATAL: Could not publish: %v", token.Error())
			os.Exit(-1)
		}
	} else {
		info("nothing to publish")
	}
}
开发者ID:Hades32,项目名称:gomqtt,代码行数:12,代码来源:gomqtt.go

示例5: SendData

func SendData(client *MQTT.Client, endChan chan struct{}) {
	ticker := time.NewTicker(1 * time.Second)
	for {
		select {
		case <-endChan:
			return
		case <-ticker.C:
			var p Payload
			p.DataPoints.Load1, p.DataPoints.Load5, p.DataPoints.Load15 = getLoadAvg()
			p.DataPoints.CPUTemp = getCPUTemp(host.CPUTempFile())
			p.Timestamp = time.Now()
			payloadBytes, err := json.Marshal(p)
			if err == nil {
				client.Publish(config.PubTopic, 0, false, payloadBytes)
			} else {
				fmt.Println(err.Error())
			}
		}
	}
}
开发者ID:alsm,项目名称:goIoT,代码行数:20,代码来源:main.go

示例6: reportStatus

func (d *Device) reportStatus(client *MQTT.Client) {

	payloadHead := protocol.DataHead{
		Flag:      0,
		Timestamp: uint64(time.Now().Unix() * 1000),
	}
	param := []interface{}{uint8(1)}
	params, err := tlv.MakeTLVs(param)
	if err != nil {
		fmt.Println(err)
		return
	}
	sub := protocol.SubData{
		Head: protocol.SubDataHead{
			SubDeviceid: uint16(1),
			PropertyNum: uint16(1),
			ParamsCount: uint16(len(params)),
		},
		Params: params,
	}

	status := protocol.Data{
		Head:    payloadHead,
		SubData: []protocol.SubData{},
	}

	status.SubData = append(status.SubData, sub)

	payload, err := status.Marshal()
	if err != nil {
		fmt.Println(err)
		return
	}

	client.Publish("s", 1, false, payload)

}
开发者ID:lg0491986,项目名称:pando-cloud,代码行数:37,代码来源:device.go

示例7: makeHandler

func makeHandler(c *MQTT.Client, cmd string) func(event lirc.Event) {
	return func(event lirc.Event) {
		c.Publish("/lights/set", 0, false, cmd)
	}
}
开发者ID:gordyf,项目名称:golirc,代码行数:5,代码来源:ir.go


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