-
Notifications
You must be signed in to change notification settings - Fork 8
/
message_example_test.go
96 lines (91 loc) · 2.29 KB
/
message_example_test.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
package gobayeux
import (
"encoding/json"
"fmt"
)
func ExampleHandshakeRequestBuilder() {
b := NewHandshakeRequestBuilder()
if err := b.AddSupportedConnectionType(ConnectionTypeLongPolling); err != nil {
return
}
if err := b.AddVersion("1.0"); err != nil {
return
}
m, err := b.Build()
if err != nil {
return
}
jsonBytes, err := json.Marshal(m)
if err != nil {
return
}
fmt.Println(string(jsonBytes))
// Output:
// [{"channel":"/meta/handshake","version":"1.0","supportedConnectionTypes":["long-polling"]}]
}
func ExampleConnectRequestBuilder() {
b := NewConnectRequestBuilder()
if err := b.AddConnectionType(ConnectionTypeLongPolling); err != nil {
return
}
b.AddClientID("Un1q31d3nt1f13r")
m, err := b.Build()
if err != nil {
return
}
jsonBytes, err := json.Marshal(m)
if err != nil {
return
}
fmt.Println(string(jsonBytes))
// Output:
// [{"channel":"/meta/connect","clientId":"Un1q31d3nt1f13r","connectionType":"long-polling"}]
}
func ExampleSubscribeRequestBuilder() {
b := NewSubscribeRequestBuilder()
if err := b.AddSubscription("/foo/**"); err != nil {
return
}
if err := b.AddSubscription("/foo/**"); err != nil { // NOTE We de-duplicate channels
return
}
if err := b.AddSubscription("/bar/foo"); err != nil {
return
}
b.AddClientID("Un1q31d3nt1f13r")
m, err := b.Build()
if err != nil {
return
}
jsonBytes, err := json.Marshal(m)
if err != nil {
return
}
fmt.Println(string(jsonBytes))
// Output:
// [{"channel":"/meta/subscribe","clientId":"Un1q31d3nt1f13r","subscription":"/foo/**"},{"channel":"/meta/subscribe","clientId":"Un1q31d3nt1f13r","subscription":"/bar/foo"}]
}
func ExampleUnsubscribeRequestBuilder() {
b := NewUnsubscribeRequestBuilder()
if err := b.AddSubscription("/foo/**"); err != nil {
return
}
if err := b.AddSubscription("/foo/**"); err != nil { // NOTE We de-duplicate channels
return
}
if err := b.AddSubscription("/bar/foo"); err != nil {
return
}
b.AddClientID("Un1q31d3nt1f13r")
m, err := b.Build()
if err != nil {
return
}
jsonBytes, err := json.Marshal(m)
if err != nil {
return
}
fmt.Println(string(jsonBytes))
// Output:
// [{"channel":"/meta/unsubscribe","clientId":"Un1q31d3nt1f13r","subscription":"/foo/**"},{"channel":"/meta/unsubscribe","clientId":"Un1q31d3nt1f13r","subscription":"/bar/foo"}]
}