Code coverage report for app/utils/stomp_client.js

Statements: 91.25% (73 / 80)      Branches: 82.5% (33 / 40)      Functions: 80.95% (17 / 21)      Lines: 91.25% (73 / 80)      Ignored: none     

All files » app/utils/ » stomp_client.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280    1                                     1                           1                                                                                                               3   3 3 3 3   3 1 1   1   3 3 3                 2 1 1 1   1                     4 4 4 4 4                               1       3 1 2     1         1   1 1 1 1 1 1 1 1 1               1                   2   2 1 1   1                   4   4     4 2   2 1 1   1         1 1 1 1                     3 3 1 1   2       2                 1 1 1 1                   2 1 1 1   1      
'use strict';
 
;require.register("utils/stomp_client", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  /**
   * Example:
   *
   * stompClient.connect();
   * stompClient.subscribe('topic1', handlerFunc1);
   * stompClient.addHandler('topic1', 'handler2-name', handlerFunc2);
   * stompClient.removeHandler('topic1', 'handler2-name');
   * stompClient.unsubscribe('topic1');
   * stompClient.disconnect();
   *
   */
 
  module.exports = Em.Object.extend({
    /**
     * @type {Stomp}
     */
    client: null,
 
    /**
     * @type {string}
     */
    webSocketUrl: '{protocol}://{hostname}{port}/api/stomp/v1/websocket',
 
    /**
     * @type {string}
     */
    sockJsUrl: '{protocol}://{hostname}{port}/api/stomp/v1',
 
    /**
     * sockJs should use only alternative options as transport in case when websocket supported but connection fails
     * @const
     * @type {Array}
     */
    sockJsTransports: ['eventsource', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'],
 
    /**
     * @type {boolean}
     */
    isConnected: false,
 
    /**
     * @type {boolean}
     */
    isWebSocketSupported: true,
 
    /**
     * @type {number}
     * @const
     */
    RECONNECT_TIMEOUT: 6000,
 
    /**
     * @type {object}
     */
    subscriptions: {},
 
    /**
     * default headers
     * @type {object}
     */
    headers: {},
 
    /**
     *
     * @param {boolean} useSockJS
     * @returns {$.Deferred}
     */
    connect: function connect(useSockJS) {
      var _this = this;
 
      var dfd = $.Deferred();
      var socket = this.getSocket(useSockJS);
      var client = Stomp.over(socket);
      var headers = this.get('headers');
 
      client.connect(headers, function () {
        _this.onConnectionSuccess();
        dfd.resolve();
      }, function () {
        dfd.reject(_this.onConnectionError(useSockJS));
      });
      client.debug = Em.K;
      this.set('client', client);
      return dfd.promise();
    },
 
    /**
     *
     * @param {boolean} useSockJS
     * @returns {SockJS|WebSocket}
     */
    getSocket: function getSocket(useSockJS) {
      if (!WebSocket || useSockJS) {
        this.set('isWebSocketSupported', false);
        var sockJsUrl = this.getSocketUrl(this.get('sockJsUrl'), false);
        return new SockJS(sockJsUrl, null, { transports: this.get('sockJsTransports') });
      } else {
        return new WebSocket(this.getSocketUrl(this.get('webSocketUrl'), true));
      }
    },
 
    /**
     *
     * @param {string} template
     * @param {boolean} isWebsocket
     * @returns {string}
     */
    getSocketUrl: function getSocketUrl(template, isWebsocket) {
      var hostname = this.getHostName();
      var isSecure = this.isSecure();
      var protocol = isWebsocket ? isSecure ? 'wss' : 'ws' : isSecure ? 'https' : 'http';
      var port = this.getPort();
      return template.replace('{hostname}', hostname).replace('{protocol}', protocol).replace('{port}', port);
    },
 
    getHostName: function getHostName() {
      return window.location.hostname;
    },
 
    isSecure: function isSecure() {
      return window.location.protocol === 'https:';
    },
 
    getPort: function getPort() {
      return window.location.port ? ':' + window.location.port : '';
    },
 
    onConnectionSuccess: function onConnectionSuccess() {
      this.set('isConnected', true);
    },
 
    onConnectionError: function onConnectionError(useSockJS) {
      if (this.get('isConnected')) {
        this.reconnect(useSockJS);
      } else if (!useSockJS) {
        //if SockJs connection failed too the stop trying to connect
        //if webSocket failed on initial connect then switch to SockJS
        return this.connect(true);
      }
    },
 
    reconnect: function reconnect(useSockJS) {
      var _this2 = this;
 
      var subscriptions = Object.assign({}, this.get('subscriptions'));
      setTimeout(function () {
        console.debug('Reconnecting to WebSocket...');
        _this2.connect(useSockJS).done(function () {
          _this2.set('subscriptions', {});
          for (var i in subscriptions) {
            _this2.subscribe(subscriptions[i].destination, subscriptions[i].handlers['default']);
            for (var key in subscriptions[i].handlers) {
              key !== 'default' && _this2.addHandler(subscriptions[i].destination, key, subscriptions[i].handlers[key]);
            }
          }
        });
      }, this.RECONNECT_TIMEOUT);
    },
 
    disconnect: function disconnect() {
      this.get('client').disconnect();
    },
 
    /**
     *
     * @param {string} destination
     * @param {string} body
     * @param {object} headers
     */
    send: function send(destination, body) {
      var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
 
      if (this.get('client.connected')) {
        this.get('client').send(destination, headers, body);
        return true;
      }
      return false;
    },
 
    /**
     *
     * @param destination
     * @param {function} handler
     * @returns {*}
     */
    subscribe: function subscribe(destination) {
      var handler = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Em.K;
 
      var handlers = {
        default: handler
      };
      if (!this.get('client.connected')) {
        return null;
      }
      if (this.get('subscriptions')[destination]) {
        console.error('Subscription with default handler for ' + destination + ' already exists');
        return this.get('subscriptions')[destination];
      } else {
        var subscription = this.get('client').subscribe(destination, function (message) {
          for (var i in handlers) {
            handlers[i](JSON.parse(message.body));
          }
        });
        subscription.destination = destination;
        subscription.handlers = handlers;
        this.get('subscriptions')[destination] = subscription;
        return subscription;
      }
    },
 
    /**
     * If trying to add handler to not existing subscription then it will be created and handler added as default
     * @param {string} destination
     * @param {string} key
     * @param {function} handler
     */
    addHandler: function addHandler(destination, key, handler) {
      var subscription = this.get('subscriptions')[destination];
      if (!subscription) {
        this.subscribe(destination);
        return this.addHandler(destination, key, handler);
      }
      Iif (subscription.handlers[key]) {
        console.error('You can\'t override subscription handler');
        return;
      }
      subscription.handlers[key] = handler;
    },
 
    /**
     * If removed handler is last and subscription have zero handlers then topic will be unsubscribed
     * @param {string} destination
     * @param {string} key
     */
    removeHandler: function removeHandler(destination, key) {
      var subscription = this.get('subscriptions')[destination];
      delete subscription.handlers[key];
      Eif (Em.keys(subscription.handlers).length === 0) {
        this.unsubscribe(destination);
      }
    },
 
    /**
     *
     * @param {string} destination
     * @returns {boolean}
     */
    unsubscribe: function unsubscribe(destination) {
      if (this.get('subscriptions')[destination]) {
        this.get('subscriptions')[destination].unsubscribe();
        delete this.get('subscriptions')[destination];
        return true;
      }
      return false;
    }
  });
});