@@ -68,10 +68,13 @@ function brokersForConfig (brokerLines) {
68
68
* include in the stored configuration file.
69
69
* @param {Number } verbosity - Level of verbosity at which to log any error
70
70
* or trace messages.
71
+ * @param {Boolean } useWebSockets Whether or not the client will use WebSockets.
72
+ * If false MQTT over tcp will be used.
73
+ * If only WebSocket brokers are specified this will default to true.
71
74
* @private
72
75
*/
73
76
function storeProvisionConfig ( config , configDir , filePrefix ,
74
- privateKeyFileName , verbosity ) {
77
+ privateKeyFileName , verbosity , useWebSockets ) {
75
78
if ( typeof config !== 'string' ) {
76
79
throw new DxlError ( 'Unexpected data type for response: ' +
77
80
typeof config )
@@ -91,16 +94,41 @@ function storeProvisionConfig (config, configDir, filePrefix,
91
94
92
95
var brokers = brokersForConfig ( brokerLines )
93
96
97
+ var brokerWSLines = [ ]
98
+ if ( configElements [ 3 ] ) {
99
+ brokerWSLines = configElements [ 3 ] . split (
100
+ / [ \r \n ] + / ) . filter ( function ( brokerWSLine ) {
101
+ return brokerWSLine . length > 0
102
+ }
103
+ )
104
+ }
105
+
106
+ var brokersWS = brokersForConfig ( brokerWSLines )
107
+ var webSocketsEnabled = typeof useWebSockets !== 'undefined' ? useWebSockets : false
94
108
var certFileName = filePrefix + '.crt'
95
109
var configString = ini . encode ( {
110
+ General : {
111
+ UseWebSockets : webSocketsEnabled
112
+ } ,
96
113
Certs : {
97
114
BrokerCertChain : DEFAULT_BROKER_CERT_CHAIN_FILE_NAME ,
98
115
CertFile : path . basename ( certFileName ) ,
99
116
PrivateKey : path . basename ( privateKeyFileName )
100
117
} ,
101
- Brokers : brokers
118
+ Brokers : brokerLines . length ? brokers : { marker : 1 } ,
119
+ BrokersWebSockets : brokerWSLines . length ? brokersWS : { marker : 1 }
102
120
} ) . replace ( / \\ ; / g, ';' )
103
121
122
+ // ini.js does not support empty sections , using marker and replacing it
123
+ if ( configString . includes ( 'marker=1' ) ) {
124
+ configString = configString . replace ( / m a r k e r = 1 ( \r \n | \n ) / g, '' )
125
+ }
126
+
127
+ if ( ! webSocketsEnabled ) {
128
+ // UseWebSockets property is commented out by default
129
+ configString = configString . replace ( 'UseWebSockets' , '#UseWebSockets' )
130
+ }
131
+
104
132
var configFileName = path . join ( configDir ,
105
133
provisionUtil . DEFAULT_CONFIG_FILE_NAME )
106
134
if ( verbosity ) {
@@ -203,10 +231,13 @@ function storeProvisionConfig (config, configDir, filePrefix,
203
231
* provisioned configuration has been stored. If an error occurs, the first
204
232
* parameter supplied to the `doneCallback` is an `Error` instance
205
233
* containing failure details.
234
+ * @param {Boolean } [useWebSockets] Whether or not the client will use WebSockets.
235
+ * If false MQTT over tcp will be used.
236
+ * If only WebSocket brokers are specified this will default to true.
206
237
* @private
207
238
*/
208
239
module . exports = function provisionConfig ( configDir , commonOrCsrFileName ,
209
- hostInfo , options ) {
240
+ hostInfo , options , useWebSockets ) {
210
241
if ( ! configDir ) {
211
242
throw new TypeError ( 'configDir is required for provisioning' )
212
243
}
@@ -239,10 +270,15 @@ module.exports = function provisionConfig (configDir, commonOrCsrFileName,
239
270
try {
240
271
storeProvisionConfig ( config , configDir ,
241
272
options . filePrefix || pki . DEFAULT_PKI_FILE_PREFIX ,
242
- privateKeyFileName , verbosity )
273
+ privateKeyFileName , verbosity , useWebSockets )
243
274
} catch ( err ) {
244
275
error = err
245
276
}
277
+ } else {
278
+ if ( error ) {
279
+ console . log ( 'Error from server' + error )
280
+ }
281
+ console . log ( 'No data from Server.Check Server Configuration' )
246
282
}
247
283
provisionUtil . invokeCallback ( error , doneCallback , verbosity )
248
284
}
0 commit comments