代码拉取完成,页面将自动刷新
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"gitee.com/aywc_1/aywc_judge/src/mqtt/lib"
MQTT "github.com/eclipse/paho.mqtt.golang"
"io/ioutil"
"os"
"time"
)
func main() {
// set the device info, include product key, device name, and device secret
var productKey = "a1Wi5Y7Wxh1"
var deviceName = "abc_01"
var deviceSecret = "5c715ef08fb20c888325dfbc31d29cce"
// set timestamp, clientid, subscribe topic and publish topic
var timeStamp = "1528018257135"
var clientId = "go_device_id_0001"
var pubTopic = "/" + productKey + "/" + deviceName + "/user/get"
// var subTopic string = "/" + productKey + "/" + deviceName + "/user/update1";
// set the login broker url
var raw_broker bytes.Buffer
raw_broker.WriteString("tls://")
raw_broker.WriteString(productKey)
raw_broker.WriteString(".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883")
opts := MQTT.NewClientOptions().AddBroker(raw_broker.String())
// calculate the login auth info, and set it into the connection options
auth := lib.Calculate_sign(clientId, productKey, deviceName, deviceSecret, timeStamp)
opts.SetClientID(auth.MqttClientId)
opts.SetUsername(auth.Username)
opts.SetPassword(auth.Password)
opts.SetKeepAlive(60 * 2 * time.Second)
opts.SetDefaultPublishHandler(f)
// set the tls configuration
//tlsconfig := NewTLSConfig()
//opts.SetTLSConfig(tlsconfig)
// create and start a client using the above ClientOptions
c := MQTT.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
// publish 5 messages to pubTopic("/a1Zd7n5yTt8/deng/user/update")
for i := 0; i < 5; i++ {
fmt.Println("publish msg:", i)
text := fmt.Sprintf("ABC #%d", i)
fmt.Println("pubTopic", pubTopic)
token := c.Publish("/ota/device/upgrade/a1Wi5Y7Wxh1/abc_01", 0, false, text)
fmt.Println("publish msg: ", text)
token.Wait()
time.Sleep(2 * time.Second)
}
time.Sleep(200 * time.Second)
//c.Disconnect(250)
}
func NewTLSConfig() *tls.Config {
// Import trusted certificates from CAfile.pem.
// Alternatively, manually add CA certificates to default openssl CA bundle.
certpool := x509.NewCertPool()
fmt.Println(os.Getwd())
pemCerts, err := ioutil.ReadFile("./src/mqtt/x509/root.pem")
if err != nil {
fmt.Println("0. read file error, game over!!")
}
certpool.AppendCertsFromPEM(pemCerts)
// Create tls.Config with desired tls properties
return &tls.Config{
// RootCAs = certs used to verify server cert.
RootCAs: certpool,
// ClientAuth = whether to request cert from server.
// Since the server is set up for SSL, this happens
// anyways.
ClientAuth: tls.NoClientCert,
// ClientCAs = certs used to validate client cert.
ClientCAs: nil,
// InsecureSkipVerify = verify that cert contents
// match server. IP matches what is in cert etc.
InsecureSkipVerify: false,
// Certificates = list of certs client sends to server.
// Certificates: []tls.Certificate{cert},
}
}
// define a function for the default message handler
var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("TOPIC: %s\n", msg.Topic())
fmt.Printf("MSG: %s\n", msg.Payload())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。