@@ -57,6 +57,11 @@ type ReconnectHandler func(Client, *ClientOptions)
5757// ConnectionAttemptHandler is invoked prior to making the initial connection.
5858type ConnectionAttemptHandler func (broker * url.URL , tlsCfg * tls.Config ) * tls.Config
5959
60+ // OpenConnectionFunc is invoked to establish the underlying network connection
61+ // Its purpose if for custom network transports.
62+ // Does not carry out any MQTT specific handshakes.
63+ type OpenConnectionFunc func (uri * url.URL , options ClientOptions ) (net.Conn , error )
64+
6065// ClientOptions contains configurable options for an Client. Note that these should be set using the
6166// relevant methods (e.g. AddBroker) rather than directly. See those functions for information on usage.
6267// WARNING: Create the below using NewClientOptions unless you have a compelling reason not to. It is easy
@@ -98,6 +103,7 @@ type ClientOptions struct {
98103 WebsocketOptions * WebsocketOptions
99104 MaxResumePubInFlight int // // 0 = no limit; otherwise this is the maximum simultaneous messages sent while resuming
100105 Dialer * net.Dialer
106+ CustomOpenConnectionFn OpenConnectionFunc
101107}
102108
103109// NewClientOptions will create a new ClientClientOptions type with some
@@ -140,6 +146,7 @@ func NewClientOptions() *ClientOptions {
140146 HTTPHeaders : make (map [string ][]string ),
141147 WebsocketOptions : & WebsocketOptions {},
142148 Dialer : & net.Dialer {Timeout : 30 * time .Second },
149+ CustomOpenConnectionFn : nil ,
143150 }
144151 return o
145152}
@@ -429,3 +436,13 @@ func (o *ClientOptions) SetDialer(dialer *net.Dialer) *ClientOptions {
429436 o .Dialer = dialer
430437 return o
431438}
439+
440+ // SetCustomOpenConectionFn replaces the inbuilt function that establishes a network connection with a custom function.
441+ // The passed in function should return an open `net.Conn` or an error (see the existing openConnection function for an example)
442+ // It enables custom networking types in addition to the defaults (tcp, tls, websockets...)
443+ func (o * ClientOptions ) SetCustomOpenConectionFn (customOpenConnectionfn OpenConnectionFunc ) * ClientOptions {
444+ if customOpenConnectionfn != nil {
445+ o .CustomOpenConnectionFn = customOpenConnectionfn
446+ }
447+ return o
448+ }
0 commit comments