Write writes a message of the given type to the connection. Always non blocking.
(ctx context.Context, typ MessageType, p []byte)
| 196 | // Write writes a message of the given type to the connection. |
| 197 | // Always non blocking. |
| 198 | func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error { |
| 199 | err := c.write(typ, p) |
| 200 | if err != nil { |
| 201 | // Have to ensure the WebSocket is closed after a write error |
| 202 | // to match the Go API. It can only error if the message type |
| 203 | // is unexpected or the passed bytes contain invalid UTF-8 for |
| 204 | // MessageText. |
| 205 | err := fmt.Errorf("failed to write: %w", err) |
| 206 | c.setCloseErr(err) |
| 207 | c.closeWithInternal() |
| 208 | return err |
| 209 | } |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | func (c *Conn) write(typ MessageType, p []byte) error { |
| 214 | if c.isClosed() { |