-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
53 lines (45 loc) · 1.3 KB
/
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
package ws_test
import (
"fmt"
"github.com/jakewins/reactivesocket-go/pkg/rs"
"github.com/jakewins/reactivesocket-go/pkg/transport/ws"
)
func ExampleClient() {
socket, err := ws.Dial("localhost:5678", rs.NewSetupPayload("text/json", "text/json", nil, nil))
if err != nil {
panic(err)
}
stream := socket.RequestSubscription(rs.NewPayload([]byte{1, 2, 3}, []byte{4, 5, 5}))
stream.Subscribe(rs.NewSubscriber(func(s rs.Subscription) {
s.Request(100)
}, func(next rs.Payload) {
// Handle payload
}, func(err error) {
// Handle errors..
}, func() {
// On complete
}))
}
func ExampleServer() {
server, err := ws.Listen(":0", func(setup rs.ConnectionSetupPayload, socket rs.ReactiveSocket) (*rs.RequestHandler, error) {
// Choose a request handler based on the setup payload. Each connection has its own handler.
return &rs.RequestHandler{
// See rs.RequestHandler for many other request handler types
HandleRequestSubscription: func(initial rs.Payload) rs.Publisher {
// Return any stream you like, based on the initial payload
return rs.NewEmptyPublisher()
},
}, nil
})
if err != nil {
panic(err)
}
fmt.Println("Starting server!")
go server.Serve()
fmt.Println("Shutting down..")
server.Shutdown()
server.AwaitShutdown()
// Output:
// Starting server!
// Shutting down..
}