Skip to content

Commit 7623f45

Browse files
committed
coap: allow to set heartbeat option
1 parent c7215a2 commit 7623f45

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

net/coap/client.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ type dialOptions struct {
433433
errors func(err error)
434434
maxMessageSize int
435435
dialer *net.Dialer
436+
heartBeat time.Duration
436437
}
437438

438439
type DialOptionFunc func(dialOptions) dialOptions
@@ -484,6 +485,13 @@ func WithDialer(dialer *net.Dialer) DialOptionFunc {
484485
}
485486
}
486487

488+
func WithHeartBeat(heartBeat time.Duration) DialOptionFunc {
489+
return func(c dialOptions) dialOptions {
490+
c.heartBeat = heartBeat
491+
return c
492+
}
493+
}
494+
487495
func DialUDP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientCloseHandler, error) {
488496
h := NewOnCloseHandler()
489497
var cfg dialOptions
@@ -500,6 +508,9 @@ func DialUDP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientC
500508
if cfg.maxMessageSize > 0 {
501509
dopts = append(dopts, udp.WithMaxMessageSize(cfg.maxMessageSize))
502510
}
511+
if cfg.heartBeat > 0 {
512+
dopts = append(dopts, udp.WithHeartBeat(cfg.heartBeat))
513+
}
503514
if cfg.dialer != nil {
504515
dopts = append(dopts, udp.WithDialer(cfg.dialer))
505516
} else {
@@ -542,6 +553,9 @@ func DialTCP(ctx context.Context, addr string, opts ...DialOptionFunc) (*ClientC
542553
if cfg.maxMessageSize > 0 {
543554
dopts = append(dopts, tcp.WithMaxMessageSize(cfg.maxMessageSize))
544555
}
556+
if cfg.heartBeat > 0 {
557+
dopts = append(dopts, tcp.WithHeartBeat(cfg.heartBeat))
558+
}
545559
if cfg.dialer != nil {
546560
dopts = append(dopts, tcp.WithDialer(cfg.dialer))
547561
} else {
@@ -621,6 +635,9 @@ func DialTCPSecure(ctx context.Context, addr string, tlsCfg *tls.Config, opts ..
621635
if cfg.maxMessageSize > 0 {
622636
dopts = append(dopts, tcp.WithMaxMessageSize(cfg.maxMessageSize))
623637
}
638+
if cfg.heartBeat > 0 {
639+
dopts = append(dopts, tcp.WithHeartBeat(cfg.heartBeat))
640+
}
624641
if cfg.dialer != nil {
625642
dopts = append(dopts, tcp.WithDialer(cfg.dialer))
626643
} else {
@@ -664,6 +681,9 @@ func DialUDPSecure(ctx context.Context, addr string, dtlsCfg *piondtls.Config, o
664681
if cfg.maxMessageSize > 0 {
665682
dopts = append(dopts, dtls.WithMaxMessageSize(cfg.maxMessageSize))
666683
}
684+
if cfg.heartBeat > 0 {
685+
dopts = append(dopts, dtls.WithHeartBeat(cfg.heartBeat))
686+
}
667687
if cfg.dialer != nil {
668688
dopts = append(dopts, dtls.WithDialer(cfg.dialer))
669689
} else {

0 commit comments

Comments
 (0)