-
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathexample.js
More file actions
55 lines (44 loc) · 1.33 KB
/
example.js
File metadata and controls
55 lines (44 loc) · 1.33 KB
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
53
54
55
import net from 'node:net'
import http from 'node:http'
import { Aedes } from './aedes.js'
import ws from 'ws'
const port = 1883
const wsPort = 8888
async function startAedes () {
const aedes = await Aedes.createBroker()
const server = net.createServer(aedes.handle)
const httpServer = http.createServer()
server.listen(port, function () {
console.log('server listening on port', port)
})
const wss = new ws.WebSocketServer({
server: httpServer
})
wss.on('connection', (websocket, req) => {
const stream = ws.createWebSocketStream(websocket)
aedes.handle(stream, req)
})
httpServer.listen(wsPort, function () {
console.log('websocket server listening on port', wsPort)
})
aedes.on('clientError', function (client, err) {
console.log('client error', client.id, err.message, err.stack)
})
aedes.on('connectionError', function (client, err) {
console.log('client error', client, 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)
})
}
startAedes()