Skip to content

Commit 88077d8

Browse files
committed
Update AndroidServerEventsClient to implement interruptible read using OkHttp
1 parent f68b785 commit 88077d8

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServerEventsClient.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@
55
import net.servicestack.client.AsyncResultVoid;
66
import net.servicestack.client.AsyncSuccess;
77
import net.servicestack.client.AsyncSuccessVoid;
8+
import net.servicestack.client.Log;
9+
import net.servicestack.client.sse.EventStream;
810
import net.servicestack.client.sse.GetEventSubscribers;
911
import net.servicestack.client.sse.ServerEventUser;
1012
import net.servicestack.client.sse.ServerEventsClient;
1113
import net.servicestack.client.sse.UpdateEventSubscriber;
1214
import net.servicestack.client.sse.UpdateEventSubscriberResponse;
1315
import net.servicestack.func.Func;
1416

17+
import java.io.BufferedInputStream;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.InterruptedIOException;
21+
import java.lang.reflect.Field;
22+
import java.net.HttpURLConnection;
23+
import java.net.SocketException;
24+
import java.net.URL;
1525
import java.util.ArrayList;
1626
import java.util.HashMap;
1727
import java.util.List;
1828

29+
import okhttp3.Call;
30+
import okhttp3.Callback;
31+
import okhttp3.OkHttpClient;
32+
import okhttp3.Request;
33+
import okhttp3.Response;
34+
import okhttp3.ResponseBody;
35+
import okio.BufferedSource;
36+
1937
/**
2038
* Created by mythz on 2/15/2017.
2139
*/
@@ -38,6 +56,73 @@ public AndroidServiceClient getAndroidClient(){
3856
return (AndroidServiceClient) this.serviceClient;
3957
}
4058

59+
@Override
60+
protected EventStream createEventStream() {
61+
return new AndroidEventStream(this);
62+
}
63+
64+
public static final OkHttpClient client = new OkHttpClient();
65+
66+
class AndroidEventStream extends EventStream
67+
{
68+
public AndroidEventStream(ServerEventsClient client) {
69+
super(client);
70+
}
71+
72+
Call call;
73+
74+
@Override
75+
protected InputStream getInputStream(URL streamUri) throws IOException {
76+
77+
Request request = new Request.Builder()
78+
.url(streamUri)
79+
.build();
80+
81+
call = client.newCall(request);
82+
Response response = call.execute();
83+
84+
return new BufferedInputStream(response.body().byteStream());
85+
}
86+
87+
@Override
88+
public void close() {
89+
if (call == null) return;
90+
call.cancel();
91+
}
92+
93+
@Override
94+
protected int readFromStream(InputStream inputStream, byte[] buffer) throws IOException, InterruptedException {
95+
int len;
96+
97+
//Simulates a bgThread.interrupt() so EventStream.run() exits gracefully
98+
99+
try {
100+
len = inputStream.read(buffer);
101+
} catch (SocketException e){
102+
if (call.isCanceled()){
103+
throw new InterruptedException();
104+
}
105+
throw e;
106+
}
107+
if (call.isCanceled()){
108+
throw new InterruptedException();
109+
}
110+
return len;
111+
}
112+
}
113+
114+
@Override
115+
protected synchronized void interruptBackgroundThread() {
116+
if (bgThread != null){
117+
bgEventStream.close();
118+
try {
119+
bgThread.join(1000);
120+
} catch (InterruptedException ignore) {}
121+
bgThread = null;
122+
bgEventStream = null;
123+
}
124+
}
125+
41126
public void getChannelSubscribersAsync(final AsyncResult<List<ServerEventUser>> asyncResult){
42127
this.getAndroidClient().getAsync(
43128
new GetEventSubscribers().setChannels(Func.toList(this.getChannels())),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy