|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +class Wamp { |
| 4 | + constructor(debug = false) { |
| 5 | + this.debug = debug; |
| 6 | + this.connection = undefined; |
| 7 | + this.requests = []; |
| 8 | + this._requestsOnProgress = false; |
| 9 | + |
| 10 | + this._initOptions(); |
| 11 | + this._initConnection(); |
| 12 | + } |
| 13 | + |
| 14 | + _initOptions() { |
| 15 | + this.options = { |
| 16 | + 'lazy': true, |
| 17 | + 'api_procedure': 'api', |
| 18 | + 'url': 'wss://' + window.location.hostname + '/wamp', |
| 19 | + 'realm': 'public', |
| 20 | + 'cookie_session_name': 'sid', |
| 21 | + 'cookie_session_separator': '~', |
| 22 | + 'auth_secret': window.navigator.userAgent, |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + _initConnection() { |
| 27 | + if (!this.options.lazy) this.connect(); |
| 28 | + } |
| 29 | + |
| 30 | + _isConnecting() { |
| 31 | + return this.connection && this.connection.isOnProgress(); |
| 32 | + } |
| 33 | + |
| 34 | + _isConnected() { |
| 35 | + return this.connection && this.connection.isReady(); |
| 36 | + } |
| 37 | + |
| 38 | + connect() { |
| 39 | + if (this._isConnected()) { |
| 40 | + throw new Error('WAMP. Connection already done.'); |
| 41 | + } |
| 42 | + if (this._isConnecting()) { |
| 43 | + throw new Error('WAMP. Connection already in progress.'); |
| 44 | + } |
| 45 | + |
| 46 | + let wampAuthChallenge = this._createAuthChallenge(); |
| 47 | + |
| 48 | + let options = this.options; |
| 49 | + this._debugNotice( |
| 50 | + `Connection:`, |
| 51 | + `Url "${options.url}".`, |
| 52 | + `Realm "${options.realm}".`, |
| 53 | + `Authentication challenge:`, wampAuthChallenge |
| 54 | + ); |
| 55 | + try { |
| 56 | + this.connection = new WampConnection(options.url, options.realm, wampAuthChallenge); |
| 57 | + this.connection |
| 58 | + .onOpen((connection) => this._onConnectResolve(connection)) |
| 59 | + .onClose((reason, details) => this._onConnectReject(reason, details)) |
| 60 | + .open(); |
| 61 | + } catch (error) { |
| 62 | + this._onConnectReject('error', error); |
| 63 | + } |
| 64 | + |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + close() { |
| 69 | + if (this._isConnected()) { |
| 70 | + this.connection.close(); |
| 71 | + this._debugNotice(`Connection closing.`); |
| 72 | + } |
| 73 | + |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * If return not WampAuthChallenge instance then connection without authentication |
| 79 | + */ |
| 80 | + _createAuthChallenge() { |
| 81 | + let options = this.options; |
| 82 | + this._debugNotice( |
| 83 | + `Cookie session:`, |
| 84 | + `Name "${options.cookie_session_name}".`, |
| 85 | + `Separator "${options.cookie_session_separator}".` |
| 86 | + ); |
| 87 | + let wampCookieSession = new WampCookieSession(options.cookie_session_name, options.cookie_session_separator); |
| 88 | + |
| 89 | + this._debugNotice( |
| 90 | + `Authentication challenge:`, |
| 91 | + `ID "${wampCookieSession.getId()}".`, |
| 92 | + `Secret "${options.auth_secret}".` |
| 93 | + ); |
| 94 | + return new WampAuthChallenge(wampCookieSession.getId(), options.auth_secret); |
| 95 | + } |
| 96 | + |
| 97 | + _onConnectResolve(connection) { |
| 98 | + this._debugNotice(`Connection ready:`, connection); |
| 99 | + this._runRequests(); |
| 100 | + } |
| 101 | + |
| 102 | + _onConnectReject(reason, details) { |
| 103 | + let isClosedByClient = false; |
| 104 | + let reconnectionState = 'unknown'; |
| 105 | + let reconnectionTry = 0; |
| 106 | + let reconnectionDelay = 0; |
| 107 | + if (details.hasOwnProperty('reason')) { |
| 108 | + isClosedByClient = this.connection.isDetailsClosedByClient(details); |
| 109 | + reconnectionState = this.connection.getDetailsReconnectionState(details); |
| 110 | + reconnectionTry = this.connection.getDetailsReconnectionTry(details); |
| 111 | + reconnectionDelay = this.connection.getDetailsReconnectionDelay(details); |
| 112 | + } |
| 113 | + if (isClosedByClient) { |
| 114 | + reason = 'closed by client'; |
| 115 | + } |
| 116 | + |
| 117 | + let message = [ |
| 118 | + `Connection closed:`, |
| 119 | + `Reason "${reason}".`, |
| 120 | + ]; |
| 121 | + if (isClosedByClient) { |
| 122 | + this._debugNotice.apply(this, message); |
| 123 | + } else { |
| 124 | + message.concat([ |
| 125 | + `Details:`, details, |
| 126 | + `Reconnection state "${reconnectionState}".`, |
| 127 | + `Reconnection try "${reconnectionTry}".`, |
| 128 | + `Reconnection delay "${reconnectionDelay}".`, |
| 129 | + ]); |
| 130 | + this._debugError.apply(this, message); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + request(procedure, data = undefined) { |
| 135 | + this._debugNotice( |
| 136 | + `Request add:`, |
| 137 | + `Procedure "${procedure}".`, |
| 138 | + `Data:`, data |
| 139 | + ); |
| 140 | + let request = { |
| 141 | + 'procedure': procedure, |
| 142 | + 'data': data, |
| 143 | + 'resolve': undefined, |
| 144 | + 'reject': undefined, |
| 145 | + }; |
| 146 | + this.requests.push(request); |
| 147 | + |
| 148 | + return new Promise((resolve, reject) => { |
| 149 | + request.resolve = resolve; |
| 150 | + request.reject = reject; |
| 151 | + |
| 152 | + this._runRequests(); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + requestApi(resurce, method, data = undefined) { |
| 157 | + data = WampApiRequest.normalizeCallData(data); |
| 158 | + data.unshift(method); |
| 159 | + data.unshift(resurce); |
| 160 | + return this.request(this.options.api_procedure, data); |
| 161 | + } |
| 162 | + |
| 163 | + _runRequests() { |
| 164 | + if (!this._isConnected()) { |
| 165 | + if (this._isConnecting()) return; |
| 166 | + return this.connect(); |
| 167 | + } |
| 168 | + |
| 169 | + if (this._requestsOnProgress) return; |
| 170 | + this._requestsOnProgress = true; |
| 171 | + |
| 172 | + for (let i in this.requests) { |
| 173 | + if (!this.requests.hasOwnProperty(i)) continue; |
| 174 | + let request = this.requests[i]; |
| 175 | + |
| 176 | + this._debugNotice( |
| 177 | + `Request run:`, |
| 178 | + `Procedure "${request.procedure}".`, |
| 179 | + `Data:`, request.data |
| 180 | + ); |
| 181 | + try { |
| 182 | + new WampRequest(this.connection) |
| 183 | + .request(request.procedure, request.data) |
| 184 | + .then(response => this._onRequestResolve(request, response)) |
| 185 | + .catch(error => this._onRequestReject(request, error)); |
| 186 | + } catch (error) { |
| 187 | + this._onRequestReject(error, request.reject); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + this.requests = []; |
| 192 | + this._requestsOnProgress = false; |
| 193 | + } |
| 194 | + |
| 195 | + _onRequestResolve(request, response) { |
| 196 | + this._debugNotice( |
| 197 | + `Request response:`, |
| 198 | + `Procedure "${request.procedure}".`, |
| 199 | + `Data:`, request.data, |
| 200 | + `Response:`, response, |
| 201 | + ); |
| 202 | + request.resolve(response); |
| 203 | + } |
| 204 | + |
| 205 | + _onRequestReject(request, error) { |
| 206 | + this._debugError( |
| 207 | + `Request error:`, |
| 208 | + `Procedure "${request.procedure}".`, |
| 209 | + `Data:`, request.data, |
| 210 | + `Error:`, error, |
| 211 | + ); |
| 212 | + request.reject(error); |
| 213 | + } |
| 214 | + |
| 215 | + _debugNotice(...args) { |
| 216 | + args.unshift(false); |
| 217 | + this._debug.apply(this, args); |
| 218 | + } |
| 219 | + |
| 220 | + _debugError(...args) { |
| 221 | + args.unshift(true); |
| 222 | + this._debug.apply(this, args); |
| 223 | + } |
| 224 | + |
| 225 | + _debug(isError, ...args) { |
| 226 | + if (!this.debug) return; |
| 227 | + args.unshift('WAMP.'); |
| 228 | + let log = Function.prototype.bind.call( |
| 229 | + isError ? console.error : console.log, |
| 230 | + console |
| 231 | + ); |
| 232 | + log.apply(console, args); |
| 233 | + } |
| 234 | +} |
0 commit comments