feat: implement WebSocket client reconnection logic and enhance client structure
This commit is contained in:
23
main.go
23
main.go
@@ -20,11 +20,24 @@ func main() {
|
||||
defer exec.DockerContainer.RemoveContainer()
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
sqlite3.InitDB()
|
||||
client, err := wsclient.NewWebSocketClient("ws", "localhost:3001", "")
|
||||
if err != nil {
|
||||
log.Print("Error creating WebSocket client:", err)
|
||||
}
|
||||
defer client.Close()
|
||||
go func() {
|
||||
const reconnectDelay = 5 * time.Second
|
||||
for {
|
||||
client, err := wsclient.NewWebSocketClient("ws", "localhost:3001", "")
|
||||
if err != nil {
|
||||
log.Printf("WebSocket连接失败: %v, %v 后重试", err, reconnectDelay)
|
||||
time.Sleep(reconnectDelay)
|
||||
continue
|
||||
}
|
||||
|
||||
// 等待连接关闭
|
||||
<-client.Done
|
||||
client.Close()
|
||||
|
||||
log.Printf("WebSocket连接断开, %v 后重连", reconnectDelay)
|
||||
time.Sleep(reconnectDelay)
|
||||
}
|
||||
}()
|
||||
startRouter()
|
||||
for {
|
||||
time.Sleep(1000 * time.Second)
|
||||
|
||||
Reference in New Issue
Block a user