断线重连

This commit is contained in:
shikong 2024-04-20 18:11:43 +08:00
parent 6de2adfd2a
commit 05df91f053

View File

@ -2,13 +2,16 @@ package client
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/toast.v1" "gopkg.in/toast.v1"
"gotify-client/internal/client" "gotify-client/internal/client"
"log" "log"
"net/http"
"net/url" "net/url"
"time"
"gotify-client/constants" "gotify-client/constants"
"gotify-client/pkg/config" "gotify-client/pkg/config"
@ -43,11 +46,10 @@ func Main() {
} }
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok { var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
_ = toml.GenerateConfig() _ = toml.GenerateConfig()
logger.Log().Fatalf("未找到配置文件, 已生成示例配置文件于运行路径下") logger.Log().Fatalf("未找到配置文件, 已生成示例配置文件于运行路径下")
} else {
logger.Log().Fatalf("配置解析失败 %s", err)
} }
} }
@ -66,38 +68,83 @@ func Main() {
u.RawQuery = params.Encode() u.RawQuery = params.Encode()
logger.Log().Infof("url %s", u.String()) logger.Log().Infof("url %s", u.String())
ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil) dialer := &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 10 * time.Second,
}
wsBuilder := func() (*websocket.Conn, error) {
ws, _, err := dialer.Dial(u.String(), nil)
return ws, err
}
ws, err := wsBuilder()
if err != nil { if err != nil {
logger.Log().Fatalf("ws 连接失败: %s, 请检查配置是否有误", err) logger.Log().Fatalf("ws 连接失败: %s, 请检查配置是否有误", err)
} }
defer func() { defer func() {
_ = ws.Close() err := recover()
if err != nil {
log.Println("panic:", err)
ws, _ = wsBuilder()
}
}() }()
done := make(chan struct{})
go func() { go func() {
defer close(done) hasError := false
for {
_, rawMessage, err := ws.ReadMessage() preHandler := func() {
if err != nil { if hasError {
log.Println("read:", err) for {
return if ws != nil {
_ = ws.Close()
}
log.Println("尝试断线重连...")
ws, err = wsBuilder()
if err != nil {
log.Println("ws 重连异常:", err)
hasError = true
time.Sleep(time.Second * 3)
} else {
hasError = false
break
}
}
} }
log.Printf("recv: %s", rawMessage)
data := &client.GotifyMessage{}
_ = json.Unmarshal(rawMessage, data)
message := fmt.Sprintf("%s", data.Message)
// windows 下 默认GBK中文编码转换
retTitle, _ := simplifiedchinese.GBK.NewEncoder().String(data.Title)
retMessage, _ := simplifiedchinese.GBK.NewEncoder().String(message)
notification.Title = retTitle
notification.Message = retMessage
_ = notification.Push()
} }
handler := func() {
defer func() {
_ = ws.Close()
}()
preHandler()
for {
_, rawMessage, err := ws.ReadMessage()
if err != nil {
log.Println("ws 消息接收异常:", err)
hasError = true
preHandler()
continue
}
log.Printf("recv: %s", rawMessage)
data := &client.GotifyMessage{}
_ = json.Unmarshal(rawMessage, data)
message := fmt.Sprintf("%s", data.Message)
// windows 下 默认GBK中文编码转换
retTitle, _ := simplifiedchinese.GBK.NewEncoder().String(data.Title)
retMessage, _ := simplifiedchinese.GBK.NewEncoder().String(message)
notification.Title = retTitle
notification.Message = retMessage
_ = notification.Push()
}
}
handler()
}() }()
quit := make(chan os.Signal) quit := make(chan os.Signal)