@@ -19,6 +19,7 @@ public class MQTTClient {
19
19
case unexpectedMessage
20
20
case decodeError
21
21
case websocketUpgradeFailed
22
+ case timeout
22
23
}
23
24
/// EventLoopGroup used by MQTTCllent
24
25
let eventLoopGroup : EventLoopGroup
@@ -28,10 +29,10 @@ public class MQTTClient {
28
29
let host : String
29
30
/// Port to connect to
30
31
let port : Int
31
- /// Called whenever a publish event occurs
32
- let publishCallback : ( Result < MQTTPublishInfo , Swift . Error > ) -> ( )
33
32
/// Client configuration
34
33
let configuration : Configuration
34
+ /// Called whenever a publish event occurs
35
+ let publishCallback : ( Result < MQTTPublishInfo , Swift . Error > ) -> ( )
35
36
36
37
/// Channel client is running on
37
38
var channel : Channel ?
@@ -44,22 +45,35 @@ public class MQTTClient {
44
45
public struct Configuration {
45
46
public init (
46
47
disablePingreq: Bool = false ,
48
+ pingreqInterval: TimeAmount ? = nil ,
49
+ timeout: TimeAmount ? = nil ,
47
50
useSSL: Bool = false ,
48
51
useWebSockets: Bool = false ,
49
52
tlsConfiguration: TLSConfiguration ? = nil ,
50
- webSocketURLPath: String ? = nil )
51
- {
53
+ webSocketURLPath: String ? = nil
54
+ ) {
52
55
self . disablePingreq = disablePingreq
56
+ self . pingreqInterval = pingreqInterval
57
+ self . timeout = timeout
53
58
self . useSSL = useSSL
54
59
self . useWebSockets = useWebSockets
55
60
self . tlsConfiguration = tlsConfiguration
56
61
self . webSocketURLPath = webSocketURLPath
57
62
}
58
63
64
+ /// disable the sending of pingreq messages
59
65
let disablePingreq : Bool
66
+ /// override internal between each pingreq message
67
+ let pingreqInterval : TimeAmount ?
68
+ /// timeout for server response
69
+ let timeout : TimeAmount ?
70
+ /// use encrypted connection to server
60
71
let useSSL : Bool
72
+ /// use a websocket connection to server
61
73
let useWebSockets : Bool
74
+ /// TLS configuration
62
75
let tlsConfiguration : TLSConfiguration ?
76
+ /// URL Path for web socket. Defaults to "/"
63
77
let webSocketURLPath : String ?
64
78
}
65
79
@@ -130,8 +144,10 @@ public class MQTTClient {
130
144
/// - Returns: Future waiting for connect to fiinsh
131
145
public func connect( info: MQTTConnectInfo , will: MQTTPublishInfo ? = nil ) -> EventLoopFuture < Void > {
132
146
guard self . channel == nil else { return eventLoopGroup. next ( ) . makeFailedFuture ( Error . alreadyConnected) }
133
- let timeout = TimeAmount . seconds ( max ( Int64 ( info. keepAliveSeconds - 5 ) , 5 ) )
134
- return createBootstrap ( pingreqTimeout: timeout)
147
+ // work out pingreq interval
148
+ let pingreqInterval = configuration. pingreqInterval ?? TimeAmount . seconds ( max ( Int64 ( info. keepAliveSeconds - 5 ) , 5 ) )
149
+
150
+ return createBootstrap ( pingreqInterval: pingreqInterval)
135
151
. flatMap { _ -> EventLoopFuture < MQTTInboundMessage > in
136
152
self . clientIdentifier = info. clientIdentifier
137
153
return self . sendMessage ( MQTTConnectMessage ( connect: info, will: nil ) ) { message in
@@ -267,7 +283,7 @@ extension MQTTClient {
267
283
return bootstrap
268
284
}
269
285
270
- func createBootstrap( pingreqTimeout : TimeAmount ) -> EventLoopFuture < Void > {
286
+ func createBootstrap( pingreqInterval : TimeAmount ) -> EventLoopFuture < Void > {
271
287
let promise = self . eventLoopGroup. next ( ) . makePromise ( of: Void . self)
272
288
do {
273
289
// Work out what handlers to add
@@ -276,7 +292,7 @@ extension MQTTClient {
276
292
ByteToMessageHandler ( ByteToMQTTMessageDecoder ( client: self ) )
277
293
]
278
294
if !configuration. disablePingreq {
279
- handlers = [ PingreqHandler ( client: self , timeout: pingreqTimeout ) ] + handlers
295
+ handlers = [ PingreqHandler ( client: self , timeout: pingreqInterval ) ] + handlers
280
296
}
281
297
// get bootstrap based off what eventloop we are running on
282
298
let bootstrap = try getBootstrap ( self . eventLoopGroup)
@@ -351,7 +367,7 @@ extension MQTTClient {
351
367
352
368
func sendMessage( _ message: MQTTOutboundMessage , checkInbound: @escaping ( MQTTInboundMessage ) throws -> Bool ) -> EventLoopFuture < MQTTInboundMessage > {
353
369
guard let channel = self . channel else { return eventLoopGroup. next ( ) . makeFailedFuture ( Error . noConnection) }
354
- let task = MQTTTask ( on: eventLoopGroup. next ( ) , checkInbound: checkInbound)
370
+ let task = MQTTTask ( on: eventLoopGroup. next ( ) , timeout : configuration . timeout , checkInbound: checkInbound)
355
371
let taskHandler = MQTTTaskHandler ( task: task, channel: channel)
356
372
357
373
channel. pipeline. addHandler ( taskHandler)
0 commit comments