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) +}