mp-mqtt
Version: 
微信小程序版 MQTT.js
110 lines (109 loc) • 3.76 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Store = exports.MqttClient = exports.connect = void 0;
var debug_1 = require("debug");
var url = require("url");
var wx_1 = require("./wx");
var MqttClient = require('mqtt/lib/client');
exports.MqttClient = MqttClient;
var Store = require('mqtt/lib/store');
exports.Store = Store;
var debug = debug_1.default('mqttjs');
// TODO:兼容wx和ws协议
var protocols = {
    wx: wx_1.buildStream,
    wxs: wx_1.buildStream,
    ws: require('mqtt/lib/connect/ws'),
    wss: require('mqtt/lib/connect/ws')
};
function getNormalizeProtocol(protocol) {
    // TODO: 根据运行时环境自动处理
    return protocol;
}
/**
 * Parse the auth attribute and merge username and password in the options object.
 *
 */
function parseAuthOptions(opts) {
    var matches;
    if (opts.auth) {
        matches = opts.auth.match(/^(.+):(.+)$/);
        if (matches) {
            opts.username = matches[1];
            opts.password = matches[2];
        }
        else {
            opts.username = opts.auth;
        }
    }
}
function connect(opts_or_brokerUrl, _opts) {
    debug('connecting to an MQTT broker...');
    var brokerUrl = null;
    var opts = {};
    if (typeof opts_or_brokerUrl === 'string') {
        brokerUrl = opts_or_brokerUrl;
        opts = _opts || {};
    }
    else {
        opts = opts_or_brokerUrl || {};
    }
    if (brokerUrl) {
        var parsed = url.parse(brokerUrl, true);
        if (parsed.port != null) {
            // @ts-ignore
            parsed.port = Number(parsed.port);
        }
        opts = __assign(__assign({}, parsed), opts);
        if (opts.protocol === null || opts.protocol === undefined) {
            throw new Error('Missing protocol');
        }
        opts.protocol = opts.protocol.replace(/:$/, '');
    }
    // merge in the auth options if supplied
    parseAuthOptions(opts);
    // support clientId passed in the query string of the url
    if (opts.query && typeof opts.query.clientId === 'string') {
        opts.clientId = opts.query.clientId;
    }
    if (opts.clean === false && !opts.clientId) {
        throw new Error('Missing clientId for unclean clients');
    }
    var defaultProtocol = opts.protocol;
    if (!opts.protocol || typeof protocols[opts.protocol] !== 'function') {
        throw new Error('不支持的协议');
    }
    var _client = new MqttClient(function (client) {
        if (opts.servers) {
            if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
                client._reconnectCount = 0;
            }
            var server = opts.servers[client._reconnectCount];
            if (server) {
                opts.host = server.host;
                opts.port = server.port;
                opts.protocol = getNormalizeProtocol(server.protocol || defaultProtocol);
                opts.hostname = opts.host;
            }
            client._reconnectCount++;
        }
        debug('calling streambuilder for', opts.protocol);
        return protocols[opts.protocol](client, opts);
    }, opts);
    _client.on('error', function () {
        /* Automatically set up client error handling */
    });
    return _client;
}
exports.connect = connect;