send xhr-polling from other threads
This commit is contained in:
@@ -6,6 +6,8 @@ import com.github.nkzawa.emitter.Emitter;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class PollingXHR extends Polling {
|
public class PollingXHR extends Polling {
|
||||||
@@ -78,6 +80,8 @@ public class PollingXHR extends Polling {
|
|||||||
|
|
||||||
private static class Request extends Emitter {
|
private static class Request extends Emitter {
|
||||||
|
|
||||||
|
private static final ExecutorService xhrService = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
String method;
|
String method;
|
||||||
String uri;
|
String uri;
|
||||||
String data;
|
String data;
|
||||||
@@ -106,13 +110,18 @@ public class PollingXHR extends Polling {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.info(String.format("sending xhr with url %s | data %s", this.uri, this.data));
|
logger.info(String.format("sending xhr with url %s | data %s", this.uri, this.data));
|
||||||
|
xhrService.submit(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
BufferedWriter writer = null;
|
||||||
BufferedReader reader = null;
|
BufferedReader reader = null;
|
||||||
try {
|
try {
|
||||||
if (this.data != null) {
|
if (self.data != null) {
|
||||||
byte[] data = this.data.getBytes("UTF-8");
|
byte[] data = self.data.getBytes("UTF-8");
|
||||||
xhr.setFixedLengthStreamingMode(data.length);
|
xhr.setFixedLengthStreamingMode(data.length);
|
||||||
xhr.getOutputStream().write(data);
|
writer = new BufferedWriter(new OutputStreamWriter(xhr.getOutputStream()));
|
||||||
|
writer.write(self.data);
|
||||||
|
writer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
@@ -121,15 +130,20 @@ public class PollingXHR extends Polling {
|
|||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
data.append(line);
|
data.append(line);
|
||||||
}
|
}
|
||||||
this.onData(data.toString());
|
self.onData(data.toString());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
this.onError(e);
|
self.onError(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
try {
|
||||||
|
if (writer != null) writer.close();
|
||||||
|
} catch (IOException e) {}
|
||||||
try {
|
try {
|
||||||
if (reader != null) reader.close();
|
if (reader != null) reader.close();
|
||||||
} catch (IOException e) {}
|
} catch (IOException e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
this.emit("success");
|
this.emit("success");
|
||||||
|
|||||||
@@ -73,8 +73,8 @@ public class SocketTest {
|
|||||||
public void stopServer() throws InterruptedException {
|
public void stopServer() throws InterruptedException {
|
||||||
System.out.println("Stopping server ...");
|
System.out.println("Stopping server ...");
|
||||||
serverProcess.destroy();
|
serverProcess.destroy();
|
||||||
serverOutout.cancel(true);
|
serverOutout.cancel(false);
|
||||||
serverError.cancel(true);
|
serverError.cancel(false);
|
||||||
serverService.shutdown();
|
serverService.shutdown();
|
||||||
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
|
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user