This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 506
mqtt over websocket on different path #606
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be a real dependency, not a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forget about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what do you mean? Would it be ok now then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's ok, my bad. |
||
}, | ||
"dependencies": { | ||
"array-from": "^2.1.1", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
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 server, mqttServ; | ||
|
||
beforeEach(function(){ | ||
server = http.createServer(); | ||
mqttServ = new mosca.Server({interfaces:[]}); | ||
}); | ||
|
||
afterEach(function(){ | ||
server.close(); | ||
}); | ||
|
||
it("should not occupy 1883 port while attached to http server", function(done) { | ||
mqttServ.attachHttpServer(server); | ||
server.listen(1883, done); | ||
}); | ||
|
||
it("should be able to do mqtt over WebSocket", function(done) { | ||
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); | ||
done(); | ||
}); | ||
client.publish(mqttTopic, ping); | ||
}); | ||
}); | ||
|
||
it("should be able to do mqtt over WebSocket on specific path", function(done) { | ||
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); | ||
done(); | ||
}); | ||
client.publish(mqttTopic, ping); | ||
}); | ||
}); | ||
|
||
it("should not be able to do mqtt over WebSocket on different path", function(done) { | ||
mqttServ.attachHttpServer(server, mqttPath); | ||
server.listen(port, function(){ | ||
var client = mqtt.connect('ws://localhost:' + port + '/junk'); | ||
client.subscribe(mqttTopic); | ||
var failed = false;// ensuring done is called once | ||
client.on("message", function(topic, payload) { | ||
failed = true; | ||
done(failed); | ||
}); | ||
client.publish(mqttTopic, ping); | ||
setTimeout(function(){ | ||
if (!failed){ | ||
done(); | ||
} | ||
}, 3000); | ||
}); | ||
}); | ||
|
||
it("should not be able to do mqtt over WebSocket on root path", function(done) { | ||
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; | ||
done(failed); | ||
}); | ||
client.publish(mqttTopic, ping); | ||
setTimeout(function(){ | ||
if (!failed){ | ||
done(); | ||
} | ||
}, 2000); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("mosca.Server - Websocket and Mqtt-over-WS attached to the same http server", function() { | ||
var server, mqttServ, wss; | ||
|
||
beforeEach(function(){ | ||
server = http.createServer(); | ||
mqttServ = new mosca.Server({interfaces:[]}); | ||
|
||
wss = new websocket.Server({ | ||
server: server, | ||
path: path, | ||
perMessageDeflate: false | ||
}); | ||
}); | ||
|
||
afterEach(function(){ | ||
server.close(); | ||
}); | ||
|
||
it("ws client should not connect when mqtt is attached to http server without path", function(done) { | ||
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; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("ws client should be able to connect when specific path is used", function(done) { | ||
mqttServ.attachHttpServer(server, mqttPath); | ||
wss.on('connection', function(conn){ | ||
conn.on('message', function(msg){ | ||
expect(msg).to.equal(ping); | ||
conn.send(pong); | ||
}); | ||
}); | ||
|
||
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); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("mqtt client should be able to connect as well", function(done) { | ||
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); | ||
done(); | ||
}); | ||
client.publish(mqttTopic, ping); | ||
}); | ||
}); | ||
|
||
it("both ws and mqtt client should be able to connect at the same time", function(done) { | ||
mqttServ.attachHttpServer(server, mqttPath); | ||
wss.on('connection', function(conn){ | ||
conn.on('message', function(msg){ | ||
expect(msg).to.equal(ping); | ||
conn.send(pong); | ||
}); | ||
}); | ||
|
||
server.listen(port, function(){ | ||
var client = mqtt.connect('ws://localhost:' + port + mqttPath); | ||
var ws = new websocket('ws://localhost:' + port + path, { | ||
perMessageDeflate: false | ||
}); | ||
|
||
client.on('connect', function () { | ||
client.subscribe(mqttTopic); | ||
setTimeout(function(){// wait for ws to connect | ||
ws.send(ping); | ||
}, 2000); | ||
}); | ||
|
||
ws.on('message', function(msg){ | ||
expect(msg).to.equal(pong); | ||
client.publish(mqttTopic, ping); | ||
}); | ||
|
||
client.on("message", function(topic, payload) { | ||
expect(topic).to.equal(mqttTopic); | ||
expect(payload.toString()).to.equal(ping); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this has been commented? If this is not needed anymore, we should just remove it.