Skip to content

Commit

Permalink
feat: 添加客户端断线重连
Browse files Browse the repository at this point in the history
  • Loading branch information
LainNetWork committed Apr 27, 2022
1 parent e5023ed commit 7a2cca6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/psyche.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (psycheClient *Client) StartAutoUpdate() {

// refresh 拉取最新配置
func (psycheClient *Client) refresh() error {
log.Println("刷新配置")
worktree, err := psycheClient.repo.Worktree()
if err != nil {
return err
Expand All @@ -164,6 +163,7 @@ func (psycheClient *Client) refresh() error {
return err
}
if psycheClient.currentHead != head.Hash().String() {
fmt.Println("config refreshed")
psycheClient.headMu.Lock()
psycheClient.currentHead = head.Hash().String()
psycheClient.configMap.Range(func(key, value interface{}) bool {
Expand Down
15 changes: 15 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ type Server struct {
Token string
connMu sync.Mutex
}

// RemoveConn 移除conn
func (w *Server) RemoveConn(conn *websocket.Conn) {
w.connMu.Lock()
for i, c := range w.conn {
if c.conn == conn {
w.conn = append(w.conn[:i], w.conn[i+1:]...)
break
}
}
w.connMu.Unlock()
}

type Conn struct {
projectName string
env string
Expand Down Expand Up @@ -141,6 +154,8 @@ func (w *Server) HandlerApi(conn *websocket.Conn, projectName string, env string
for {
msgType, p, err := conn.ReadMessage()
if err != nil {
//remove conn
w.RemoveConn(conn)
log.Println("与客户端连接异常!", err.Error())
break
}
Expand Down

0 comments on commit 7a2cca6

Please sign in to comment.