var Client = function()

in activemq-web-demo/src/main/webapp/mqtt/mqttws31.js [1441:1725]


    var Client = function (host, port, clientId) {
    	if (typeof host !== "string")
        	throw new Error(format(ERROR.INVALID_TYPE, [typeof host, "host"]));
    	if (typeof port !== "number" || port < 0)
        	throw new Error(format(ERROR.INVALID_TYPE, [typeof port, "port"]));
    	
    	var clientIdLength = 0;
    	for (var i = 0; i<clientId.length; i++) {
    		var charCode = clientId.charCodeAt(i);                   
    		if (0xD800 <= charCode && charCode <= 0xDBFF)  {    			
                 i++; // Surrogate pair.
            }   		   
    		clientIdLength++;
    	}     	   	
        if (typeof clientId !== "string" || clientIdLength < 1 | clientIdLength > 23)
        	throw new Error(format(ERROR.INVALID_ARGUMENT, [clientId, "clientId"])); 
    	
        var client = new ClientImpl(host, port, clientId);
        this._getHost =  function() { return client.host; };
    	this._setHost = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
         	
        this._getPort = function() { return client.port; };
    	this._setPort = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
    	
    	this._getClientId = function() { return client.clientId; };
    	this._setClientId = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); };
        
        this._getOnConnectionLost = function() { return client.onConnectionLost; };
        this._setOnConnectionLost = function(newOnConnectionLost) { 
            if (typeof newOnConnectionLost === "function")
            	client.onConnectionLost = newOnConnectionLost;
            else 
    			throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnectionLost, "onConnectionLost"]));
        };

        this._getOnMessageDelivered = function() { return client.onMessageDelivered; };
    	this._setOnMessageDelivered = function(newOnMessageDelivered) { 
    		if (typeof newOnMessageDelivered === "function")
    			client.onMessageDelivered = newOnMessageDelivered;
    		else 
    			throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageDelivered, "onMessageDelivered"]));
    	};
       
        this._getOnMessageArrived = function() { return client.onMessageArrived; };
    	this._setOnMessageArrived = function(newOnMessageArrived) { 
    		if (typeof newOnMessageArrived === "function")
    			client.onMessageArrived = newOnMessageArrived;
    		else 
    			throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageArrived, "onMessageArrived"]));
    	};
        
        /** 
         * Connect this Messaging client to its server. 
         * 
         * @name Messaging.Client#connect
         * @function
         * @param {Object} [connectOptions] attributes used with the connection. 
         * <p>
         * Properties of the connect options are: 
         * @config {number} [timeout] If the connect has not succeeded within this number of seconds, it is deemed to have failed.
         *                            The default is 30 seconds.
         * @config {string} [userName] Authentication username for this connection.
         * @config {string} [password] Authentication password for this connection.
         * @config {Messaging.Message} [willMessage] sent by the server when the client disconnects abnormally.
         * @config {Number} [keepAliveInterval] the server disconnects this client if there is no activity for this
         *                number of seconds. The default value of 60 seconds is assumed if not set.
         * @config {boolean} [cleanSession] if true(default) the client and server persistent state is deleted on successful connect.
         * @config {boolean} [useSSL] if present and true, use an SSL Websocket connection.
         * @config {object} [invocationContext] passed to the onSuccess callback or onFailure callback.
         * @config {function} [onSuccess] called when the connect acknowledgement has been received from the server.
         * A single response object parameter is passed to the onSuccess callback containing the following fields:
         * <ol>
         * <li>invocationContext as passed in to the onSuccess method in the connectOptions.       
         * </ol>
         * @config {function} [onFailure] called when the connect request has failed or timed out.
         * A single response object parameter is passed to the onFailure callback containing the following fields:
         * <ol>
         * <li>invocationContext as passed in to the onFailure method in the connectOptions.       
         * <li>errorCode a number indicating the nature of the error.
         * <li>errorMessage text describing the error.      
         * </ol>
         * @config {Array} [hosts] If present this set of hostnames is tried in order in place 
         * of the host and port paramater on the construtor. The hosts and the matching ports are tried one at at time in order until
         * one of then succeeds.
         * @config {Array} [ports] If present this set of ports matching the hosts.
         * @throws {InvalidState} if the client is not in disconnected state. The client must have received connectionLost
         * or disconnected before calling connect for a second or subsequent time.
         */
        this.connect = function (connectOptions) {
        	connectOptions = connectOptions || {} ;
        	validate(connectOptions,  {timeout:"number",
        			                   userName:"string", 
        		                       password:"string", 
        		                       willMessage:"object", 
        		                       keepAliveInterval:"number", 
        		                       cleanSession:"boolean", 
        		                       useSSL:"boolean",
        		                       invocationContext:"object", 
      		                           onSuccess:"function", 
      		                           onFailure:"function",
      		                           hosts:"object",
      		                           ports:"object"});
        	
        	// If no keep alive interval is set, assume 60 seconds.
            if (connectOptions.keepAliveInterval === undefined)
            	connectOptions.keepAliveInterval = 60;

        	if (connectOptions.willMessage) {
                if (!(connectOptions.willMessage instanceof Message))
            	    throw new Error(format(ERROR.INVALID_TYPE, [connectOptions.willMessage, "connectOptions.willMessage"]));
                // The will message must have a payload that can be represented as a string.
                // Cause the willMessage to throw an exception if this is not the case.
            	connectOptions.willMessage.stringPayload;
            	
            	if (typeof connectOptions.willMessage.destinationName === "undefined")
                	throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.willMessage.destinationName, "connectOptions.willMessage.destinationName"]));
        	}
        	if (typeof connectOptions.cleanSession === "undefined")
        		connectOptions.cleanSession = true;
        	if (connectOptions.hosts) {
        		if (!connectOptions.ports)
        			throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"]));
        		if (!(connectOptions.hosts instanceof Array) )
        			throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts, "connectOptions.hosts"]));
        		if (!(connectOptions.ports instanceof Array) )
        			throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"]));
        		if (connectOptions.hosts.length <1 )
        			throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts, "connectOptions.hosts"]));
        		if (connectOptions.hosts.length != connectOptions.ports.length)
        			throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"]));
        		for (var i = 0; i<connectOptions.hosts.length; i++) {
        			if (typeof connectOptions.hosts[i] !== "string")
        	        	throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.hosts[i], "connectOptions.hosts["+i+"]"]));
        			if (typeof connectOptions.ports[i] !== "number" || connectOptions.ports[i] < 0)
        	        	throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.ports[i], "connectOptions.ports["+i+"]"]));
        	    }
        	}

        	client.connect(connectOptions);
        };
     
        /** 
         * Subscribe for messages, request receipt of a copy of messages sent to the destinations described by the filter.
         * 
         * @name Messaging.Client#subscribe
         * @function
         * @param {string} filter describing the destinations to receive messages from.
         * <br>
         * @param {object} [subscribeOptions] used to control the subscription, as follows:
         * <p>
         * @config {number} [qos] the maiximum qos of any publications sent as a result of making this subscription.
         * @config {object} [invocationContext] passed to the onSuccess callback or onFailure callback.
         * @config {function} [onSuccess] called when the subscribe acknowledgement has been received from the server.
         * A single response object parameter is passed to the onSuccess callback containing the following fields:
         * <ol>
         * <li>invocationContext if set in the subscribeOptions.       
         * </ol>
         * @config {function} [onFailure] called when the subscribe request has failed or timed out.
         * A single response object parameter is passed to the onFailure callback containing the following fields:
         * <ol>
         * <li>invocationContext if set in the subscribeOptions.       
         * <li>errorCode a number indicating the nature of the error.
         * <li>errorMessage text describing the error.      
         * </ol>
         * @config {number} [timeout] which if present determines the number of seconds after which the onFailure calback is called
         * the presence of a timeout does not prevent the onSuccess callback from being called when the MQTT Suback is eventually received.         
    	 * @throws {InvalidState} if the client is not in connected state.
         */
        this.subscribe = function (filter, subscribeOptions) {
        	if (typeof filter !== "string")
        		throw new Error("Invalid argument:"+filter);
        	subscribeOptions = subscribeOptions || {} ;
        	validate(subscribeOptions,  {qos:"number", 
        		                         invocationContext:"object", 
        		                         onSuccess:"function", 
        		                         onFailure:"function",
        		                         timeout:"number"
        		                        });
        	if (subscribeOptions.timeout && !subscribeOptions.onFailure)
        		throw new Error("subscribeOptions.timeout specified with no onFailure callback.");
        	if (typeof subscribeOptions.qos !== "undefined" 
        		&& !(subscribeOptions.qos === 0 || subscribeOptions.qos === 1 || subscribeOptions.qos === 2 ))
    			throw new Error(format(ERROR.INVALID_ARGUMENT, [subscribeOptions.qos, "subscribeOptions.qos"]));
            client.subscribe(filter, subscribeOptions);
        };

        /**
         * Unsubscribe for messages, stop receiving messages sent to destinations described by the filter.
         * 
         * @name Messaging.Client#unsubscribe
         * @function
         * @param {string} filter describing the destinations to receive messages from.
         * @param {object} [unsubscribeOptions] used to control the subscription, as follows:
         * <p>
         * @config {object} [invocationContext] passed to the onSuccess callback or onFailure callback.
         * @config {function} [onSuccess] called when the unsubscribe acknowledgement has been receive dfrom the server.
         * A single response object parameter is passed to the onSuccess callback containing the following fields:
         * <ol>
         * <li>invocationContext if set in the unsubscribeOptions.     
         * </ol>
         * @config {function} [onFailure] called when the unsubscribe request has failed or timed out.
         * A single response object parameter is passed to the onFailure callback containing the following fields:
         * <ol>
         * <li>invocationContext if set in the unsubscribeOptions.       
         * <li>errorCode a number indicating the nature of the error.
         * <li>errorMessage text describing the error.      
         * </ol>
         * @config {number} [timeout] which if present determines the number of seconds after which the onFailure callback is called, the
         * presence of a timeout does not prevent the onSuccess callback from being called when the MQTT UnSuback is eventually received.
         * @throws {InvalidState} if the client is not in connected state.
         */
        this.unsubscribe = function (filter, unsubscribeOptions) {
        	if (typeof filter !== "string")
        		throw new Error("Invalid argument:"+filter);
        	unsubscribeOptions = unsubscribeOptions || {} ;
        	validate(unsubscribeOptions,  {invocationContext:"object", 
        		                           onSuccess:"function", 
        		                           onFailure:"function",
        		                           timeout:"number"
        		                          });
        	if (unsubscribeOptions.timeout && !unsubscribeOptions.onFailure)
        		throw new Error("unsubscribeOptions.timeout specified with no onFailure callback.");
            client.unsubscribe(filter, unsubscribeOptions);
        };

        /**
         * Send a message to the consumers of the destination in the Message.
         * 
         * @name Messaging.Client#send
         * @function 
         * @param {Messaging.Message} message to send.
         
         * @throws {InvalidState} if the client is not in connected state.
         */   
        this.send = function (message) {       	
            if (!(message instanceof Message))
                throw new Error("Invalid argument:"+typeof message);
            if (typeof message.destinationName === "undefined")
            	throw new Error("Invalid parameter Message.destinationName:"+message.destinationName);
           
            client.send(message);   
        };
        
        /** 
         * Normal disconnect of this Messaging client from its server.
         * 
         * @name Messaging.Client#disconnect
         * @function
         * @throws {InvalidState} if the client is not in connected or connecting state.     
         */
        this.disconnect = function () {
        	client.disconnect();
        };
        
        /** 
         * Get the contents of the trace log.
         * 
         * @name Messaging.Client#getTraceLog
         * @function
         * @return {Object[]} tracebuffer containing the time ordered trace records.
         */
        this.getTraceLog = function () {
        	return client.getTraceLog();
        }
        
        /** 
         * Start tracing.
         * 
         * @name Messaging.Client#startTrace
         * @function
         */
        this.startTrace = function () {
        	client.startTrace();
        };
        
        /** 
         * Stop tracing.
         * 
         * @name Messaging.Client#stopTrace
         * @function
         */
        this.stopTrace = function () {
            client.stopTrace();
        };
    };