• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang git.MqttClient类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Publish

// メッセージを送信する。
func Publish(client *MQTT.MqttClient, 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:EriclLee,项目名称:mqtt-bench,代码行数:8,代码来源:mqtt-bench.go


示例2: Publish

func Publish(client *MQTT.MqttClient, topic, message string) error {
	topicName := config.user + "/" + topic
	qos := 0

	receipt := client.Publish(MQTT.QoS(qos), topicName, message)
	<-receipt // Publish成功を待ち受ける

	return nil
}
开发者ID:kyokomi-sandbox,项目名称:sandbox,代码行数:9,代码来源:mqtt.go


示例3: Subscribe

func Subscribe(client *MQTT.MqttClient, topic string) {
	filter, _ := MQTT.NewTopicFilter(topic, byte(MQTT.QOS_ZERO))
	if receipt, err := client.StartSubscription(f, filter); err != nil {
		fmt.Println(err)
		os.Exit(1)
	} else {
		<-receipt
	}
}
开发者ID:hakobera,项目名称:osx-cpu-temp,代码行数:9,代码来源:main.go


示例4: Publish

func Publish(client *MQTT.MqttClient, topic string, payload []byte) {
	message := MQTT.NewMessage(payload)
	message.SetQoS(0)
	receipt := client.PublishMessage(topic, message)

	fmt.Println("publish:")
	fmt.Printf("TOPIC: %s\n", topic)
	fmt.Printf("MSG: %s\n", message.Payload())

	<-receipt
}
开发者ID:hakobera,项目名称:osx-cpu-temp,代码行数:11,代码来源:main.go


示例5: reconnectMqttBroker

func reconnectMqttBroker(client *MQTT.MqttClient, opts *MQTT.ClientOptions) {
	if mqttConnected == false {
		//log.Println("Attempting reconnect . . .")
		// Always reconnect with cleansession false
		opts.SetCleanSession(false)
		_, err := client.Start()
		if err != nil {
			time.Sleep(1 * time.Second)
		} else {
			log.Info("Reconnected")
			mqttConnected = true
		}
	}
}
开发者ID:mackristof,项目名称:golang-weather-station,代码行数:14,代码来源:main.go


示例6: PostCurrentData

// post WeatherData structure on JSON to MQTT topic
func PostCurrentData(dataChannel <-chan *weatherStation.WeatherData, mqttClient *MQTT.MqttClient, opts *MQTT.ClientOptions) {
	//log.Println("start posting data")
	for {
		currentWeather := <-dataChannel
		//fmt.Printf("#########################getting data =%+v\n", currentWeather)
		jsonWeather, err := json.Marshal(currentWeather)
		//fmt.Printf("#########################sended data =%s\n", jsonWeather)
		check(err)
		msg := MQTT.NewMessage(jsonWeather)
		reconnectMqttBroker(mqttClient, opts)
		if mqttConnected {
			mqttClient.PublishMessage("/mackristof/weather-mtp/davis1", msg)
		}
	}
}
开发者ID:mackristof,项目名称:golang-weather-station,代码行数:16,代码来源:main.go


示例7: Subscribe

// メッセージを受信する。
func Subscribe(client *MQTT.MqttClient, topic string, qos byte) *SubscribeResult {
	var result *SubscribeResult = &SubscribeResult{}
	result.Count = 0

	var handler MQTT.MessageHandler = func(client *MQTT.MqttClient, msg MQTT.Message) {
		result.Count++
		if Debug {
			fmt.Printf("Received message : topic=%s, message=%s\n", msg.Topic(), msg.Payload())
		}
	}

	token := client.Subscribe(topic, qos, handler)

	if token.Wait() && token.Error() != nil {
		fmt.Printf("Subscribe error: %s\n", token.Error())
	}

	return result
}
开发者ID:EriclLee,项目名称:mqtt-bench,代码行数:20,代码来源:mqtt-bench.go


示例8: Subscribe

func Subscribe(client *MQTT.MqttClient, topic string) error {
	topicName := config.user + "/" + topic
	// 無料枠はQoS 0のみ
	qos := 0

	// Subscribeするtopicを設定します
	topicFilter, err := MQTT.NewTopicFilter(topicName, byte(qos))
	if err != nil {
		return err
	}

	// Subscribeします
	// onMessageReceived はメッセージが届いたら呼び出されるコールバックです
	_, err = client.StartSubscription(onMessageReceived, topicFilter)
	if err != nil {
		return err
	}

	// そのままではプロセスが終わってしまいますので、待ち受けます
	for {
		time.Sleep(1 * time.Second)
	}
}
开发者ID:kyokomi-sandbox,项目名称:sandbox,代码行数:23,代码来源:mqtt.go


示例9: Disconnect

// Brokerとの接続を切断する。
func Disconnect(client *MQTT.MqttClient) {
	client.ForceDisconnect()
}
开发者ID:EriclLee,项目名称:mqtt-bench,代码行数:4,代码来源:mqtt-bench.go


示例10: mainLoop

// mainLoop initiates all ports and handles the traffic
func mainLoop() {
	openPorts()
	defer closePorts()

	waitCh := make(chan bool)
	go func() {
		total := 0
		for {
			v := <-outCh
			if !v {
				log.Println("An OUT port is closed. Interrupting execution")
				exitCh <- syscall.SIGTERM
				break
			} else {
				total++
			}
			// At least one output ports are opened
			if total >= 1 && waitCh != nil {
				waitCh <- true
			}
		}
	}()

	log.Println("Waiting for options to arrive...")
	var (
		ip            [][]byte
		clientOptions *mqtt.ClientOptions
		client        *mqtt.MqttClient
		defaultTopic  string
		qos           mqtt.QoS
	)

	// Setup socket poll items
	poller := zmq.NewPoller()
	poller.Add(optsPort, zmq.POLLIN)
	poller.Add(tmplPort, zmq.POLLIN)

	for {
		sockets, err := poller.Poll(-1)
		if err != nil {
			log.Println("Error polling ports:", err.Error())
			continue
		}
		for _, socket := range sockets {
			if socket.Socket == nil {
				log.Println("ERROR: could not find socket in polling items array")
				continue
			}
			ip, err = socket.Socket.RecvMessageBytes(0)
			if err != nil {
				log.Println("Error receiving message:", err.Error())
				continue
			}
			if !runtime.IsValidIP(ip) || !runtime.IsPacket(ip) {
				log.Println("Invalid IP:", ip)
				continue
			}
			switch socket.Socket {
			case optsPort:
				err = json.Unmarshal(ip[1], &options)
				if err != nil {
					log.Println("ERROR: failed to unmarshal options:", err.Error())
					continue
				}
				clientOptions, defaultTopic, qos, err = helper.ParseOptionsURI(options.OptionsURI)
				if err != nil {
					log.Printf("Failed to parse connection uri. Error: %s", err.Error())
					continue
				}
				log.Println("Options specified:", options)
			case tmplPort:
				err = json.Unmarshal(ip[1], &propTemplate)
				if err != nil {
					log.Println("ERROR: failed to unmarshal template:", err.Error())
					continue
				}
				log.Printf("Template specified: %+v", propTemplate)

			default:
				log.Println("ERROR: IP from unhandled socket received!")
				continue
			}

			if defaultTopic != "" && clientOptions != nil && propTemplate != nil {
				log.Println("Component configured. Moving on...")
				break
			}
		}

		client = mqtt.NewClient(clientOptions)
		if _, err = client.Start(); err != nil {
			log.Printf("Failed to create MQTT client. Error: %s", err.Error())
			continue
		}

		defer client.Disconnect(1e6)

		optsPort.Close()
		tmplPort.Close()
//.........这里部分代码省略.........
开发者ID:voxadam,项目名称:cascades-caf,代码行数:101,代码来源:main.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang packets.NewControlPacket函数代码示例发布时间:2022-05-24
下一篇:
Golang git.Message类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap