@@ -8,7 +8,7 @@ var mqtt = require('..')
8
8
/**
9
9
* Testing options
10
10
*/
11
- var port = 9876 ;
11
+ var serverPort = 9876 ;
12
12
13
13
/**
14
14
* Test server
@@ -19,7 +19,7 @@ var websocket = require('websocket-stream')
19
19
var http = require ( "http" ) ;
20
20
21
21
var server = http . createServer ( ) ;
22
- server . listen ( port ) ;
22
+ server . listen ( serverPort ) ;
23
23
24
24
var clientHandler = function ( client ) {
25
25
@@ -79,16 +79,37 @@ var wss = new WebSocketServer({server: server})
79
79
wss . on ( 'connection' , function ( ws ) {
80
80
var connection = websocket ( ws ) . pipe ( new mqtt . MqttConnection ( ) ) ;
81
81
clientHandler ( connection ) ;
82
+ server . emit ( "client" , connection ) ;
82
83
} )
83
84
84
- var createClient = function ( port ) {
85
+ var createClient = function ( port , host , opts ) {
86
+ if ( 'object' === typeof port ) {
87
+ opts = port ;
88
+ port = serverPort ;
89
+ host = 'localhost' ;
90
+ } else if ( 'object' === typeof host ) {
91
+ opts = host ;
92
+ host = 'localhost' ;
93
+ } else if ( 'object' !== typeof opts ) {
94
+ opts = { } ;
95
+ }
96
+
97
+ if ( ! host ) {
98
+ host = 'localhost'
99
+ }
100
+
101
+ if ( opts && opts . clean === false && ! opts . clientId ) {
102
+ throw new Error ( "Missing clientId for unclean clients" ) ;
103
+ }
104
+
85
105
var build = function ( ) {
86
- return websocket ( 'ws://localhost:' + port )
106
+ var url = 'ws://' + host + ':' + port ;
107
+ return websocket ( url ) ;
87
108
} ;
88
109
89
- return new mqtt . MqttClient ( build ) ;
110
+ return new mqtt . MqttClient ( build , opts ) ;
90
111
} ;
91
112
92
113
describe ( 'MqttClient' , function ( ) {
93
- abstractClientTests ( server , createClient , port ) ;
114
+ abstractClientTests ( server , createClient , serverPort ) ;
94
115
} ) ;
0 commit comments