-
Notifications
You must be signed in to change notification settings - Fork 45
/
rsocket_example_test.go
152 lines (144 loc) · 4.65 KB
/
rsocket_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package rsocket_test
import (
"context"
"fmt"
"log"
"time"
"github.com/jjeffcaii/reactor-go/scheduler"
"github.com/rsocket/rsocket-go"
"github.com/rsocket/rsocket-go/payload"
"github.com/rsocket/rsocket-go/rx"
"github.com/rsocket/rsocket-go/rx/flux"
"github.com/rsocket/rsocket-go/rx/mono"
)
func Example() {
// Serve a server
err := rsocket.Receive().
Acceptor(func(ctx context.Context, setup payload.SetupPayload, sendingSocket rsocket.CloseableRSocket) (rsocket.RSocket, error) {
return rsocket.NewAbstractSocket(
rsocket.RequestResponse(func(msg payload.Payload) mono.Mono {
log.Println("incoming request:", msg)
return mono.Just(payload.NewString("Pong", time.Now().String()))
}),
), nil
}).
Transport(rsocket.TCPServer().SetAddr(":7878").Build()).
Serve(context.Background())
if err != nil {
panic(err)
}
// Connect to a server.
cli, err := rsocket.Connect().
SetupPayload(payload.NewString("Hello World", "From Golang")).
Transport(rsocket.TCPClient().SetHostAndPort("127.0.0.1", 7878).Build()).
Start(context.Background())
if err != nil {
panic(err)
}
defer cli.Close()
cli.RequestResponse(payload.NewString("Ping", time.Now().String())).
DoOnSuccess(func(elem payload.Payload) error {
log.Println("incoming response:", elem)
return nil
}).
Subscribe(context.Background())
}
func ExampleReceive() {
err := rsocket.Receive().
Resume(rsocket.WithServerResumeSessionDuration(30 * time.Second)).
Fragment(65535).
Acceptor(func(ctx context.Context, setup payload.SetupPayload, sendingSocket rsocket.CloseableRSocket) (rsocket.RSocket, error) {
// Handle close.
sendingSocket.OnClose(func(err error) {
log.Println("sending socket is closed")
})
// You can reject connection. For example, do some authorization.
// return nil, errors.New("ACCESS_DENY")
// Request to client.
sendingSocket.RequestResponse(payload.NewString("Ping", time.Now().String())).
DoOnSuccess(func(elem payload.Payload) error {
log.Println("response of Ping from client:", elem)
return nil
}).
SubscribeOn(scheduler.Parallel()).
Subscribe(context.Background())
// Return responser which just echo.
return rsocket.NewAbstractSocket(
rsocket.FireAndForget(func(msg payload.Payload) {
log.Println("receive fnf:", msg)
}),
rsocket.RequestResponse(func(msg payload.Payload) mono.Mono {
return mono.Just(msg)
}),
rsocket.RequestStream(func(msg payload.Payload) flux.Flux {
return flux.Create(func(ctx context.Context, s flux.Sink) {
for i := 0; i < 3; i++ {
s.Next(payload.NewString(msg.DataUTF8(), fmt.Sprintf("This is response #%04d", i)))
}
s.Complete()
})
}),
rsocket.RequestChannel(func(requests flux.Flux) flux.Flux {
return requests
}),
), nil
}).
Transport(rsocket.TCPServer().SetHostAndPort("127.0.0.1", 7878).Build()).
Serve(context.Background())
panic(err)
}
func ExampleConnect() {
cli, err := rsocket.Connect().
Resume(). // Enable RESUME.
Lease(). // Enable LEASE.
Fragment(4096).
SetupPayload(payload.NewString("Hello", "World")).
Acceptor(func(ctx context.Context, socket rsocket.RSocket) rsocket.RSocket {
return rsocket.NewAbstractSocket(
rsocket.RequestResponse(func(msg payload.Payload) mono.Mono {
return mono.Just(payload.NewString("Pong", time.Now().String()))
}),
)
}).
Transport(rsocket.TCPClient().SetAddr("127.0.0.1:7878").Build()).
Start(context.Background())
if err != nil {
panic(err)
}
defer func() {
_ = cli.Close()
}()
// Simple FireAndForget.
cli.FireAndForget(payload.NewString("This is a FNF message.", ""))
// Simple RequestResponse.
cli.RequestResponse(payload.NewString("This is a RequestResponse message.", "")).
DoOnSuccess(func(elem payload.Payload) error {
log.Println("response:", elem)
return nil
}).
Subscribe(context.Background())
var s rx.Subscription
// RequestStream with backpressure. (one by one)
cli.RequestStream(payload.NewString("This is a RequestStream message.", "")).
DoOnNext(func(elem payload.Payload) error {
log.Println("next element in stream:", elem)
s.Request(1)
return nil
}).
Subscribe(context.Background(), rx.OnSubscribe(func(ctx context.Context, s rx.Subscription) {
s.Request(1)
}))
// Simple RequestChannel.
sendFlux := flux.Create(func(ctx context.Context, s flux.Sink) {
for i := 0; i < 3; i++ {
s.Next(payload.NewString(fmt.Sprintf("This is a RequestChannel message #%d.", i), ""))
}
s.Complete()
})
cli.RequestChannel(sendFlux).
DoOnNext(func(elem payload.Payload) error {
log.Println("next element in channel:", elem)
return nil
}).
Subscribe(context.Background())
}