增加keepalive

This commit is contained in:
lwch
2021-08-12 18:33:33 +08:00
parent 46e1f30d24
commit d9497bb34c
2 changed files with 22 additions and 0 deletions
+14
View File
@@ -39,6 +39,8 @@ func (c *Client) Run() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go c.keepalive(ctx)
for _, t := range c.cfg.Tunnels {
if t.Type == "tcp" {
go c.handleTcpTunnel(ctx, t)
@@ -110,6 +112,18 @@ func (c *Client) handleTcpTunnel(ctx context.Context, t global.Tunnel) {
}
}
func (c *Client) keepalive(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
}
time.Sleep(10 * time.Second)
c.sendKeepalive()
}
}
// handleUdpTunnel local listen to udp tunnel
func (c *Client) handleUdpTunnel(ctx context.Context, t global.Tunnel) {
// TODO
+8
View File
@@ -98,3 +98,11 @@ func (c *Client) disconnect(id, to string) {
delete(c.tunnels, id)
c.Unlock()
}
func (c *Client) sendKeepalive() {
var msg network.Msg
msg.From = c.cfg.ID
msg.To = "server"
msg.XType = network.Msg_keepalive
c.conn.WriteMessage(&msg, time.Second)
}