Read attempts to read a message from the connection. The maximum time spent waiting is bounded by the context.
(ctx context.Context)
| 131 | // Read attempts to read a message from the connection. |
| 132 | // The maximum time spent waiting is bounded by the context. |
| 133 | func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) { |
| 134 | c.closeReadMu.Lock() |
| 135 | closedRead := c.closeReadCtx != nil |
| 136 | c.closeReadMu.Unlock() |
| 137 | if closedRead { |
| 138 | return 0, nil, errors.New("WebSocket connection read closed") |
| 139 | } |
| 140 | |
| 141 | typ, p, err := c.read(ctx) |
| 142 | if err != nil { |
| 143 | return 0, nil, fmt.Errorf("failed to read: %w", err) |
| 144 | } |
| 145 | readLimit := c.msgReadLimit.Load() |
| 146 | if readLimit >= 0 && int64(len(p)) > readLimit { |
| 147 | reason := fmt.Errorf("read limited at %d bytes", c.msgReadLimit.Load()) |
| 148 | c.Close(StatusMessageTooBig, reason.Error()) |
| 149 | return 0, nil, fmt.Errorf("%w: %v", ErrMessageTooBig, reason) |
| 150 | } |
| 151 | return typ, p, nil |
| 152 | } |
| 153 | |
| 154 | func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) { |
| 155 | select { |