Android webview mqtt,mqtt------ mosca服务器端参数简介

一:服务器端

为什么使用mosca:mosca是基于node.js开发,上手难度相对较小,其次协议支持完整,除了不支持Qos 2,其它的基本都支持。持久化支持redis以及mongo。二次开发接口简单。部署非常简单,并且支持docker镜像。

mosca参数简介:

var mosca = require(‘mosca‘)

ascoltatore :  是Mosca作者开发的一个订阅与发布类库,Mosca核心的订阅与发布模型

var ascoltatore ={

type:‘redis‘, //指定类型,mongo的写法请参考官方wiki

redis: require(‘redis‘),

db:1,

port:6379,

return_buffers:true, //to handle binary payloads//指定数据保留多长时间,单位毫秒

ttl: {//TTL for subscriptions is 23 hour

subscriptions: 23 * 60 * 60 * 1000,//TTL for packets is 23 hour

packets: 23 * 60 * 60 * 1000,

},

host:"localhost"};

//基本参数设置

var moscaSettings ={

port:1883, //设置监听端口

backend: ascoltatore,

maxInflightMessages:10240, //设置单条消息的最大长度,超出后服务端会返回//设置WebSocket参数

http: {

port:1884,

bundle:true,static: ‘./‘},//数据持久化参数设置

persistence: {

factory: mosca.persistence.Redis,

db:1,

port:6379,

return_buffers:true, //to handle binary payloads

ttl: {//TTL for subscriptions is 23 hour

subscriptions: 23 * 60 * 60 * 1000,//TTL for packets is 23 hour

packets: 23 * 60 * 60 * 1000, },

host:"localhost"}

}

//如果需要用户登录验证权限,需要改写此方法

//这里以简单判断了用户名和密码为例,真实环境可以连接实际业务系统的鉴权服务

var authenticate =function(client, username, password, callback) {var authorized = (username === ‘test‘ &;&; password.toString() === ‘passwd‘);if (authorized) client.user =username;

callback(null, authorized);

}

function authPub(client, topic, payload, callback) {

callback(null, payload);

}

function authSub(client, topic, callback) {

callback(null, topic);

}var server = newmosca.Server(moscaSettings);

server.on(‘ready‘, setup);

server.on(‘clientConnected‘, function(client) {

console.log(‘client connected‘, client.id);

});

server.on(‘published‘, function(packet, client) {

console.log(‘Published‘, packet.topic +packet.payload);

});//fired when the mqtt server is ready

function setup() {

server.authenticate=authenticate;

server.authorizePublish=authPub;

server.authorizeSubscribe=authSub;

console.log(‘Mosca server is up and running‘)

}

二次开发可以监听的事件列表

clientConnected: when a client is connected; the client is passed as a parameter.

clientDisconnecting: when a client is being disconnected; the client is passed as a parameter.

clientDisconnected: when a client is disconnected; the client is passed as a parameter.

published: when a new message is published; the packet and the client are passed as parameters.

delivered: when a client has sent back a puback for a published message; the packet and the client are passed as parameters.

subscribed: when a client is subscribed to a topic; the topic and the client are passed as parameters.

unsubscribed: when a client is unsubscribed to a topic; the topic and the client are passed as parameters.

有了上面可以监听到事件你就可以根据自己的业务进行相应的开发,拦截特定的事件并添加业务代码

ascoltatore托管地址 https://github.com/mcollina/ascoltatori

高级参数设置可以参考 https://github.com/mcollina/mosca/wiki/Mosca-advanced-usage

权限验证可以参考 https://github.com/mcollina/mosca/wiki/Authentication-&;-Authorization

配置ssl可以参考 https://github.com/mcollina/mosca/wiki/TLS-SSL-Configuration

配置WebSocket可以参考 https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值