Mosca lib/client.js

    Description

    Copyright (c) 2013-2014 Matteo Collina, http://matteocollina.com

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

    Source

    "use strict";
    
    var async  = require("async");
    
    function nop() {}

    Client

    function
    Client()
    • @param: {MqttConnection}connThe mqtt connection object for this client
    • @param: {Server}serverThe Mosca server this client will be tied to

    Description

    The Client is just the object modelling a server representation
    of a client

    Source

    function Client(conn, server) {
      this.connection = conn;
      conn.setPacketEncoding('binary');
    
      this.server = server;
      this.logger = server.logger;
      this.subscriptions = {};
    
      this.nextId = 1;
      this.inflight = {};
      this.inflightCounter = 0;
      this._lastDedupId = -1;
    
      this._setup();
    }

    close

    method
    Client.prototype.close()
    • @param: {Function}callbackThe callback to be called when the Client is closed

    Description

    Close the client

    Source

    Client.prototype.close = function(callback) {
    
      callback = callback || nop;
    
      if (this._closed || this._closing) {
        return callback();
      }
    
      var that = this;
    
      if (this.id) {
        that.logger.debug("closing client");
    
        if (this.timer) {
          clearTimeout(this.timer);
        }
      }
    
      var cleanup = function() {
        that._closed = true;
    
        that.logger.info("closed");
        that.connection.removeAllListeners();
        // ignore all errors after disconnection
        that.connection.on("error", function() {});
        that.server.emit("clientDisconnected", that);
    
        callback();
      };
    
      that._closing = true;
    
      async.parallel(Object.keys(that.subscriptions).map(that.unsubscribeMapTo.bind(that)), function(err) {
        if (err) {
          that.logger.info(err);
        }
    
        // needed in case of errors
        if (!that._closed) {
          that.server.persistClient(that);
          cleanup();
          that.connection.stream.end();
        }
      });
    };
    
    module.exports = Client;