From d9497bb34cc09fa8afaa5cc6031aba1e0dfdd69e Mon Sep 17 00:00:00 2001 From: lwch Date: Thu, 12 Aug 2021 18:33:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0keepalive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/client/client.go | 14 ++++++++++++++ code/client/client/send.go | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/code/client/client/client.go b/code/client/client/client.go index 430dd02..392507d 100644 --- a/code/client/client/client.go +++ b/code/client/client/client.go @@ -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 diff --git a/code/client/client/send.go b/code/client/client/send.go index 865be92..d7767a5 100644 --- a/code/client/client/send.go +++ b/code/client/client/send.go @@ -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) +}