Nodejs的mqtt简单实现

mosca

安装

1
2
npm i mosca
npm i mqtt

简单服务器

server.ts

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
56
57
58
59
60
61
62
63
64
import { Server, Client, Packet, Authorizer } from 'mosca'

// 简单的设置方法
const setting = {
// 监听的端口
port: 12345,
// 开启http的客户端
http: {
port: 12346, // http的绑定端口
bundle: true, // 提供mqtt.js文件地址
}
}

const server = new Server(setting)
const auth = new Authorizer()


server.on('clientConnected', (client: Client) => {
console.log('客户端连接的ID:', client.id)
})

server.on('clientDisconnecting', (client: Client) => {
console.log('客户端正在断连', client.id)
})

server.on('clientDisconnected', (client: Client) => {
console.log('客户端已经断连', client.id)
})

server.on('published', (packet: Packet, client: Client) => {
console.log('发布消息:', packet)
})

server.on('subscribed', (topic, client: Client) => {
console.log('订阅主题:', topic)
})

server.on('unsubscribed', (topic, client: Client) => {
console.log('取消订阅:', topic)
})

server.on('ready', () => {
console.log('服务器启动成功!')
// 简单的acl版本
// server.authenticate = auth.authenticate
// auth.addUser('limo', 'limo1234', '+', '+', (err) => {})

// 自定义的校验方法
server.authenticate = (client: Client, username: string, password: string, callback) => {
if (username == 'limo' && password == 'limo123') {
return callback(null, true)
} else {
return callback(null, false)
}
}

server.authorizePublish = (client: Client, topic: string, payload: string, callback) => {
return callback(null, true)
}

server.authorizeSubscribe = (client: Client, topic: string, callback) => {
return callback(null, true)
}
});

简单的本地客户端

client.ts

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
import { connect, Client } from 'mqtt'

const client: Client = connect('mqtt://127.0.0.1:12345', {
username: 'limo',
password: 'limo123'
})

const topic = 'sys/log/now'

client.on('connect', () => {
console.log('客户端连接成功!')
client.subscribe(topic)
setInterval(() => {
client.publish(topic, new Date().toLocaleString())
}, 1000)
})

client.on('error', (err) => {
console.log('连接失败!')
client.end()
})

client.on('message', (topic, message) => {
console.log(`topic:${topic} message:${message}`)
})

简单的html客户端

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./mqtt.js"></script>
</head>
<body>
<script >
const client = mqtt.connect('mqtt://192.168.218.88:12346')
const topic = 'sys/log/now'
client.on('connect', () => {
console.log('客户端连接成功!')
client.subscribe(topic)
})
client.on('message', (topic, message) => {
console.log(`topic:${topic} message:${message}`)
})
</script>
</body>
</html>