-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathmain.go
68 lines (60 loc) · 1.51 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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"log"
"sync"
"sync/atomic"
cews "github.com/cloudevents/sdk-go/protocol/ws/v2"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
func main() {
ctx := context.Background()
p, err := cews.Dial(ctx, "http://localhost:8080", nil)
if err != nil {
log.Fatalf("failed to dial: %v", err)
}
c, err := cloudevents.NewClient(p, cloudevents.WithTimeNow(), cloudevents.WithUUIDs())
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
var wg sync.WaitGroup
wg.Add(2)
go func() {
for i := 0; i < 10; i++ {
e := cloudevents.NewEvent()
e.SetType("com.cloudevents.sample.sent")
e.SetSource("https://github.com/cloudevents/sdk-go/v2/samples/stan/sender")
_ = e.SetData(cloudevents.ApplicationJSON, map[string]interface{}{
"id": i,
"message": "Hello, World!",
})
if result := c.Send(context.Background(), e); cloudevents.IsUndelivered(result) {
log.Printf("Failed to send: %v", err)
} else {
log.Printf("Sent: %d, accepted: %t", i, cloudevents.IsACK(result))
}
}
wg.Done()
}()
go func() {
received := uint32(0)
ctx, cancel := context.WithCancel(ctx)
err := c.StartReceiver(ctx, func(event cloudevents.Event) {
log.Printf("Received event:\n%v", event)
if atomic.AddUint32(&received, 1) == 10 {
cancel()
}
})
if err != nil {
log.Printf("failed to start receiver: %v", err)
} else {
<-ctx.Done()
}
wg.Done()
}()
wg.Wait()
}