Moving the Callback Server Start/Stop to the onPageStarted fixed timing errors

This commit is contained in:
Joe Bowser 2012-05-10 15:07:14 -07:00
parent c0dcbecbea
commit de6652dd9d
3 changed files with 23 additions and 23 deletions

View File

@ -97,7 +97,7 @@ public class CallbackServer implements Runnable {
* Constructor. * Constructor.
*/ */
public CallbackServer() { public CallbackServer() {
//System.out.println("CallbackServer()"); //Log.d(LOG_TAG, "CallbackServer()");
this.active = false; this.active = false;
this.empty = true; this.empty = true;
this.port = 0; this.port = 0;
@ -113,7 +113,7 @@ public class CallbackServer implements Runnable {
* @param url The URL of the Cordova app being loaded * @param url The URL of the Cordova app being loaded
*/ */
public void init(String url) { public void init(String url) {
//System.out.println("CallbackServer.start("+url+")"); //Log.d(LOG_TAG, "CallbackServer.start("+url+")");
this.active = false; this.active = false;
this.empty = true; this.empty = true;
this.port = 0; this.port = 0;
@ -175,7 +175,7 @@ public class CallbackServer implements Runnable {
* Start the server on a new thread. * Start the server on a new thread.
*/ */
public void startServer() { public void startServer() {
//System.out.println("CallbackServer.startServer()"); //Log.d(LOG_TAG, "CallbackServer.startServer()");
this.active = false; this.active = false;
// Start server on new thread // Start server on new thread
@ -207,18 +207,18 @@ public class CallbackServer implements Runnable {
String request; String request;
ServerSocket waitSocket = new ServerSocket(0); ServerSocket waitSocket = new ServerSocket(0);
this.port = waitSocket.getLocalPort(); this.port = waitSocket.getLocalPort();
//System.out.println("CallbackServer -- using port " +this.port); //Log.d(LOG_TAG, "CallbackServer -- using port " +this.port);
this.token = java.util.UUID.randomUUID().toString(); this.token = java.util.UUID.randomUUID().toString();
//System.out.println("CallbackServer -- using token "+this.token); //Log.d(LOG_TAG, "CallbackServer -- using token "+this.token);
while (this.active) { while (this.active) {
//System.out.println("CallbackServer: Waiting for data on socket"); //Log.d(LOG_TAG, "CallbackServer: Waiting for data on socket");
Socket connection = waitSocket.accept(); Socket connection = waitSocket.accept();
BufferedReader xhrReader = new BufferedReader(new InputStreamReader(connection.getInputStream()),40); BufferedReader xhrReader = new BufferedReader(new InputStreamReader(connection.getInputStream()),40);
DataOutputStream output = new DataOutputStream(connection.getOutputStream()); DataOutputStream output = new DataOutputStream(connection.getOutputStream());
request = xhrReader.readLine(); request = xhrReader.readLine();
String response = ""; String response = "";
//System.out.println("CallbackServerRequest="+request); //Log.d(LOG_TAG, "CallbackServerRequest="+request);
if (this.active && (request != null)) { if (this.active && (request != null)) {
if (request.contains("GET")) { if (request.contains("GET")) {
@ -227,7 +227,7 @@ public class CallbackServer implements Runnable {
// Must have security token // Must have security token
if ((requestParts.length == 3) && (requestParts[1].substring(1).equals(this.token))) { if ((requestParts.length == 3) && (requestParts[1].substring(1).equals(this.token))) {
//System.out.println("CallbackServer -- Processing GET request"); //Log.d(LOG_TAG, "CallbackServer -- Processing GET request");
// Wait until there is some data to send, or send empty data every 10 sec // Wait until there is some data to send, or send empty data every 10 sec
// to prevent XHR timeout on the client // to prevent XHR timeout on the client
@ -235,7 +235,7 @@ public class CallbackServer implements Runnable {
while (this.empty) { while (this.empty) {
try { try {
this.wait(10000); // prevent timeout from happening this.wait(10000); // prevent timeout from happening
//System.out.println("CallbackServer>>> break <<<"); //Log.d(LOG_TAG, "CallbackServer>>> break <<<");
break; break;
} }
catch (Exception e) { } catch (Exception e) { }
@ -247,11 +247,11 @@ public class CallbackServer implements Runnable {
// If no data, then send 404 back to client before it times out // If no data, then send 404 back to client before it times out
if (this.empty) { if (this.empty) {
//System.out.println("CallbackServer -- sending data 0"); //Log.d(LOG_TAG, "CallbackServer -- sending data 0");
response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space
} }
else { else {
//System.out.println("CallbackServer -- sending item"); //Log.d(LOG_TAG, "CallbackServer -- sending item");
response = "HTTP/1.1 200 OK\r\n\r\n"; response = "HTTP/1.1 200 OK\r\n\r\n";
String js = this.getJavascript(); String js = this.getJavascript();
if (js != null) { if (js != null) {
@ -270,8 +270,8 @@ public class CallbackServer implements Runnable {
else { else {
response = "HTTP/1.1 400 Bad Request\r\n\r\n "; response = "HTTP/1.1 400 Bad Request\r\n\r\n ";
} }
//System.out.println("CallbackServer: response="+response); //Log.d(LOG_TAG, "CallbackServer: response="+response);
//System.out.println("CallbackServer: closing output"); //Log.d(LOG_TAG, "CallbackServer: closing output");
output.writeBytes(response); output.writeBytes(response);
output.flush(); output.flush();
} }
@ -282,7 +282,7 @@ public class CallbackServer implements Runnable {
e.printStackTrace(); e.printStackTrace();
} }
this.active = false; this.active = false;
//System.out.println("CallbackServer.startServer() - EXIT"); //Log.d(LOG_TAG, "CallbackServer.startServer() - EXIT");
} }
/** /**
@ -290,7 +290,7 @@ public class CallbackServer implements Runnable {
* This stops the thread that the server is running on. * This stops the thread that the server is running on.
*/ */
public void stopServer() { public void stopServer() {
//System.out.println("CallbackServer.stopServer()"); //Log.d(LOG_TAG, "CallbackServer.stopServer()");
if (this.active) { if (this.active) {
this.active = false; this.active = false;

View File

@ -321,14 +321,6 @@ public class CordovaWebView extends WebView {
this.baseUrl = this.url + "/"; this.baseUrl = this.url + "/";
} }
// Create callback server and plugin manager
if (callbackServer == null) {
callbackServer = new CallbackServer();
callbackServer.init(url);
}
else {
callbackServer.reinit(url);
}
pluginManager.init(); pluginManager.init();
if(!useBrowserHistory) if(!useBrowserHistory)

View File

@ -209,6 +209,14 @@ public class CordovaWebViewClient extends WebViewClient {
view.clearHistory(); view.clearHistory();
this.doClearHistory = true; this.doClearHistory = true;
} }
// Create callback server and plugin manager
if (appView.callbackServer == null) {
appView.callbackServer = new CallbackServer();
appView.callbackServer.init(url);
}
else {
appView.callbackServer.reinit(url);
}
} }
/** /**