如何从 MeteorJS 应用程序 运行 在 meteor 服务器上远程执行外部命令?
How to run remote external commands on meteor server from MeteorJS Application?
我创建了一些登录 Duolingo 的 CasperJS 脚本,点击一个模块并打开,就像我在那里玩一样。
我创建了一个简单的 meteorJS 应用程序,我希望当我单击一个按钮时能够执行该 casperjs 脚本。我正在寻找有这种经验的人来帮助我或以正确的方式指导我,因为我不太清楚我可以用什么来实现这个小小的个人游戏。
我读过 RPC - MeteorJS 的远程过程调用,我读过 PHP 和 NodeJS 你可以 运行 一个执行脚本的函数,就像我输入命令一样运行 脚本。
我找到了这些资源:
ShellJS:https://github.com/shelljs/shelljs
和 NodeJS 子进程:https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback.
但我没有太多经验,我这样做是为了更多地了解 CasperJS、MeteorJS。
我需要的是能够 运行 这个命令 -> "casperjs duolingo.js --engine=slimerjs --disk-cache=no" 使用我的 Meteorjs 应用程序,这样我就可以继续创建我的小自动化机器人来玩 Duolingo 的全部内容。
非常感谢您的帮助。
如果你知道该怎么做,这就是一个“简单”:-)
只是想知道会发生什么:
1.) 您在服务器端创建一个方法,它可以 运行 外部进程
2.) 你创建一个可以被客户端调用的流星远程方法
3.) 您在客户端创建动作并调用远程流星方法
4.) 你绑定点击事件来调用客户端的动作
调用外部进程的方法
process_exec_sync = function (command) {
// Load future from fibers
var Future = Npm.require("fibers/future");
// Load exec
var child = Npm.require("child_process");
// Create new future
var future = new Future();
// Run command synchronous
child.exec(command, function(error, stdout, stderr) {
// return an onbject to identify error and success
var result = {};
// test for error
if (error) {
result.error = error;
}
// return stdout
result.stdout = stdout;
future.return(result);
});
// wait for future
return future.wait();
}
Meteor 远程服务器方法
// define server methods so that the clients will have access to server components
Meteor.methods({
runCasperJS: function() {
// This method call won't return immediately, it will wait for the
// asynchronous code to finish, so we call unblock to allow this client
// to queue other method calls (see Meteor docs)
this.unblock();
// run synchonous system command
var result = process_exec_sync('casperjs duolingo.js --engine=slimerjs --disk-cache=no');
// check for error
if (result.error) {
throw new Meteor.Error("exec-fail", "Error running CasperJS: " + result.error.message);
}
// success
return true;
}
})
客户端事件和远程方法调用
Template.mytemplate.events({
'click #run-casper': function(e) {
// try to run remote system call
Meteor.call('runCasperJS', function(err, res) {
// check result
if (err) {
// Do some error notification
} else {
// Do some success action
}
});
}
});
继续
您需要将服务器端方法放入目录“yourproject/server”中的文件中(例如)main.js,并将客户端部分放入带有您希望按下的按钮的模板中(将 mytemplate 重命名为你定义的那个)。
希望你得到你需要的东西。
干杯
汤姆
我创建了一些登录 Duolingo 的 CasperJS 脚本,点击一个模块并打开,就像我在那里玩一样。
我创建了一个简单的 meteorJS 应用程序,我希望当我单击一个按钮时能够执行该 casperjs 脚本。我正在寻找有这种经验的人来帮助我或以正确的方式指导我,因为我不太清楚我可以用什么来实现这个小小的个人游戏。
我读过 RPC - MeteorJS 的远程过程调用,我读过 PHP 和 NodeJS 你可以 运行 一个执行脚本的函数,就像我输入命令一样运行 脚本。 我找到了这些资源: ShellJS:https://github.com/shelljs/shelljs 和 NodeJS 子进程:https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback.
但我没有太多经验,我这样做是为了更多地了解 CasperJS、MeteorJS。
我需要的是能够 运行 这个命令 -> "casperjs duolingo.js --engine=slimerjs --disk-cache=no" 使用我的 Meteorjs 应用程序,这样我就可以继续创建我的小自动化机器人来玩 Duolingo 的全部内容。
非常感谢您的帮助。
如果你知道该怎么做,这就是一个“简单”:-)
只是想知道会发生什么:
1.) 您在服务器端创建一个方法,它可以 运行 外部进程
2.) 你创建一个可以被客户端调用的流星远程方法
3.) 您在客户端创建动作并调用远程流星方法
4.) 你绑定点击事件来调用客户端的动作
调用外部进程的方法
process_exec_sync = function (command) {
// Load future from fibers
var Future = Npm.require("fibers/future");
// Load exec
var child = Npm.require("child_process");
// Create new future
var future = new Future();
// Run command synchronous
child.exec(command, function(error, stdout, stderr) {
// return an onbject to identify error and success
var result = {};
// test for error
if (error) {
result.error = error;
}
// return stdout
result.stdout = stdout;
future.return(result);
});
// wait for future
return future.wait();
}
Meteor 远程服务器方法
// define server methods so that the clients will have access to server components
Meteor.methods({
runCasperJS: function() {
// This method call won't return immediately, it will wait for the
// asynchronous code to finish, so we call unblock to allow this client
// to queue other method calls (see Meteor docs)
this.unblock();
// run synchonous system command
var result = process_exec_sync('casperjs duolingo.js --engine=slimerjs --disk-cache=no');
// check for error
if (result.error) {
throw new Meteor.Error("exec-fail", "Error running CasperJS: " + result.error.message);
}
// success
return true;
}
})
客户端事件和远程方法调用
Template.mytemplate.events({
'click #run-casper': function(e) {
// try to run remote system call
Meteor.call('runCasperJS', function(err, res) {
// check result
if (err) {
// Do some error notification
} else {
// Do some success action
}
});
}
});
继续
您需要将服务器端方法放入目录“yourproject/server”中的文件中(例如)main.js,并将客户端部分放入带有您希望按下的按钮的模板中(将 mytemplate 重命名为你定义的那个)。
希望你得到你需要的东西。
干杯 汤姆