mirror of
https://github.com/jkstack/libagent.git
synced 2025-12-24 12:11:57 +08:00
修正websocket连接断开问题
This commit is contained in:
8
app.go
8
app.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
rt "runtime"
|
rt "runtime"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jkstack/anet"
|
"github.com/jkstack/anet"
|
||||||
@@ -22,9 +23,10 @@ type app struct {
|
|||||||
chWrite chan *anet.Msg
|
chWrite chan *anet.Msg
|
||||||
|
|
||||||
// runtime
|
// runtime
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
connected bool
|
connected bool
|
||||||
|
mWriteLock sync.Mutex
|
||||||
|
|
||||||
// monitor
|
// monitor
|
||||||
inPackets, inBytes uint64
|
inPackets, inBytes uint64
|
||||||
|
|||||||
@@ -61,7 +61,9 @@ func (app *app) write(ctx context.Context, cancel context.CancelFunc, conn *webs
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.mWriteLock.Lock()
|
||||||
err = conn.WriteMessage(websocket.TextMessage, data)
|
err = conn.WriteMessage(websocket.TextMessage, data)
|
||||||
|
app.mWriteLock.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error("write message: %v", err)
|
logging.Error("write message: %v", err)
|
||||||
return
|
return
|
||||||
@@ -89,7 +91,9 @@ func (app *app) keepalive(ctx context.Context, conn *websocket.Conn) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case <-tk.C:
|
case <-tk.C:
|
||||||
|
app.mWriteLock.Lock()
|
||||||
conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(2*time.Second))
|
conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(2*time.Second))
|
||||||
|
app.mWriteLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user