本文整理汇总了Java中com.koushikdutta.async.AsyncServer类的典型用法代码示例。如果您正苦于以下问题:Java AsyncServer类的具体用法?Java AsyncServer怎么用?Java AsyncServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AsyncServer类属于com.koushikdutta.async包,在下文中一共展示了AsyncServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMaxAgePrivate
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void testMaxAgePrivate() throws Exception {
AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/uname/(.*)", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.getHeaders().set("Date", HttpDate.format(new Date()));
response.getHeaders().set("Cache-Control", "private, max-age=10000");
response.send(request.getMatcher().group(1));
}
});
AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
int port = socket.getLocalPort();
// clear the old cache
cache.clear();
client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
assertEquals(cache.getCacheHitCount(), 1);
assertEquals(cache.getNetworkCount(), 1);
}
finally {
AsyncServer.getDefault().stop();
client.getMiddleware().remove(cache);
}
}
示例2: testFileDataEmitter
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void testFileDataEmitter() throws Exception {
final Semaphore semaphore = new Semaphore(0);
File f = getContext().getFileStreamPath("test.txt");
StreamUtility.writeFile(f, "hello world");
FileDataEmitter fdm = new FileDataEmitter(AsyncServer.getDefault(), f);
final Md5 md5 = Md5.createInstance();
Future<String> stringBody = new StringParser().parse(fdm)
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
semaphore.release();
}
});
fdm.resume();
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
assertEquals("hello world", stringBody.get());
}
示例3: testNoDomain
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void testNoDomain() throws Exception {
AsyncServer server = new AsyncServer();
try {
final Semaphore semaphore = new Semaphore(0);
server.connectSocket("www.clockworkmod-notfound.com", 8080, new ConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, AsyncSocket socket) {
assertTrue(ex instanceof UnknownHostException);
semaphore.release();
}
});
assertTrue(semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS));
}
finally {
server.stop();
}
}
示例4: setUp
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
@Override
protected void setUp() throws Exception {
super.setUp();
httpServer = new AsyncHttpServer();
httpServer.setErrorCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
fail();
}
});
httpServer.listen(AsyncServer.getDefault(), 5000);
httpServer.websocket("/ws", new WebSocketRequestCallback() {
@Override
public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
webSocket.setStringCallback(new StringCallback() {
@Override
public void onStringAvailable(String s) {
webSocket.send(s);
}
});
}
});
}
示例5: create
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public static EndEmitter create(AsyncServer server, final Exception e) {
final EndEmitter ret = new EndEmitter();
// don't need to worry about any race conditions with post and this return value
// since we are in the server thread.
server.post(new Runnable() {
@Override
public void run() {
ret.report(e);
}
});
return ret;
}
示例6: stop
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void stop() {
if(mHttpServer != null)
mHttpServer.stop();
AsyncServer.getDefault().stop();
if(mAsyncServer != null)
mAsyncServer.stop();
if(mWifiLock != null && mWifiLock.isHeld())
mWifiLock.release();
if(mWakeLock != null && mWakeLock.isHeld())
mWakeLock.release();
}
示例7: AsyncHttpClient
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public AsyncHttpClient(AsyncServer server) {
mServer = server;
insertMiddleware(socketMiddleware = new AsyncSocketMiddleware(this));
insertMiddleware(sslSocketMiddleware = new SpdyMiddleware(this));
insertMiddleware(httpTransportMiddleware = new HttpTransportMiddleware());
sslSocketMiddleware.addEngineConfigurator(new SSLEngineSNIConfigurator());
}
示例8: testProxy
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void testProxy() throws Exception {
wasProxied = false;
final AsyncServer proxyServer = new AsyncServer();
try {
AsyncProxyServer httpServer = new AsyncProxyServer(proxyServer) {
@Override
protected boolean onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
wasProxied = true;
return super.onRequest(request, response);
}
};
AsyncServerSocket socket = httpServer.listen(proxyServer, 0);
// client.getSocketMiddleware().enableProxy("localhost", 5555);
AsyncHttpGet get = new AsyncHttpGet("http://www.clockworkmod.com");
get.enableProxy("localhost", socket.getLocalPort());
Future<String> ret = client.executeString(get, null);
String data;
assertNotNull(data = ret.get(TIMEOUT, TimeUnit.MILLISECONDS));
assertTrue(data.contains("ClockworkMod"));
assertTrue(wasProxied);
}
finally {
proxyServer.stop();
}
}
示例9: write
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
@Override
public void write(final AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
AsyncServer.getDefault().postDelayed(new Runnable() {
@Override
public void run() {
DelayedStringBody.super.write(request, sink, completed);
}
}, 1000);
}
示例10: tearDown
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
@Override
protected void tearDown() throws Exception {
super.tearDown();
httpServer.stop();
AsyncServer.getDefault().stop();
}
示例11: disabled__testClientCertificateIssue163
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void disabled__testClientCertificateIssue163() throws Exception {
// https://security.springthroughtest.com/hello.json
AsyncServer server = new AsyncServer();
try {
AsyncHttpClient client = new AsyncHttpClient(server);
JSONObject json = client.executeJSONObject(new AsyncHttpGet("https://security.springthroughtest.com/hello.json"), null).get();
}
finally {
server.stop();
}
}
示例12: go
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public void go(String ip, int port, String msg, CallBack callBack) {
mCallBack = callBack;
Looper.prepare();
handler = new Handler();
cancellable = AsyncServer.getDefault().connectSocket(ip, port, (ex, socket) -> {
if (ex != null) {
handler.removeCallbacks(runnable);
mCallBack.onConnectError(ex.getMessage());
return;
}
socket.setDataCallback((emitter, bb) -> L.iTag("TcpClient", new String(bb.getAllByteArray())));
socket.setClosedCallback(ex1 -> {
if (ex1 != null) {
L.iTag("TcpClient", "setClosedCallback出错");
return;
}
L.iTag("TcpClient", "setClosedCallback");
});
socket.setEndCallback(ex12 -> {
if (ex12 != null) {
L.iTag("TcpClient", "setEndCallback出错");
return;
}
L.iTag("TcpClient", "setEndCallback");
});
socket.setWriteableCallback(() -> Log.d("TcpClient", "onWriteable"));
byte[] dataContent = msg.getBytes();
byte[] end = new byte[]{0x0a};
byte[] start = new byte[]{0x0a};
byte[] data = new byte[dataContent.length + start.length + end.length];
try {
System.arraycopy(start, 0, data, 0, start.length);
System.arraycopy(dataContent, 0, data, start.length, dataContent.length);
System.arraycopy(end, 0, data, start.length + dataContent.length, end.length);
} catch (Exception e) {
L.logFile(e.getMessage() + "\n" + e.getStackTrace().toString());
data = dataContent;
}
L.iTag("TcpClient", data.toString());
L.i("TcpClient", data);
Util.writeAll(socket, data, ex13 -> {
if (ex13 != null) {
L.iTag("TcpClient", "writeAll出错");
handler.removeCallbacks(runnable);
mCallBack.onWriteError(ex13.getMessage());
return;
}
handler.removeCallbacks(runnable);
mCallBack.onSuccess();
L.iTag("TcpClient", "writeAll");
});
});
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 5000);
Looper.loop();
}
示例13: SocketServer
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public SocketServer(int serverPort) {
asyncServer = new AsyncServer();
asyncServer.listen(null, serverPort, listenCallback);
}
示例14: InputStreamDataEmitter
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
public InputStreamDataEmitter(AsyncServer server, InputStream inputStream) {
this.server = server;
this.inputStream = inputStream;
doResume();
}
示例15: getServer
import com.koushikdutta.async.AsyncServer; //导入依赖的package包/类
@Override
public AsyncServer getServer() {
return server;
}