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

这里需防止客户端异常断开链接导致groutine异常程序崩溃 #27

Open
cocoyes opened this issue Aug 29, 2024 · 0 comments

Comments

@cocoyes
Copy link

cocoyes commented Aug 29, 2024

func (c *Client) Read() {
go func() {
defer func() {
// 当 goroutine 退出时触发断开连接
Manager.DisConnect <- c
}()

	for {
		messageType, _, err := c.Socket.ReadMessage()
		if err != nil {
			// 检查是否是 WebSocket 连接正常关闭的错误
			if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure, websocket.CloseNoStatusReceived) {
				return
			}
			// 其他错误或网络问题也应该退出读取循环
			return
		}

		// 您可以根据需要处理不同类型的消息
		if messageType == websocket.PingMessage {
			// 对 Ping 消息做出反应,例如发送 Pong 消息
			_ = c.Socket.WriteMessage(websocket.PongMessage, nil)
		}
	}
}()

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant