-
Notifications
You must be signed in to change notification settings - Fork 45
/
doc.go
74 lines (57 loc) · 1.72 KB
/
doc.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
/*
Package rpcc provides an RPC client connection with support for the
JSON-RPC 2.0 specification, not including Batch requests. Server side
RPC notifications are also supported.
Dial connects to an RPC server listening on a websocket using the
gorilla/websocket package.
conn, err := rpcc.Dial("ws://127.0.0.1:9999/f39a3624-e972-4a77-8a5f-6f8c42ef5129")
// ...
The user must close the connection when finnished with it:
conn, err := rpcc.Dial("ws://127.0.0.1:9999/f39a3624-e972-4a77-8a5f-6f8c42ef5129")
if err != nil {
// Handle error.
}
defer conn.Close()
// ...
A custom dialer can be used to change the websocket lib or communicate
over other protocols.
netDial := func(ctx context.Context, addr string) (io.ReadWriteCloser, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
// Handle error.
}
// Wrap connection to handle writing JSON.
// ...
return conn, nil
}
conn, err := rpcc.Dial("127.0.0.1:9999", rpcc.WithDialer(netDial))
// ...
Communicating with the server
Send a request using Invoke:
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := rpcc.Invoke(ctx, "Domain.method", args, reply, conn)
// ...
Receive a notification using NewStream:
stream, err := rpcc.NewStream(ctx, "Domain.event", conn)
if err != nil {
// Handle error.
}
err = stream.RecvMsg(&reply)
if err != nil {
// Handle error.
}
The stream should be closed when it is no longer used to avoid leaking
memory:
stream, err := rpcc.NewStream(ctx, "Domain.event", conn)
if err != nil {
// Handle error.
}
defer stream.Close()
When order is important, two streams can be synchronized with Sync:
err := rpcc.Sync(stream1, stream2)
if err != nil {
// Handle error.
}
*/
package rpcc