@@ -50,16 +50,18 @@ export default class MqttClient implements ProtocolClient {
5050 ) : Promise < Subscription > {
5151 const contentType = form . contentType ?? ContentSerdes . DEFAULT ;
5252 const requestUri = new url . URL ( form . href ) ;
53- const topic = requestUri . pathname . slice ( 1 ) ;
5453 const brokerUri : string = `${ this . scheme } ://` + requestUri . host ;
54+ // Keeping the path as the topic for compatibility reasons.
55+ // Current specification allows only form["mqv:filter"]
56+ const filter = requestUri . pathname . slice ( 1 ) ?? form [ "mqv:filter" ] ;
5557
5658 if ( this . client === undefined ) {
5759 this . client = await mqtt . connectAsync ( brokerUri , this . config ) ;
5860 }
5961
6062 this . client . on ( "message" , ( receivedTopic : string , payload : Buffer ) => {
6163 debug ( `Received MQTT message (topic: ${ receivedTopic } , data length: ${ payload . length } )` ) ;
62- if ( receivedTopic === topic ) {
64+ if ( filter . includes ( receivedTopic ) ) {
6365 next ( new Content ( contentType , Readable . from ( payload ) ) ) ;
6466 }
6567 } ) ;
@@ -70,7 +72,7 @@ export default class MqttClient implements ProtocolClient {
7072 if ( error ) error ( err ) ;
7173 } ) ;
7274
73- await this . client . subscribeAsync ( topic ) ;
75+ await this . client . subscribeAsync ( filter ) ;
7476
7577 return new Subscription ( ( ) => {
7678 if ( ! this . client ) {
@@ -79,7 +81,7 @@ export default class MqttClient implements ProtocolClient {
7981 ) ;
8082 return ;
8183 }
82- this . client . unsubscribe ( topic ) ;
84+ this . client . unsubscribe ( filter ) ;
8385 } ) ;
8486 }
8587
@@ -89,20 +91,19 @@ export default class MqttClient implements ProtocolClient {
8991
9092 public async writeResource ( form : MqttForm , content : Content ) : Promise < void > {
9193 const requestUri = new url . URL ( form . href ) ;
92- const topic = requestUri . pathname . slice ( 1 ) ;
9394 const brokerUri = `${ this . scheme } ://${ requestUri . host } ` ;
95+ const topic = requestUri . pathname . slice ( 1 ) ?? form [ "mqv:topic" ] ;
9496
9597 if ( this . client === undefined ) {
9698 this . client = await mqtt . connectAsync ( brokerUri , this . config ) ;
9799 }
98100
99101 // if not input was provided, set up an own body otherwise take input as body
100- if ( content === undefined ) {
101- await this . client . publishAsync ( topic , Buffer . from ( "" ) ) ;
102- } else {
103- const buffer = await content . toBuffer ( ) ;
104- await this . client . publishAsync ( topic , buffer ) ;
105- }
102+ const buffer = content === undefined ? Buffer . from ( "" ) : await content . toBuffer ( ) ;
103+ await this . client . publishAsync ( topic , buffer , {
104+ retain : form [ "mqv:retain" ] ,
105+ qos : this . mapQoS ( form [ "mqv:qos" ] ) ,
106+ } ) ;
106107 }
107108
108109 public async invokeResource ( form : MqttForm , content : Content ) : Promise < Content > {
@@ -115,12 +116,11 @@ export default class MqttClient implements ProtocolClient {
115116 }
116117
117118 // if not input was provided, set up an own body otherwise take input as body
118- if ( content === undefined ) {
119- await this . client . publishAsync ( topic , Buffer . from ( "" ) ) ;
120- } else {
121- const buffer = await content . toBuffer ( ) ;
122- await this . client . publishAsync ( topic , buffer ) ;
123- }
119+ const buffer = content === undefined ? Buffer . from ( "" ) : await content . toBuffer ( ) ;
120+ await this . client . publishAsync ( topic , buffer , {
121+ retain : form [ "mqv:retain" ] ,
122+ qos : this . mapQoS ( form [ "mqv:qos" ] ) ,
123+ } ) ;
124124 // there will be no response
125125 return new DefaultContent ( Readable . from ( [ ] ) ) ;
126126 }
@@ -169,15 +169,20 @@ export default class MqttClient implements ProtocolClient {
169169 return true ;
170170 }
171171
172- private mapQoS ( qos : MqttQoS ) : Required < IClientPublishOptions > [ "qos" ] {
172+ private mapQoS ( qos : MqttQoS | undefined ) : Required < IClientPublishOptions > [ "qos" ] {
173173 switch ( qos ) {
174- case 2 :
175- return ( qos = 2 ) ;
176- case 1 :
177- return ( qos = 1 ) ;
178- case 0 :
174+ case "0" :
175+ return 0 ;
176+ case "1" :
177+ return 1 ;
178+ case "2" :
179+ return 2 ;
180+ case undefined :
181+ return 0 ;
179182 default :
180- return ( qos = 0 ) ;
183+ warn ( `MqttClient received unsupported QoS level '${ qos } '` ) ;
184+ warn ( `MqttClient falling back to QoS level '0'` ) ;
185+ return 0 ;
181186 }
182187 }
183188}
0 commit comments