Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw websocket error #652

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions server/api/studio/pkg/ws/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (c *Client) Destroy() {
c.Hub.unregister <- c
}

func (c *Client) SendCloseMessage(closeCode int, reason string) error {
msg := websocket.FormatCloseMessage(closeCode, reason)
return c.Conn.WriteControl(websocket.CloseMessage, msg, time.Now().Add(time.Second))
}

// readPump pumps messages from the websocket connection to the hub.
//
// The application runs readPump in a per-connection goroutine. The application
Expand All @@ -160,7 +165,9 @@ func (c *Client) readPump() {
} else {
logx.Errorf("[WebSocket ReadMessage]: %v", err)
}
break

c.SendCloseMessage(websocket.CloseNormalClosure, err.Error())
return
}

msgReceivedByte := bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
Expand Down Expand Up @@ -202,7 +209,7 @@ func (c *Client) writePump() {
if !ok {
// The hub closed the channel.
logx.Errorf("[WebSocket writePump]: c.send length: %v", len(c.send))
c.Conn.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(time.Second))
c.SendCloseMessage(websocket.CloseInternalServerErr, "client send channel error")
return
}

Expand All @@ -213,19 +220,27 @@ func (c *Client) writePump() {

msgLen := len(message)
if wsConfig.WriteLimit > 0 && int64(msgLen) > wsConfig.WriteLimit {
errMsg := websocket.FormatCloseMessage(websocket.CloseMessageTooBig, fmt.Sprintf("message length (%d) exceeds config limit (%d)", msgLen, wsConfig.WriteLimit))
c.Conn.WriteControl(websocket.CloseMessage, errMsg, time.Now().Add(time.Second))
reason := fmt.Sprintf("message length (%d) exceeds config limit (%d)", msgLen, wsConfig.WriteLimit)
c.SendCloseMessage(websocket.CloseMessageTooBig, reason)
return
}

w, err := c.Conn.NextWriter(websocket.TextMessage)
if err != nil {
c.SendCloseMessage(websocket.CloseInternalServerErr, err.Error())
logx.Errorf("[WebSocket WriteMessage]: %v", err)
return
}

_, err = w.Write(message)
if err != nil {
c.SendCloseMessage(websocket.CloseInternalServerErr, err.Error())
logx.Errorf("[WebSocket WriteMessage]: %v", err)
return
}
w.Write(message)

if err := w.Close(); err != nil {
c.SendCloseMessage(websocket.CloseInternalServerErr, err.Error())
logx.Errorf("[WebSocket WriteMessage Close]: %v", err)
return
}
Expand All @@ -235,6 +250,7 @@ func (c *Client) writePump() {
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
}
if err := c.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
c.SendCloseMessage(websocket.CloseInternalServerErr, err.Error())
logx.Errorf("[WebSocket ticker error]: %v", err)
return
}
Expand Down