Skip to content

Commit c8d7dea

Browse files
committed
Basic MQTT over Websocket test working with modded dependencies.
1 parent bd76efb commit c8d7dea

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"devDependencies": {
3434
"mocha": "*",
3535
"should": "*",
36-
"websocket-stream": "~0.3.1",
36+
"websocket-stream": "git://github.com/mcollina/websocket-stream.git#node-support",
3737
"ws": "~0.4.28"
3838
}
3939
}

test/client_websocket.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var mqtt = require('..')
88
/**
99
* Testing options
1010
*/
11-
var port = 9876;
11+
var serverPort = 9876;
1212

1313
/**
1414
* Test server
@@ -19,7 +19,7 @@ var websocket = require('websocket-stream')
1919
var http = require("http");
2020

2121
var server = http.createServer();
22-
server.listen(port);
22+
server.listen(serverPort);
2323

2424
var clientHandler = function (client) {
2525

@@ -79,16 +79,37 @@ var wss = new WebSocketServer({server: server})
7979
wss.on('connection', function(ws) {
8080
var connection = websocket(ws).pipe(new mqtt.MqttConnection());
8181
clientHandler(connection);
82+
server.emit("client", connection);
8283
})
8384

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+
85105
var build = function() {
86-
return websocket('ws://localhost:' + port)
106+
var url = 'ws://' + host + ':' + port;
107+
return websocket(url);
87108
};
88109

89-
return new mqtt.MqttClient(build);
110+
return new mqtt.MqttClient(build, opts);
90111
};
91112

92113
describe('MqttClient', function() {
93-
abstractClientTests(server, createClient, port);
114+
abstractClientTests(server, createClient, serverPort);
94115
});

0 commit comments

Comments
 (0)