From b501600fcce2f5b04884c55fe9fd6b72ae5ce22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=C3=B6ffel?= Date: Fri, 6 Jul 2018 01:09:36 +0200 Subject: [PATCH] Added ReadJSON --- recws.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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