如何使用socket.io从远程jquery客户端控制CasperJS自动化脚本 [英] How to control a CasperJS automation script from a remote jquery client using socket.io
问题描述
我在CasperJS中有一个自动化脚本,用于控制PhantomJS无头浏览器,该浏览器登录到站点,并通过多个页面/表单输入数据.
I have an automation script in CasperJS controlling a PhantomJS headless browser that logs into a site, enters data over multiple pages / form.
在同一台物理服务器上,我有PHP/MySQL服务于CRM客户网站.在CRM网站上,我希望能够:
From the same physical server, I have PHP/MySQL serving up a CRM client website. On the CRM site, I want to have the ability to:
- 触发远程CasperJS脚本以浏览远程站点并登录并填写表格
- 读取输出流(即第1页已完成,第2页已完成"等)
- 在执行CasperJS脚本时向客户端用户显示状态更新
我在想socket.io是这张票.但是,我要把这一切弄错了吗?我试图避免运行硒服务器.我在此答案上进行了检查,但我不是在寻找屏幕截图, m寻找CasperJS的控制台输出以显示在客户端网站上.
I am thinking that socket.io is the ticket here. But, I am I going about this all wrong? I am trying to avoid having a selenium server running. I checked this answer on SO but I am not looking for screenshots, I'm looking for the console output from CasperJS to be displayed in the client website.
推荐答案
我曾经做过类似的任务,并且使用带有Socket.io的本地Express.js服务器构想了一个解决方案.
I had a similar task once and concocted a solution using local Express.js server with Socket.io.
您将使用node.js启动此服务器,然后通过向http://127.0.0.1:9000
发出POST请求从PHP将任务传递给它(我使用了出色的请求库).
You would launch this server with node.js and then pass tasks to it from PHP by making POST requests to http://127.0.0.1:9000
(I used the excellent Requests library).
这是我的脚本的简化版本:
Here's a simplified version of my script:
var fs = require("fs");
var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server);
var iosocket;
// Express middleware to get variables from POST request
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// Create websocket connection
io.on("connection", function(socket){
console.log('io.js connection');
iosocket = socket;
});
// Receieve task from external POST request
app.post("/scrape", function(req, res){
res.send("Request accepted");
// Url to parse
var url = req.body.url;
// Variable to collect data from scraper
var data = [];
// Launch scraping script
var spawn = require('child_process').spawn,
child = spawn('/path/to/casperjs', ['/path/to/scrape/script.js', url]);
console.log("Spawned parser");
// Receieve data from script
child.stdout.on('data', function (data) {
var message = data.toString();
data.push(message);
// Send data to the web client
iosocket.emit("message", message);
});
// On error
child.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
// On scraper exit
child.on('close', function (code) {
console.log("Scraper exited with code: " + code);
//
// Put data into a file or a database, for example
//
fs.writeFileSync("path/to/file/results_" + (new Date()).getTime() + ".json", JSON.stringify(data));
});
});
// Bind app to port @ localhost
server.listen(9000, "127.0.0.1");
使用CasperJS/Phantomjs服务器的解决方案很有趣,但是人们指出它会泄漏内存,如果您运行短暂的CasperJS脚本,这可能不会发生.
Solution with CasperJS/Phantomjs server is interesting, however people pointed out that it leaks memory, which probably won't be happening if you run short-lived CasperJS scripts.
这篇关于如何使用socket.io从远程jquery客户端控制CasperJS自动化脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!