Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

mqtt over websocket on different path #606

Merged
merged 5 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,17 @@ Server.prototype.close = function(callback) {
*
* @api public
* @param {HttpServer} server
* @param {String} path
*/
Server.prototype.attachHttpServer = function(server) {
Server.prototype.attachHttpServer = function(server, path) {
var that = this;
ws.createServer({ server: server }, function(stream) {

var opt = { server: server };
if (path) {
opt.path = path;
}

ws.createServer(opt, function(stream) {
var conn = new Connection(stream);
new Client(conn, that);
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"author": "Matteo Collina <[email protected]>",
"license": "MIT",
"devDependencies": {
"browserify": "~13.0.0",
"chai": "^3.5.0",
"coveralls": "~2.11.1",
"dox-foundation": "~0.5.4",
Expand All @@ -62,9 +63,9 @@
"sinon-chai": "~2.8.0",
"supertest": "~1.2.0",
"tmp": "0.0.24",
"browserify": "~13.0.0",
"uglify-js": "^2.4.16",
"underscore": "^1.7.0"
"underscore": "^1.7.0",
"ws": "^1.0.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a real dependency, not a devDependency.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forget about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do you mean? Would it be ok now then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok, my bad.

},
"dependencies": {
"array-from": "^2.1.1",
Expand Down
6 changes: 3 additions & 3 deletions test/abstract_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ module.exports = function(moscaSettings, createConnection) {
});

function finish () {
client.removeListener('error', finish)
client.stream.removeListener('close', finish)
done()
client.removeListener('error', finish);
client.stream.removeListener('close', finish);
done();
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe("mosca.Server", function() {
// Simulate a situation that it takes same time to do authorizeSubscribe.
this.instance.authorizeSubscribe = function(client, topic, callback) {
setTimeout(function(){
callback(null, true)
callback(null, true);
}, 300);
};

Expand Down
214 changes: 214 additions & 0 deletions test/server_websocket_attach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
var mqtt = require('mqtt')
var websocket = require('ws');
var http = require('http');

var port = nextPort();
var path = '/test';
var mqttPath = '/mqttws';
var mqttTopic = 'atopic';
var ping = 'ping';
var pong = 'pong';

describe("mosca.Server - Mqtt-over-WS attached to existing http server", function() {

var mqttServ
var server

afterEach(function(done){
server.close();
mqttServ.close(done);
})

it("should be able to do mqtt over WebSocket", function(done) {
server = http.createServer();
mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port);
client.subscribe(mqttTopic);
client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
server.close();
mqttServ.close(done);
});
client.publish(mqttTopic, ping);
});
});

it("should be able to do mqtt over WebSocket on specific path", function(done) {
server = http.createServer();
mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
server.close();
mqttServ.close(done);
});
client.publish(mqttTopic, ping);
});
});

it("should not be able to do mqtt over WebSocket on different path", function(done) {
server = http.createServer();
mqttServ = new mosca.Server({port:3333});// async test, preventing intefere with other tests that spawn on 1883 port
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + '/junk');
client.subscribe(mqttTopic);
var failed = false;
client.on("message", function(topic, payload) {
failed = true;
done(failed)
});
client.publish(mqttTopic, ping);
setTimeout(function(){
if (!failed){
done()
}
}, 2000);
});
});

it("should not be able to do mqtt over WebSocket on root path", function(done) {
var server = http.createServer();
var mqttServ = new mosca.Server({port:3333});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port);
client.subscribe(mqttTopic);
var failed = false;
client.on("message", function(topic, payload) {
failed = true;
mqttServ.close(function(){
done(failed);
});
});
client.publish(mqttTopic, ping);
setTimeout(function(){
if (!failed){
server.close();
mqttServ.close(done);
}
}, 2000);
});
});

//TODO: this test failed, what does the spec say?
xit("should not be able to do mqtt over WebSocket on a specific path without attaching to any path", function(done) {
var server = http.createServer();
var mqttServ = new mosca.Server({port:3333});
mqttServ.attachHttpServer(server);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
var failed = false;
client.on("message", function(topic, payload) {
failed = true;
mqttServ.close(function(){
done(failed);
});
});
client.publish(mqttTopic, ping);
setTimeout(function(){
if (!failed){
server.close();
mqttServ.close(done);
}
}, 2000);
});
});
});

describe("mosca.Server - Websocket and Mqtt-over-WS attached to the same http server", function() {
it("ws client should not connect when mqtt is attached to http server without path", function(done) {
var server = http.createServer();
var wss = new websocket.Server({
server: server,
path: path,
perMessageDeflate: false
});
var mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server);

server.listen(port, function(){
var ws = new websocket('ws://localhost:' + port + path, {
perMessageDeflate: false
});

ws.on('error', function(e) {
expect(e).to.not.be.undefined;
server.close();
mqttServ.close(done);
});
});
});

it("ws client should be able to connect when specific path is used", function(done) {
var server = http.createServer();
var wss = new websocket.Server({
server: server,
path: path,
perMessageDeflate: false
});
wss.on('connection', function(conn){
conn.on('message', function(msg){
expect(msg).to.equal(ping);
conn.send(pong);
});
});

var mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var ws = new websocket('ws://localhost:' + port + path, {
perMessageDeflate: false
});

ws.on('open', function(){
ws.send(ping);
});

ws.on('message', function(msg){
expect(msg).to.equal(pong);
server.close();
mqttServ.close(done);
});
});
});


it("mqtt client should be able to connect as well", function(done) {
var server = http.createServer();
var wss = new websocket.Server({
server: server,
path: path,
perMessageDeflate: false
});

var mqttServ = new mosca.Server({});
mqttServ.attachHttpServer(server, mqttPath);

server.listen(port, function(){
var client = mqtt.connect('ws://localhost:' + port + mqttPath);
client.subscribe(mqttTopic);
client.on("message", function(topic, payload) {
expect(topic).to.equal(mqttTopic);
expect(payload.toString()).to.equal(ping);
server.close();
mqttServ.close(done);
});
client.publish(mqttTopic, ping);
});
});
});