-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_sub.go
56 lines (45 loc) · 995 Bytes
/
mqtt_sub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"fmt"
"os"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
var pMsg SubMessage
json.Unmarshal(msg.Payload(), &pMsg)
pubChan <- pMsg
}
var (
pubChan chan SubMessage
messageRecChan chan string
)
//SubMessage -
type SubMessage struct {
Type uint8 `json:"type"`
Name string `json:"name"` //can be guid or magnet link
}
//MQTTSub -
type MQTTSub struct {
MessageAlert chan SubMessage
client mqtt.Client
}
//NewSub -
//returns a channel which will fire on
// - 0: new torrent
// - 1: stop torrent
func NewSub(client mqtt.Client) MQTTSub {
pubChan = make(chan SubMessage)
if token := client.Subscribe(topicSub, 0, f); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
return MQTTSub{
MessageAlert: pubChan,
client: client,
}
}
//ShutDown -
func (mqttsub *MQTTSub) ShutDown() {
mqttsub.client.Disconnect(0)
}