-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwww-mqtt-broker.js
223 lines (204 loc) · 6.1 KB
/
www-mqtt-broker.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env node
/**
* WebStorm
* Helper.php Created by usher.yue.
* User: usher.yue
* Date: 17/1/8
* Time: 下午5:27
* mqtt-server启动入口
*/
'use strict';
const http = require('http');
const https = require('https');
const path = require('path');
const cluster = require('cluster');
const fs=require('fs');
const aedes = require('aedes')();
const mqttServer = require('net').createServer(aedes.handle);
///////////进程启动控制状态/////////////////
/**
* mqtt server监听服务
* @type {number}
*/
global.MQTT_PORT=33301;
/**
* true开启集群多核
* false 启动单核用于开发
* @type {boolean}
*/
global.USE_CLUSTER = true ;
/**
* app path
* @type {string}
*/
global.APP_PATH = `${__dirname}/app/`;
/**
* 项目目录
* @type {string}
*/
global.APP_ROOT=__dirname;
/**
* 名字空间配置文件
* @type {string}
*/
global.NS_CONFIG = `${APP_PATH}conf/ns`;
/**
* 应用全局配置文件
* @type {string}
*/
global.APP_CONFIG = `${APP_PATH}conf/config`;
/**
* core入口禁止修改
* @type {string}
*/
global.CORE_PATH= `${__dirname}/core/core`;
/**
* process name
* @type {string}
*/
global.PROCESS_NAME='WWW-MQTT';
///////////////加载模块/////////////////////
global.config = require(APP_CONFIG);
global.config.nsConfig = require(NS_CONFIG);
const app = require(CORE_PATH);
//////////////////读取配置选项////////////////////
const httpPort = normalizePort(process.env.HTTP_PORT || global.config.httpConfig.httpPort)||0;
const httpsPort = normalizePort(process.env.HTTPS_PORT || global.config.httpConfig.httpsPort)||0;
//run service
if (cluster.isMaster) {
var workersNum = 1;
colorlog.info('Master',`Run ${PROCESS_NAME} Master Process ,PID:`+process.pid);
// Fork workers.
if(USE_CLUSTER){
workersNum=require('os').cpus().length
}
for (let i = 0; i < workersNum; i++) {
cluster.fork();
}
cluster.on('listening', function (worker, address) {
if( address.port==MQTT_PORT){
colorlog.info('Worker','PID:'+worker.process.pid+',mqtt Listen Port:' + address.port);
}else{
colorlog.info('Worker','PID:'+worker.process.pid+',http(s) Listen Port:' + address.port);
}
});
cluster.on('exit', function (worker, code, signal) {
colorlog.warning('Error','Worker ' + worker.process.pid + ' Exited');
cluster.fork();
});
process.title = `${PROCESS_NAME} Master Process `;
} else {
//http server port
if(httpPort){
//HTTP协议监听
const httpServer = http.createServer(app);
httpServer.listen(httpPort);
httpServer.on('error', onError);
httpServer.on('listening', onListening);
}
//http servers port
if(httpsPort){
const options = {
key: fs.readFileSync(`${APP_PATH}cert/${config.httpConfig.cert.key}`),
cert: fs.readFileSync(`${APP_PATH}cert/${config.httpConfig.cert.cert}`)
};
const httpsServer = https.createServer(options,app);
httpsServer.listen(httpsPort);
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);
}
//MQTT协议监听
mqttServer.listen(MQTT_PORT, function () {
// console.log('MQTT Listen Portt:', MQTT_PORT)
});
/**
* mqtt auth
* @param client
* @param username
* @param password
* @param callback
*/
aedes.authenticate = function (client, username, password, callback) {
callback(null, username === 'admin' && password=='admin');
}
/**
* 客户端连接
*/
aedes.on('client', function (client) {
console.log('Client Connected: \x1b[33m' + (client ? client.id : client) + '\x1b[0m', 'to broker', aedes.id)
});
/**
* 订阅
*/
aedes.on('subscribe', function (subscriptions, client) {
// console.log('MQTT client \x1b[32m' + (client ? client.id : client) +
// '\x1b[0m subscribed to topics: ' + subscriptions.map(s => s.topic).join('\n'), 'from broker', aedes.id)
});
/**
* when message is publish
*/
aedes.on('publish', async function (packet, client) {
// console.log('Client \x1b[31m' + (client ? client.id : 'BROKER_' + aedes.id) + '\x1b[0m has published', packet.payload.toString(), 'on', packet.topic, 'to broker', aedes.id)
});
/**
* 取消订阅消息
*/
aedes.on('unsubscribe', function (subscriptions, client) {
// console.log('MQTT client \x1b[32m' + (client ? client.id : client) +
// '\x1b[0m unsubscribed to topics: ' + subscriptions.join('\n'), 'from broker', aedes.id)
});
/**
* 客户端取消链接
*/
aedes.on('clientDisconnect', function (client) {
// console.log('Client Disconnected: \x1b[31m' + (client ? client.id : client) + '\x1b[0m', 'to broker', aedes.id)
})
//////////MQTT协议->HTTP路由开发业务更简单//////////
////////////MQTT协议也可以直接调用Model进行业务编写//////////
//https://github.com/UsherYue/aedes/blob/main/examples/clusters/index.js
//////////MQTT协议 转 HTTP//////////
//script file
const scriptName = path.parse(__filename).name;
process.title = `${PROCESS_NAME} Worker Process ${scriptName}`;
}
/**
* port convert
* @param val
* @returns {*}
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
}
/**
*
* @param error
*/
function onError(error) {
switch (error.code) {
case 'EACCES':
colorlog.warning('Error',error.port + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
colorlog.warning('Error',error.port + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
let debug = require('debug')('debugserver');
httpPort&&debug('Express Listening on ' + httpPort);
httpsPort&&debug('Express Listening on ' + httpsPort);
}