diff --git a/recws.go b/recws.go index 2b0c28a..63c91aa 100644 --- a/recws.go +++ b/recws.go @@ -117,6 +117,25 @@ func (rc *RecConn) WriteJSON(v interface{}) error { return err } +// ReadJSON reads the next JSON-encoded message from the connection and stores +// it in the value pointed to by v. +// +// See the documentation for the encoding/json Unmarshal function for details +// about the conversion of JSON to a Go value. +// +// If the connection is closed ErrNotConnected is returned +func (rc *RecConn) ReadJSON(v interface{}) error { + err := ErrNotConnected + if rc.IsConnected() { + err = rc.Conn.ReadJSON(v) + if err != nil { + rc.closeAndReconnect() + } + } + + return err +} + // Dial creates a new client connection. // The URL url specifies the host and request URI. Use requestHeader to specify // the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies