src/protocolMessaging/index.js
// 4. Receive an "ok" and start sending and receiving RPC
4. Receive an "ok" and start sending and receiving RPC
4 . Receive an " ok " and start sending and receiving RPC
connect(reconnect = true) {
    if (this.supportLegacy) {
      return new Promise((resolve, reject) => {
        this.socket = new WebSocket(this.socketUrl)
        this.socket.onopen = function healthCheck() {
          this.isAlive = true
          if (this.ping) {
            this.addEventListener('pong', () => {
              this.isAlive = true
            })
            this.interval = setInterval(() => {
              if (this.isAlive === false) {
                console.log('no response for 30s; closing socket')
                this.close()
              }
              this.isAlive = false
              this.ping()
            }, 30000)
          }
        }
        this.socket.onclose = () => {
          this.isAuthenticated = false
          clearInterval(this.socket.interval)
          if (reconnect) {
            console.log('socket closed; attempting reconnect in 10s')
            setTimeout(() => {
              this.connect()
            }, 10000)
          } else {
            reject('socket closed')
          }
        }
        this.socket.onerror = event => {
          reject(event)
        }
        this.socket.onmessage = event => {
          if (!this.isAuthenticated && this.requireAuthentication) {
            switch (event.data) {
              case 'ok':
                this.isAuthenticated = true
                console.log('Authentication successful')
                resolve(event.data)
                break
              case 'not authorized':
                reject(new Error('Address is not authorized.'))
                break
              default:
                this.messageSigner(event.data).then(signature => {
                  this.socket.send(signature)
                })
            }
          } else if (!this.isAuthenticated && !this.requireAuthentication) {
            if (event.data === 'ok') {
              this.isAuthenticated = true
              console.log('Authentication successful')
              resolve(event.data)
            }
          } else if (this.isAuthenticated) {
            let payload
            let message
            try {
              payload = JSON.parse(event.data)
              message = payload.message && JSON.parse(payload.message)
              payload.message = message
            } catch (e) {
              console.error('Error parsing payload', e, payload)
            }
            if (!payload || !message) {
              return
            }
            if (message.method) {
              if (this.RPC_METHOD_ACTIONS[message.method]) {
                this.RPC_METHOD_ACTIONS[message.method](payload)
              }
            } else if (message.id) {
              const isError = Object.prototype.hasOwnProperty.call(message, 'error')
              if (!isError && message.result) {
                if (typeof this.RESOLVERS[message.id] === 'function') {
                  this.RESOLVERS[message.id](message.result)
                }
              } else if (isError) {
                if (typeof this.REJECTORS[message.id] === 'function') {
                  this.REJECTORS[message.id](message.error)
                }
              }
              delete this.RESOLVERS[message.id]
              delete this.REJECTORS[message.id]
              clearTimeout(this.TIMEOUTS[message.id])
            }
          }
        }
      })
    }
    return Promise.resolve('ok')
  }
connect ( reconnect = true ) { if ( this . supportLegacy ) { return new Promise ( ( resolve , reject ) => { this . socket = new WebSocket ( this . socketUrl ) this . socket . onopen = function healthCheck ( ) { this . isAlive = true if ( this . ping ) { this . addEventListener ( 'pong' , ( ) => { this . isAlive = true } ) this . interval = setInterval ( ( ) => { if ( this . isAlive === false ) { console . log ( 'no response for 30s; closing socket' ) this . close ( ) } this . isAlive = false this . ping ( ) } , 30000 ) } } this . socket . onclose = ( ) => { this . isAuthenticated = false clearInterval ( this . socket . interval ) if ( reconnect ) { console . log ( 'socket closed; attempting reconnect in 10s' ) setTimeout ( ( ) => { this . connect ( ) } , 10000 ) } else { reject ( 'socket closed' ) } } this . socket . onerror = event => { reject ( event ) } this . socket . onmessage = event => { if ( ! this . isAuthenticated && this . requireAuthentication ) { switch ( event . data ) { case 'ok' : this . isAuthenticated = true console . log ( 'Authentication successful' ) resolve ( event . data ) break case 'not authorized' : reject ( new Error ( 'Address is not authorized.' ) ) break default : this . messageSigner ( event . data ) . then ( signature => { this . socket . send ( signature ) } ) } } else if ( ! this . isAuthenticated && ! this . requireAuthentication ) { if ( event . data === 'ok' ) { this . isAuthenticated = true console . log ( 'Authentication successful' ) resolve ( event . data ) } } else if ( this . isAuthenticated ) { let payload let message try { payload = JSON . parse ( event . data ) message = payload . message && JSON . parse ( payload . message ) payload . message = message } catch ( e ) { console . error ( 'Error parsing payload' , e , payload ) } if ( ! payload || ! message ) { return } if ( message . method ) { if ( this . RPC_METHOD_ACTIONS [ message . method ] ) { this . RPC_METHOD_ACTIONS [ message . method ] ( payload ) } } else if ( message . id ) { const isError = Object . prototype . hasOwnProperty . call ( message , 'error' ) if ( ! isError && message . result ) { if ( typeof this . RESOLVERS [ message . id ] === 'function' ) { this . RESOLVERS [ message . id ] ( message . result ) } } else if ( isError ) { if ( typeof this . REJECTORS [ message . id ] === 'function' ) { this . REJECTORS [ message . id ] ( message . error ) } } delete this . RESOLVERS [ message . id ] delete this . REJECTORS [ message . id ] clearTimeout ( this . TIMEOUTS [ message . id ] ) } } } } ) } return Promise . resolve ( 'ok' ) }
4.
4 .
// Promisify the `onmessage` handler. Allows us to return information // about the connection state after the authentication handshake // Check socket health every 30 seconds // trying to make this isomorphic, and ping/pong isn't supported in browser websocket api // The connection was closed // There was an error on the connection // Received a message // We are authenticating // We have completed the challenge. // We have been issued a challenge. // We are already authenticated and are receiving an RPC. // Another peer is invoking a method. // We have received a response from a method call. // Resolve the call if a resolver exists. // Reject the call if a resolver exists. // Call lifecycle finished; tear down resolver, rejector, and timeout