def receive_message(expected_id)
    json = @web_socket.recv_frame
    begin
      data = JSON.parse json
    rescue JSONError
      close
      raise RuntimeError, 'Invalid JSON received'
    end
    if data['id']
      # RPC result.
      if data['id'] != expected_id
        close
        raise RuntimeError, 'Out of sequence RPC response id'
      end
      if data['error']
        code = data['error']['code']
        message = data['error']['message']
        raise RuntimeError, "RPC Error #{code}: #{message}"
      end
      return data['result']
    elsif data['method']
      # RPC notice.
      event = { name: data['method'], data: data['params'] }
      @events << event
      return nil
    else
      close
      raise RuntimeError, "Unexpected / invalid RPC message #{data.inspect}"
    end
  end
def receive_message(expected_id)
    json = @web_socket.recv_frame
    begin
      data = JSON.parse json
    rescue JSONError
      close
      raise RuntimeError, 'Invalid JSON received'
    end
    if data['id']
      # RPC result.
      if data['id'] != expected_id
        close
        raise RuntimeError, 'Out of sequence RPC response id'
      end
      if data['error']
        code = data['error']['code']
        message = data['error']['message']
        raise RuntimeError, "RPC Error #{code}: #{message}"
      end
      return data['result']
    elsif data['method']
      # RPC notice.
      event = { name: data['method'], data: data['params'] }
      @events << event
      return nil
    else
      close
      raise RuntimeError, "Unexpected / invalid RPC message #{data.inspect}"
    end
  end
Blocks until a WebKit message is received, then parses it.

 RPC notifications are added to the @events array.

 @param [Integer, nil] expected_id if a RPC response is expected, this
     argument has the response id; otherwise, the argument should be nil
 @return [Hash<String, Object>, nil] a Hash containing the RPC result if an
     expected RPC response was received; nil if an RPC notice was received