mirror of
https://github.com/lwch/natpass
synced 2025-10-07 14:20:49 +08:00
客户端忽略io timeout错误,服务器端定时发心跳
This commit is contained in:
@@ -3,7 +3,6 @@ package pool
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"natpass/code/network"
|
"natpass/code/network"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -91,10 +90,6 @@ func (conn *Conn) loopRead(cancel context.CancelFunc) {
|
|||||||
for {
|
for {
|
||||||
msg, err := conn.conn.ReadMessage(conn.parent.cfg.ReadTimeout)
|
msg, err := conn.conn.ReadMessage(conn.parent.cfg.ReadTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error("read message: %v", err)
|
|
||||||
if strings.Contains(err.Error(), "i/o timeout") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var linkID string
|
var linkID string
|
||||||
|
@@ -90,3 +90,19 @@ func (c *client) closeLink(id string) {
|
|||||||
func (c *client) is(id string, idx uint32) bool {
|
func (c *client) is(id string, idx uint32) bool {
|
||||||
return c.parent.id == id && c.idx == idx
|
return c.parent.id == id && c.idx == idx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *client) keepalive() {
|
||||||
|
var msg network.Msg
|
||||||
|
msg.From = "server"
|
||||||
|
msg.To = c.parent.id
|
||||||
|
msg.ToIdx = c.idx
|
||||||
|
msg.XType = network.Msg_keepalive
|
||||||
|
for {
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
err := c.writeMessage(&msg)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error("send keepalive: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -62,6 +62,7 @@ func (h *Handler) Handle(conn net.Conn) {
|
|||||||
cli := clients.new(idx, c)
|
cli := clients.new(idx, c)
|
||||||
|
|
||||||
defer h.closeClient(cli)
|
defer h.closeClient(cli)
|
||||||
|
go cli.keepalive()
|
||||||
|
|
||||||
cli.run()
|
cli.run()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user