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