From JavaScript Integration introduction, I read that
JS APIs that communicate between the browser and renderer processes should be designed using asynchronous callbacks
I started playing with the source code for the Detailed Test Application, and I noticed in MessageRouterHandler and MessageRouterHandlerEx classes that a binding is identified by doing
- Code: Select all
request.indexOf("BindingTest:") == 0
- Code: Select all
request.startsWith({"hasExtension")
1. So my first question is, is there a better way to identify those bindings without looking into the request each time?
Also, the onQuery method call is something like this:
- Code: Select all
public boolean onQuery(CefBrowser browser,
long query_id,
String request,
boolean persistent,
CefQueryCallback callback)
2. How should the query_id be used and what sort of purpose does it have?
On the client side (on the html page), the code is as follows:
window.cefQuery({
request: 'BindingTest:' + document.getElementById("message").value,
onSuccess: function(response) {
document.getElementById('result').value = 'Response: '+response;
},
onFailure: function(error_code, error_message) {}
});
3. Is there a way to send some sort of information or bindingID, so that it always points to it's own MessageRouterHandler?
4. What other data can we send in window.cefQuery, besides request, onSuccess and onFailure?
and last one,
The client web app wants to send some data and then receive a response back from my back end server. Unfortunately the backend server is not a web service and doesn't understand AJAX. So I wrote a JS API that will use CEF to send data to my backend server and get a response. the code is somewhat like this:
On the client web app JS:
- Code: Select all
var myCustomSender = new MyCustomSender();
myCustomSender.send(messageID, message, mySuccessCallBack, myErrorCallBack);
// define mySuccessCallBack
// define myErrorCallBack
On my JavaScript API:
- Code: Select all
MyCustomSender.prototype.send = function(options) {
window.cefQuery({
request: options.messageID + "," + options.message,
onSuccess: function(response) {
options.mySuccessCallBack(response);
},
onError: function(errorCode, errorMessage) {
options.myErrorCallBack(errorCode, errorMessage);
}
});
}
On my own custom MessageRouterHandler:
- Code: Select all
public boolean onQuery(CefBrowser browser,
long query_id,
String request,
boolean persistent,
CefQueryCallback callback) {
if (request.indexOf("BindingID") == 0) { [b]// This part is related to question 3[/b]
// Send data to my backend Java Server and get a response
callback.success(response);
return true;
}
// Not handled.
return false;
}
5. So the question is, is this the right approach to send data to a backend server using CEF?
My questions can seem a little confusing, and if it is, please let me know, and I can try more to explain them better.
Thanks!