我在试着运行jxcore。但我不能to.Please帮助我,因为我是新手。
server.js
var http = require("http");
jxcore.tasks.on('message', function (threadId, param) {
console.log('Main thread received a message from subthread no ' +
threadId + '. Message: ', param);
});
http.createServer(function(req,resp) {
console.log("Listening To Thread " + process.threadId);
resp.writeHead(200,{"Content-Type":"text/html"});
resp.end("Running JXCORE "+process.threadId);
}).listen(3000);serverjx.js
var method = function() {
try {
process.keepAlive();
require("server");
console.log("Welcome To NodeJS");
return {
someResult: "some result";
};
} catch(e) {
console.log("Error Occured : "+e);
return {"Error":e};
}
}
jxcore.tasks.runOnce(code, {count:1000}, function(obj) {
process.sendToMain({started:true});
console.log("Return Value " + obj);
setTimeout(function() {
console.log("Waiting For TimeOut 5 Sec");
}, 5000);
});我正在键入cmd作为jx server.js jx mt-keep server.js
我没有看到线程在运行。请帮帮忙
发布于 2015-01-15 13:12:28
这里有几个结构性错误。另外,这并不明显,你想要做的事情。
场景1 -只运行server.js
它们都能工作:jx mt server.js或jx mt-keep server.js
场景2 -运行serverjx.js,为每个线程server.js加载
在这里,您可能尝试使用jxcore.tasks.runOnce()在每个线程上创建一个http服务器。因此,每个线程都将加载server.js并在那里创建自己的http服务器实例。
这应该以这样的方式启动:jx serverjx.js (没有mt或mt保持)
虽然我不认为这样做有什么意义(为什么不像在场景1中那样运行它,因为它是多线程http服务器的正确方法?),但经过几次修复之后,代码将如下所示:
serverjx.js
var method = function () {
try {
process.keepAlive();
require("./server");
console.log("Welcome To NodeJS");
return {
someResult: "some result"
};
} catch (e) {
console.log("Error Occured : " + e);
return {"Error": e};
}
};
jxcore.tasks.runOnce(method, {count: 1000});请注意以下几点:
require('server'),则server.js是错误的--您必须调用require('./server')jxcore.tasks.runOnce(code, ...)是错的-应该是jxcore.tasks.runOnce(method, ...)jxcore.tasks.runOnce不接收回调参数,因此我删除了这个参数。另一件事是,在回调中,您试图将一个对象发送到主线程(process.sendToMain({started:true})),但是您已经在server.js (jxcore.tasks.on('message'))中找到了侦听器,它实际上被加载到一个线程中(所以它不是主线程),并且消息无法到达那里。
https://stackoverflow.com/questions/27944545
复制相似问题