-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (80 loc) · 1.76 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
96
97
98
99
100
101
102
103
package main
import (
"context"
"fmt"
"log"
"time"
"./endpoints"
)
var (
pubnub *PubNub
pnconf *PNConfiguration
)
type PubNub struct {
pnconfig *PNConfiguration
}
func (pn *PubNub) Publish() *endpoints.Publish {
return &endpoints.Publish{}
}
func NewPubNub(pnconfig *PNConfiguration) *PubNub {
return &PubNub{pnconfig}
}
type PNConfiguration struct {
}
func main() {
pnconf = &PNConfiguration{}
pubnub = NewPubNub(pnconf)
// FirstWay()
SecondWay()
//ThirdWay()
}
// Sync() generates a synchronous endpoit call and returns both response and
// error as a result
func FirstWay() {
ok, err := pubnub.Publish().Channel("foo").Message("bar").Sync()
if err != nil {
log.Fatalf("Oooops! %s", err)
}
fmt.Println("1st way result", ok)
}
// async
func SecondWay() {
timeout := 2
ctx, cancel := context.WithTimeout(context.Background(),
time.Duration(timeout)*time.Second)
defer cancel()
ok := make(chan interface{})
err := make(chan error)
pubnub.Publish().Context(ctx).Channel("news").Success(ok).Error(err).Async()
printResult(1, ok, err, ctx)
}
// only for publish
func ThirdWay() {
timeout := 2
ctx, cancel := context.WithTimeout(context.Background(),
time.Duration(timeout)*time.Second)
defer cancel()
ok := make(chan interface{})
err := make(chan error)
ch := pubnub.Publish().Channel("news").Success(ok).Error(err).PnChannel()
go printResult(2, ok, err, ctx)
ch <- 2
ch <- 3
// TODO: don't forget to close channel to stop listener loop
close(ch)
}
func SubscribeExample() {
}
func printResult(times int, ok chan interface{}, err chan error,
ctx context.Context) {
for i := 0; i < times; i++ {
select {
case res := <-ok:
fmt.Println(res)
case er := <-err:
fmt.Println(er)
case <-ctx.Done():
fmt.Println("timeout")
}
}
}