磕磕碰碰的完成了 写个日志记录下整个流程
在拿到了阿里云的IP后,安装了Xshell来访问终端:
填写 Host:IP地址 && User Name:root && Password:(root password)
安装配置node.js
1:安装node.js、npm(包管理工具)、n模块(管理node.js版本)
~# cd /usr/local/lib/
/usr/local/lib# sudo apt-get update
/usr/local/lib# sudo apt-get install nodejs
/usr/local/lib# sudo apt-get install npm
/usr/local/lib# node -v
/usr/local/lib# npm -v
/usr/local/lib# sudo npm install -g n
2:升级node.js到稳定版、升级npm //-g 为全局安装
/usr/local/lib# sudo n stable
/usr/local/lib# sudo npm install npm -g
/usr/local/lib# node -v
/usr/local/lib# npm -v
3:安装及更新express模块
/usr/local/lib# npm install express -g
/usr/local/lib# npm update express
搭建 MQTT server
1:安装mosca模块
mosca:MQTT在Node.js中的一个代理服务器开源实现。(MQTT代理服务器也就是MQTT server实现,统称Broker;已有诸多实现,我用的mosca)
/usr/local/lib# npm install mocas --save
/usr/local/lib# cd node_modules/
/usr/local/lib/node_modules# ls
2:创建broker.js
/usr/local/lib/node_modules# mkdir mqtt_test
/usr/local/lib/node_modules# cd mqtt_test/
/usr/local/lib/node_modules/mqtt_test# vi broker.js`
var mosca = require('mosca');
var ascoltatore = {
};
var settings = {
port:80,
backend: ascoltatore
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', packet.payload);
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}
//保存并退出编辑模式 按Esc键 + :wq
3:阿里云实例内添加安全组规则,设置80端口 + 运行broker.js
/usr/local/lib/node_modules/mqtt_test# node broker.js
测试 MQTT Broker
1、运用在线测试工具 MQTTLens 进行测试
打开URL:
https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?utm_source=chrome-ntp-icon
添加应用 + 启动应用 + Connections+
填好后 SAVE CHANGES
2、添加Subscribe信息 + SUBSCRIBE
3、查看终端
到此代理服务器装好了 且能用了。
下篇介绍server 和client 部分代码实现