@@ -13,7 +13,7 @@ import Promise
13
13
14
14
public protocol MQTTDelegate : AnyObject {
15
15
func mqtt( _ mqtt: MQTT , didUpdate status: MQTT . Status , prev: MQTT . Status )
16
- func mqtt( _ mqtt: MQTT , didReceive error: Message )
16
+ func mqtt( _ mqtt: MQTT , didReceive error: MQTT . Message )
17
17
func mqtt( _ mqtt: MQTT , didReceive error: Error )
18
18
19
19
}
@@ -34,9 +34,9 @@ extension MQTT{
34
34
/// - clientID: Client Identifier
35
35
/// - endpoint:The network endpoint
36
36
/// - params: The connection parameters `default` is `.tls`
37
- public init ( _ clientId: String , endpoint: NWEndpoint , params : NWParameters = . tls ) {
37
+ public init ( _ clientId: String , endpoint: Endpoint ) {
38
38
self . config = Config ( . v3_1_1, clientId: clientId)
39
- self . socket = Socket ( self . config, endpoint: endpoint, params : params )
39
+ self . socket = Socket ( self . config, endpoint: endpoint)
40
40
}
41
41
}
42
42
}
@@ -75,10 +75,26 @@ extension MQTT.Client{
75
75
}
76
76
}
77
77
extension MQTT . Client {
78
- /// Close gracefully by sending close frame
78
+ /// Close from server
79
+ /// - Parameters:
80
+ /// - reason: close reason code send to the server
81
+ /// - Returns: Future waiting on disconnect message to be sent
82
+ ///
79
83
@discardableResult
80
- public func close( _ code: ReasonCode = . success) -> Promise < Void > {
81
- self . close ( packet: DisconnectPacket ( reason: code) )
84
+ public func close( _ reason: MQTT . CloseReason = . normalClose) -> Promise < Void > {
85
+ var packet = DisconnectPacket ( )
86
+ if case . disconnect( let reasonCode, let properties) = reason{
87
+ switch config. version {
88
+ case . v5_0:
89
+ packet = . init( reason: reasonCode, properties: properties)
90
+ case . v3_1_1:
91
+ packet = . init( reason: reasonCode)
92
+ }
93
+ }
94
+ return self . socket. sendNoWait ( packet) . map { _ in
95
+ self . socket. directClose ( reason: reason)
96
+ return Promise ( ( ) )
97
+ }
82
98
}
83
99
/// Connect to MQTT server
84
100
///
@@ -102,7 +118,7 @@ extension MQTT.Client{
102
118
) -> Promise < Bool > {
103
119
104
120
let message = will. map {
105
- Message (
121
+ MQTT . Message (
106
122
qos: . atMostOnce,
107
123
dup: false ,
108
124
topic: $0. topic,
@@ -146,7 +162,7 @@ extension MQTT.Client{
146
162
qos: MQTTQoS = . atLeastOnce,
147
163
retain: Bool = false
148
164
) -> Promise < Void > {
149
- let message = Message ( qos: qos, dup: false , topic: topic, retain: retain, payload: payload, properties: [ ] )
165
+ let message = MQTT . Message ( qos: qos, dup: false , topic: topic, retain: retain, payload: payload, properties: [ ] )
150
166
let packetId = self . nextPacketId ( )
151
167
let packet = PublishPacket ( id: packetId, message: message)
152
168
return self . publish ( packet: packet) . then { _ in }
@@ -255,7 +271,7 @@ extension MQTT.Client {
255
271
switch self . config. version {
256
272
case . v3_1_1:
257
273
if connack. returnCode != 0 {
258
- let returnCode = MQTTError . ConnectionReturnValue ( rawValue: connack. returnCode) ?? . unrecognizedReturnValue
274
+ let returnCode = ConnectRetrunCode ( rawValue: connack. returnCode) ?? . unrecognizedReturnValue
259
275
throw MQTTError . connectionError ( returnCode)
260
276
}
261
277
case . v5_0:
@@ -319,26 +335,26 @@ extension MQTT.Client {
319
335
// check publish validity
320
336
// check qos against server max qos
321
337
guard self . connParams. maxQoS. rawValue >= packet. message. qos. rawValue else {
322
- return . init( MQTTPacketError . qosInvalid)
338
+ return . init( PacketError . qosInvalid)
323
339
}
324
340
// check if retain is available
325
341
guard packet. message. retain == false || self . connParams. retainAvailable else {
326
- return . init( MQTTPacketError . retainUnavailable)
342
+ return . init( PacketError . retainUnavailable)
327
343
}
328
344
for p in packet. message. properties {
329
345
// check topic alias
330
346
if case . topicAlias( let alias) = p {
331
347
guard alias <= self . connParams. maxTopicAlias, alias != 0 else {
332
- return . init( MQTTPacketError . topicAliasOutOfRange)
348
+ return . init( PacketError . topicAliasOutOfRange)
333
349
}
334
350
}
335
351
if case . subscriptionIdentifier = p {
336
- return . init( MQTTPacketError . publishIncludesSubscription)
352
+ return . init( PacketError . publishIncludesSubscription)
337
353
}
338
354
}
339
355
// check topic name
340
356
guard !packet. message. topic. contains ( where: { $0 == " # " || $0 == " + " } ) else {
341
- return . init( MQTTPacketError . invalidTopicName)
357
+ return . init( PacketError . invalidTopicName)
342
358
}
343
359
344
360
if packet. message. qos == . atMostOnce {
@@ -390,7 +406,7 @@ extension MQTT.Client {
390
406
/// - Returns: Future waiting for subscribe to complete. Will wait for SUBACK message from server
391
407
func subscribe( packet: SubscribePacket ) -> Promise < SubackPacket > {
392
408
guard packet. subscriptions. count > 0 else {
393
- return . init( ( MQTTPacketError . atLeastOneTopicRequired) )
409
+ return . init( ( PacketError . atLeastOneTopicRequired) )
394
410
}
395
411
return self . socket. sendPacket ( packet) . then { ack in
396
412
if let suback = ack as? SubackPacket {
@@ -404,7 +420,7 @@ extension MQTT.Client {
404
420
/// - Returns: Future waiting for subscribe to complete. Will wait for SUBACK message from server
405
421
func unsubscribe( packet: UnsubscribePacket ) -> Promise < SubackPacket > {
406
422
guard packet. subscriptions. count > 0 else {
407
- return . init( ( MQTTPacketError . atLeastOneTopicRequired) )
423
+ return . init( ( PacketError . atLeastOneTopicRequired) )
408
424
}
409
425
return self . socket. sendPacket ( packet) . then { ack in
410
426
if let suback = ack as? SubackPacket {
@@ -413,11 +429,6 @@ extension MQTT.Client {
413
429
throw MQTTError . unexpectedMessage
414
430
}
415
431
}
416
- /// Close from server
417
- /// - Returns: Future waiting on disconnect message to be sent
418
- func close( packet: DisconnectPacket ) -> Promise < Void > {
419
- return self . socket. sendNoWait ( packet)
420
- }
421
432
422
433
func auth( packet: AuthPacket ) -> Promise < Packet > {
423
434
return self . socket. sendPacket ( packet)
0 commit comments