-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (43 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require("express");
const app = express();
const path = require("path");
const aedes = require("aedes")();
const mqtt = require("net").createServer(aedes.handle);
const httpServer = require("http").createServer(app);
const ws = require("websocket-stream");
const mqttPort = 1883;
const appPort = 8080;
mqtt.listen(mqttPort, function() {
console.log("mqtt server listening on port", mqttPort);
});
ws.createServer(
{
server: httpServer
},
aedes.handle
);
httpServer.listen(appPort, function() {
console.log("server listening on port", appPort);
});
aedes.on("clientError", function(client, err) {
console.log("client error", client.id, err.message, err.stack);
});
aedes.on("publish", function(packet, client) {
if (client) {
console.log("message from client", client.id);
}
});
aedes.on("subscribe", function(subscriptions, client) {
if (client) {
console.log("subscribe from client", subscriptions, client.id);
}
});
aedes.on("client", function(client) {
console.log("new client", client.id);
});
// Serve static assets
app.use(express.static(path.resolve(__dirname, "client", "build")));
// Always return the main index.html, so react-router render the route in the client
app.get("/", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});