From e8c732c54c4a5053767e8e9e7bd3fc665f0c14dc Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sat, 5 Dec 2020 01:18:27 +0100 Subject: [PATCH] Fix error handling at websocket close. --- rtpconn/webclient.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index f9010cf..39000ce 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -657,13 +657,13 @@ func readMessage(conn *websocket.Conn, m *clientMessage) error { return conn.ReadJSON(&m) } -func StartClient(conn *websocket.Conn) error { +func StartClient(conn *websocket.Conn) (err error) { var m clientMessage - err := readMessage(conn, &m) + err = readMessage(conn, &m) if err != nil { conn.Close() - return err + return } if m.Type != "handshake" { @@ -674,7 +674,8 @@ func StartClient(conn *websocket.Conn) error { ), ) conn.Close() - return group.ProtocolError("client didn't handshake") + err = group.ProtocolError("client didn't handshake") + return } c := &webClient{ @@ -689,13 +690,14 @@ func StartClient(conn *websocket.Conn) error { c.writerDone = make(chan struct{}) go clientWriter(conn, c.writeCh, c.writerDone) defer func() { - var e []byte - if !isWSNormalError(err) { - var m *clientMessage - m, e = errorToWSCloseMessage(c.id, err) - if m != nil { - c.write(*m) - } + m, e := errorToWSCloseMessage(c.id, err) + if isWSNormalError(err) { + err = nil + } else if _, ok := err.(group.KickError); ok { + err = nil + } + if m != nil { + c.write(*m) } c.close(e) }()