From ba2acc3200934e3a0513a06b2a3e167b404aaf3f Mon Sep 17 00:00:00 2001
From: Nicolas Duhamel <nicolas@jombi.fr>
Date: Wed, 28 Dec 2022 09:45:30 +0100
Subject: [PATCH] Support Will

---
 mqtt.go | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/mqtt.go b/mqtt.go
index 31e4208..a8b3869 100644
--- a/mqtt.go
+++ b/mqtt.go
@@ -69,22 +69,9 @@ type Config struct {
 	Retained      bool
 	KeepAlive     time.Duration
 	MsgChanDept   uint
+	WillMessage   PubMessage
 }
 
-// 	return &Config{
-// 		Host:          host,
-// 		Port:          port,
-// 		Username:      username,
-// 		Password:      password,
-// 		ClientID:      clientId,
-// 		CleanSession:  true,
-// 		AutoReconnect: true,
-// 		Retained:      false,
-// 		KeepAlive:     15 * time.Second,
-// 		MsgChanDept:   100,
-// 	}
-// }
-
 type Client struct {
 	Config     Config
 	pahoClient paho.Client
@@ -103,6 +90,11 @@ func New(conf Config) *Client {
 	opts.SetAutoReconnect(conf.AutoReconnect)
 	opts.SetKeepAlive(conf.KeepAlive)
 	opts.SetMessageChannelDepth(conf.MsgChanDept)
+
+	if conf.WillMessage != nil {
+		m := conf.WillMessage
+		opts.SetBinaryWill(m.Topic(), m.Payload(), m.Qos(), m.Retained())
+	}
 	if conf.AutoReconnect {
 		opts.SetResumeSubs(true)
 	}