Add ReadText method

This commit is contained in:
GeertJohan 2014-02-25 22:40:29 +01:00
parent 119002ce04
commit d56ec60076
1 changed files with 14 additions and 0 deletions

14
conn.go
View File

@ -63,6 +63,7 @@ var (
var ( var (
ErrCloseSent = errors.New("websocket: close sent") ErrCloseSent = errors.New("websocket: close sent")
ErrReadLimit = errors.New("websocket: read limit exceeded") ErrReadLimit = errors.New("websocket: read limit exceeded")
ErrWrongType = errors.New("websocket: received wrong type")
) )
type websocketError struct { type websocketError struct {
@ -729,6 +730,19 @@ func (c *Conn) ReadMessage() (messageType int, p []byte, err error) {
return messageType, p, err 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 // SetReadDeadline sets the deadline for future calls to NextReader and the
// io.Reader returned from NextReader. If the deadline is reached, the call // 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 // will fail with a timeout instead of blocking. A zero value for t means that