-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (77 loc) · 2.03 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"io"
"net/http"
"time"
"github.com/notnotquinn/go-websub"
)
var log = websub.Logger()
func main() {
// initialize subscriber, hub, and publisher.
baseURL := "http://localhost:3033"
mux := http.NewServeMux()
s := websub.NewSubscriber(
baseURL+"/sub/",
websub.SubscriberWithLeaseLength(time.Hour),
)
p := websub.NewPublisher(
baseURL+"/topic/",
baseURL+"/hub/",
// websub.PublisherWithPostBodyAsContent(true),
// websub.PublisherAdvertiseInvalidTopics(true),
)
h := websub.NewHub(
baseURL+"/hub/",
// websub.HubAllowPostBodyAsContent(true),
websub.HubWithHashFunction("sha256"),
)
// register handlers
mux.Handle("/sub/", http.StripPrefix("/sub", s))
mux.Handle("/topic/", http.StripPrefix("/topic", p))
mux.Handle("/hub/", http.StripPrefix("/hub", h))
// listen for requests
go http.ListenAndServe("127.0.0.1:3033", mux)
fmt.Println("Listening on 127.0.0.1:3033")
// publish every 6 seconds
go func() {
ticker := time.NewTicker(time.Second * 6)
i := 0
for {
fmt.Println("\n--Publish.", time.Now().Unix())
err := p.Publish(
baseURL+"/topic/count",
"text/plain",
[]byte("count "+fmt.Sprint(i)),
)
if err != nil {
log.Err(err).Msg("could not publish")
}
i++
<-ticker.C
}
}()
time.Sleep(time.Second * 5)
fmt.Println("Subscribing!")
// Important: You must publish at least once before subscribing
// unless you use websub.PublisherAdvertiseInvalidTopics(true) on the publisher
// otherwise you will be unable to subscribe. (because the topic doesnt exist)
// subscribe to a topic
_, err := s.Subscribe(
baseURL+"/topic/count",
"random secret string",
func(sub *websub.SubscriberSubscription, contentType string, body io.Reader) {
fmt.Printf("Topic %s updated. %v\n", sub.Topic, time.Now().Unix())
fmt.Printf("contentType: %v\n", contentType)
bytes, err := io.ReadAll(body)
if err != nil {
panic(err)
}
fmt.Printf("string(bytes): %v\n", string(bytes))
},
)
if err != nil {
panic(err)
}
<-make(chan struct{})
}