當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。