我在使用 phantomjs.rb 时无法从我的应用程序中获取屏幕截图 gem 。我目前只是在尝试本地网址。如果我让它们正常工作,我稍后会使用 url_for。
在我的 Controller 中:
Phantomjs.run('./public/javascripts/testScreenCapture.js', 'http://localhost:3000')
还有我的 testScreenCapture.js:
var page = require('webpage').create();
var args = require('system').args;
var url = args[1];
page.open(url, function () {
window.setTimeout(function () {
page.render('./public/appPage.png');
phantom.exit();
}, 3000);
});
传递“http://www.google.com”作为 url 参数工作正常,当我从本地服务器传递任何东西时,rails 服务器挂起,我假设 phantomjs 脚本也是如此,因为我没有得到屏幕截图在我的公共(public)文件夹中。
更奇怪的是从命令行运行它:
phantomjs public/javascripts/testScreenCapture.js http://localhost:3000
这很好用!所以我假设我的 Controller 与服务器发生冲突是有问题的。有什么想法吗?
最佳答案
你必须使用像puma这样的多线程网络服务器.
参见 here了解不同选项的概览。
另一个陷阱可能是您的 Controller 逻辑。如果由 phantom 呈现的页面触发与调用 phantom 的页面相同的 Controller 操作,则会创建一个无限循环。
然后,即使是多线程服务器也会卡住:)
关于javascript - 尝试在我的 Rails 应用程序中截屏页面时 PhantomJS.rb 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32957020/