Workaround lack of net.ErrClosed in WebSocket library

This commit is contained in:
Simon Ser 2021-09-21 11:49:48 +02:00
parent 27ea855ea2
commit 872ed736b4

12
conn.go
View File

@ -74,8 +74,18 @@ func (wic *websocketIRCConn) WriteMessage(msg *irc.Message) error {
return wic.conn.Write(ctx, websocket.MessageText, b) return wic.conn.Write(ctx, websocket.MessageText, b)
} }
func isErrWebSocketClosed(err error) bool {
return err != nil && strings.HasSuffix(err.Error(), "failed to close WebSocket: already wrote close")
}
func (wic *websocketIRCConn) Close() error { func (wic *websocketIRCConn) Close() error {
return wic.conn.Close(websocket.StatusNormalClosure, "") err := wic.conn.Close(websocket.StatusNormalClosure, "")
// TODO: remove once this PR is merged:
// https://github.com/nhooyr/websocket/pull/303
if isErrWebSocketClosed(err) {
return nil
}
return err
} }
func (wic *websocketIRCConn) SetReadDeadline(t time.Time) error { func (wic *websocketIRCConn) SetReadDeadline(t time.Time) error {