How to use the running express service?
quanganh206 opened this issue · 9 comments
I try example and it run so good.
I try to test on my mobile and call express from chrome browser with address: http://127.0.0.1:3000 everything is okay.
My question here: is there anyway to use that express service on the example app. Do I try to make a background service or can call it from another thread?
I try to test on my mobile and call express from chrome browser
I assume, that you speak about chrome browser installed on the same device, right? If that's the case, you can also call the server from any other device in the network (PC, Mac, tablet, phone). Remote access.
Calling from another thread, or even another app is also possible.
Thanks @ktrzeciaknubisa, I will try. One notification, it not working with API 23, hope it had in the next version.
Hi @ktrzeciaknubisa, seem like I success in make call in another thread. But it's a bit trick, I must wait for a moment. Is there any beautiful way to check jxcore ready like deviceready event? I try to work with isReady but there is no luck, deviceready running before jxcore ready so my ajax call produce an error. I don't think interval that call is the best way.
Am I guessing right, that you're creating an express server under the sub thread (not a main thread) and then making http request by ajax call from the client (cordova/webview) side?
Yes I try to call from cordova, jxcore running in a sub thread in second example @ktrzeciaknubisa. Do you have any help for a better way.
Because currently there is no direct way for the thread to make a call to cordova, you might try the following approach:
-
when the sub thread is ready, inform the main thread about this:
process.sendToMain({ cmd: "threadStarted" });
-
the main thread receives the message and then it makes a call to cordova:
jxcore.tasks.on("message", function(tid, params) { if (params.cmd === "threadStarted") { Mobile("threadStarted").call({ url : params.url }); } });
-
the cordova client should have
threadStarted
method registered (you should do it inside:jxcore.isReady(function () { var threadStarted = function(params) { // do something }; jxcore('threadStarted').register(threadStarted); });
-
now you may do calls from cordova to the sever running under the sub thread.
Same approach we have used in our demo wifi-shoplist-app
Thanks @ktrzeciaknubisa, I will try and feedback the result soon.
Hi @ktrzeciaknubisa, I play with your suggested code and it works well, thank you so much. Here is my short code, hope it helps for newbie with jxcore-cordova like me 😄
// run express server under a sub thread
jxcore.tasks.addTask(function() {
// requiring utilities again. This function doesn't share any
// variable from the above thread code.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.json('Hello World! (' + Date.now() + ")");
});
var server = app.listen(3000, function() {
process.sendToMain({ cmd: "threadStarted", url: '127.0.0.1' });
});
});
jxcore.tasks.on("message", function(tid, params) {
if (params.cmd === "threadStarted") {
Mobile("threadStarted").call({ url : params.url });
}
});
Thanks for the update and code-sharing :) Then I guess we can close the issue.
One quick note: I would define jxcore.tasks.on("message")
before jxcore.tasks.addTask()
. Perhaps that shouldn't matter, but who knows. There are different mobiles out there, different CPUs etc., so I would do this just for sake of being careful :)