-
Notifications
You must be signed in to change notification settings - Fork 96
/
stream.go
42 lines (36 loc) · 1.13 KB
/
stream.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
package deribit
import (
"context"
"io"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
// A ObjectStream is a jsonrpc2.ObjectStream that uses a WebSocket to
// send and receive JSON-RPC 2.0 objects.
type ObjectStream struct {
conn *websocket.Conn
}
// NewObjectStream creates a new jsonrpc2.ObjectStream for sending and
// receiving JSON-RPC 2.0 objects over a WebSocket.
func NewObjectStream(conn *websocket.Conn) ObjectStream {
return ObjectStream{conn: conn}
}
// WriteObject implements jsonrpc2.ObjectStream.
func (t ObjectStream) WriteObject(obj interface{}) error {
return wsjson.Write(context.Background(), t.conn, obj)
}
// ReadObject implements jsonrpc2.ObjectStream.
func (t ObjectStream) ReadObject(v interface{}) error {
err := wsjson.Read(context.Background(), t.conn, v)
if e, ok := err.(*websocket.CloseError); ok {
if e.Code == websocket.StatusNormalClosure && e.Error() == io.ErrUnexpectedEOF.Error() {
// unwrapping this error.
err = io.ErrUnexpectedEOF
}
}
return err
}
// Close implements jsonrpc2.ObjectStream.
func (t ObjectStream) Close() error {
return t.conn.Close(websocket.StatusNormalClosure, "")
}