From d56ec60076cf82fd285d6cfb8c393a203e95a9e4 Mon Sep 17 00:00:00 2001 From: GeertJohan Date: Tue, 25 Feb 2014 22:40:29 +0100 Subject: [PATCH] Add ReadText method --- conn.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/conn.go b/conn.go index 050d8bc..41e2127 100644 --- a/conn.go +++ b/conn.go @@ -63,6 +63,7 @@ var ( var ( ErrCloseSent = errors.New("websocket: close sent") ErrReadLimit = errors.New("websocket: read limit exceeded") + ErrWrongType = errors.New("websocket: received wrong type") ) type websocketError struct { @@ -729,6 +730,19 @@ func (c *Conn) ReadMessage() (messageType int, p []byte, err error) { return messageType, p, err } +// ReadText reads a new text message from the websocket. +// When retrieved message was not of type Text: ErrWrongType is returned. +func (c *Conn) ReadText() (msg string, err error) { + messageType, p, err := c.ReadMessage() + if err != nil { + return "", err + } + if messageType != TextMessage { + return "", ErrWrongType + } + return string(p), nil +} + // SetReadDeadline sets the deadline for future calls to NextReader and the // io.Reader returned from NextReader. If the deadline is reached, the call // will fail with a timeout instead of blocking. A zero value for t means that