-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
89 lines (56 loc) · 1.67 KB
/
express.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require('express')
const mqtt = require('mqtt')
const app = express()
const WebSocket = require('ws');
// 创建MQTT客户端
const client = mqtt.connect('wss://chaoxi.live:8084/mqtt', {
username: 'chaoxi',
password: 'ilove4dbim',
clientId: `web_${Math.random().toString(16).slice(3)}`
})
app.get('/', function (req, res) {
res.send('Hello World!');
console.log("hh,你这次请求在访问我哦。。。")
})
app.get('/example', (req, res) => {
res.send('Hello World!');
});
// 处理HTTP请求
app.get('/api', (req, res) => {
// 将请求数据转换为MQTT消息
// const messanoge = {
// method: req.method,
// url: req.url,
// headers: req.headers,
// body: req.body
// }
//获取当前时间戳
const timestamp = Date.parse(new Date());
const mqmessage = {"cardupdate": "true","time": timestamp}
client.publish('1/1/1/service/1', JSON.stringify(mqmessage))
res.send('收到更新请求,发送到mqtt' + JSON.stringify(mqmessage))
console.log("api被请求了")
})
// //websocket部分
// const wss = new WebSocket.Server({ port: 7777 });
// // ws://localhost:7777
// wss.on('connection', (ws) => {
// console.log('客户端已连接');
// ws.on('message', (messagews) => {
// console.log(`收到消息:${messagews}`);
// if (message === 'update') {
// // 向连接的客户端发送信息
// wss.clients.forEach((client) => {
// if (client.readyState === WebSocket.OPEN) {
// client.send('更新信息');
// }
// });
// }
// });
// ws.on('close', () => {
// console.log('客户端已断开连接');
// });
// });
app.listen(3000, () => {
console.log('Server started on port 3000')
})