当前位置:   article > 正文

go 链接阿里云mqtt_golang对接阿里云mqtt

golang对接阿里云mqtt
  1. package services
  2. import (
  3. "fmt"
  4. mqtt "github.com/eclipse/paho.mqtt.golang"
  5. "github.com/pelletier/go-toml"
  6. "strconv"
  7. )
  8. var Config, _ = toml.LoadFile("./config.toml")
  9. type Mqttservices struct {
  10. }
  11. func (ctx Mqttservices)MqttClient () {
  12. conn()
  13. checkToken()
  14. client.Subscribe("Test", 1, func(c mqtt.Client, message mqtt.Message) {
  15. fmt.Printf(message.Payload())
  16. })
  17. }
  18. var (
  19. broker string
  20. port int
  21. AccessKey_ID string
  22. AccessKey_Secret string
  23. InstanceId string
  24. client mqtt.Client
  25. groupId string
  26. clientId string
  27. connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
  28. fmt.Println("Mqtt Connect success")
  29. }
  30. connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
  31. fmt.Printf("Mqtt Connect lost: %v", err)
  32. }
  33. messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  34. fmt.Printf("Mqtt Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
  35. }
  36. )
  37. func init() {
  38. broker =Config.Get("common.ali_mqtt.broker").(string)
  39. port, _ = strconv.Atoi(Config.Get("common.ali_mqtt.port").(string))
  40. AccessKey_ID = Config.Get("common.ali_mqtt.AccessKey_ID").(string)
  41. AccessKey_Secret = Config.Get("common.ali_mqtt.AccessKey_Secret").(string)
  42. InstanceId = Config.Get("common.ali_mqtt.InstanceId").(string)
  43. groupId =Config.Get("common.ali_mqtt.groupId").(string)
  44. clientId = groupId + "@@@" + Config.Get("common.ali_mqtt.deviceId").(string)
  45. }
  46. func checkToken() {
  47. if token := client.Connect(); token.Wait() && token.Error() != nil {
  48. fmt.Println(" -- ")
  49. fmt.Printf(token.Error().Error())
  50. panic(token.Error())
  51. }
  52. }
  53. func conn() {
  54. opts := mqtt.NewClientOptions()
  55. opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port))
  56. userName := "Signature" + "|" + AccessKey_ID + "|" + InstanceId
  57. password := HmacSha1(AccessKey_Secret, clientId)
  58. opts.SetClientID(clientId)
  59. opts.SetUsername(userName)
  60. opts.SetPassword(string(Decode(password)))
  61. opts.SetDefaultPublishHandler(messagePubHandler)
  62. opts.SetAutoReconnect(true)
  63. opts.SetMaxReconnectInterval(3)
  64. opts.OnConnect = connectHandler
  65. opts.OnConnectionLost = connectLostHandler
  66. client = mqtt.NewClient(opts)
  67. }

config.toml

  1. [common.mqtt]
  2. AccessKey_ID=""
  3. AccessKey_Secret=""
  4. InstanceId=""
  5. broker=""
  6. port="1883"
  7. deviceId="6e9f1aa4195f47428c253740e412187c"
  8. groupId=""

base64

  1. package services
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha1"
  5. "encoding/base64"
  6. "fmt"
  7. "io"
  8. )
  9. func Encode(data string) string {
  10. sEnc := base64.StdEncoding.EncodeToString([]byte(data))
  11. fmt.Println(sEnc) //
  12. return sEnc
  13. }
  14. func Decode(data string) []byte {
  15. sEnc := base64.StdEncoding.EncodeToString([]byte(data))
  16. sDec, err := base64.StdEncoding.DecodeString(sEnc)
  17. if err != nil {
  18. fmt.Printf("Error decoding string: %s ", err.Error())
  19. return nil
  20. }
  21. return sDec
  22. }
  23. func Sha1() {
  24. h := sha1.New()
  25. io.WriteString(h, "")
  26. fmt.Printf("%x\n", h.Sum(nil))
  27. //hmac ,use sha1
  28. key := []byte("123456")
  29. mac := hmac.New(sha1.New, key)
  30. mac.Write([]byte(""))
  31. fmt.Printf("%x\n", mac.Sum(nil))
  32. }
  33. func HmacSha1(keyStr, value string) string {
  34. key := []byte(keyStr)
  35. mac := hmac.New(sha1.New, key)
  36. mac.Write([]byte(value))
  37. //进行base64编码
  38. res := base64.StdEncoding.EncodeToString(mac.Sum(nil))
  39. return res
  40. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42893
推荐阅读
相关标签
  

闽ICP备14008679号