Skip to content

Commit d642cb2

Browse files
committed
Add ActionVoid/onReconnect callback to match TypeScript SSE
1 parent 9aea349 commit d642cb2

File tree

7 files changed

+134
-8
lines changed

7 files changed

+134
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.servicestack.client.sse;
2+
3+
public interface ServerEventJoinCallback {
4+
void execute(ServerEventJoin e);
5+
}
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.servicestack.client.sse;
2+
3+
public interface ServerEventLeaveCallback {
4+
void execute(ServerEventLeave e);
5+
}
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.servicestack.client.sse;
2+
3+
public interface ServerEventUpdateCallback {
4+
void execute(ServerEventUpdate e);
5+
}

src/AndroidClient/android/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.servicestack.client.Log;
1010
import net.servicestack.client.Utils;
1111
import net.servicestack.func.Action;
12+
import net.servicestack.func.ActionVoid;
1213
import net.servicestack.func.Func;
1314
import net.servicestack.func.Function;
1415

@@ -52,8 +53,12 @@ public class ServerEventsClient implements Closeable {
5253

5354
protected ServerEventConnectCallback onConnect;
5455
protected ServerEventMessageCallback onMessage;
56+
protected ServerEventJoinCallback onJoin;
57+
protected ServerEventLeaveCallback onLeave;
58+
protected ServerEventUpdateCallback onUpdate;
5559
protected ServerEventMessageCallback onCommand;
5660
protected ServerEventMessageCallback onHeartbeat;
61+
protected ActionVoid onReconnect;
5762
protected ExceptionCallback onException;
5863
protected HttpRequestFilter heartbeatRequestFilter;
5964

@@ -156,11 +161,31 @@ public ServerEventsClient setOnMessage(ServerEventMessageCallback onMessage) {
156161
return this;
157162
}
158163

164+
public ServerEventsClient setOnJoin(ServerEventJoinCallback onJoin) {
165+
this.onJoin = onJoin;
166+
return this;
167+
}
168+
169+
public ServerEventsClient setOnLeave(ServerEventLeaveCallback onLeave) {
170+
this.onLeave = onLeave;
171+
return this;
172+
}
173+
174+
public ServerEventsClient setOnUpdate(ServerEventUpdateCallback onUpdate) {
175+
this.onUpdate = onUpdate;
176+
return this;
177+
}
178+
159179
public ServerEventsClient setOnCommand(ServerEventMessageCallback onCommand) {
160180
this.onCommand = onCommand;
161181
return this;
162182
}
163183

184+
public ServerEventsClient setOnReconnect(ActionVoid onReconnect) {
185+
this.onReconnect = onReconnect;
186+
return this;
187+
}
188+
164189
public ServerEventsClient setOnException(ExceptionCallback onException) {
165190
this.onException = onException;
166191
return this;
@@ -220,11 +245,23 @@ public void execute(ServerEventsClient client, ServerEventMessage msg) {
220245
if (!Modifier.isPublic(mi.getModifiers()) || Modifier.isStatic(mi.getModifiers()))
221246
continue;
222247
Class[] args = mi.getParameterTypes();
223-
if (args.length != 1)
248+
if (args.length > 1)
224249
continue;
225250
if ("equals".equals(mi.getName()))
226251
continue;
227252

253+
String actionName = mi.getName();
254+
if (!target.equalsIgnoreCase(actionName) && actionName.startsWith("set"))
255+
actionName = actionName.substring(3); //= "set".length()
256+
257+
if (args.length == 0){
258+
if (target.equalsIgnoreCase(actionName)) {
259+
mi.invoke(receiver, null);
260+
return;
261+
}
262+
continue;
263+
}
264+
228265
Class requestType = args[0];
229266

230267
if (target.equals(requestType.getSimpleName()) &&
@@ -236,10 +273,6 @@ public void execute(ServerEventsClient client, ServerEventMessage msg) {
236273
return;
237274
}
238275

239-
String actionName = mi.getName();
240-
if (!target.equalsIgnoreCase(actionName) && actionName.startsWith("set"))
241-
actionName = actionName.substring(3); //= "set".length()
242-
243276
if (target.equalsIgnoreCase(actionName)) {
244277
Object request = !Utils.isNullOrEmpty(msg.getJson())
245278
? JsonUtils.fromJson(msg.getJson(), requestType)
@@ -317,6 +350,9 @@ public synchronized void restart() {
317350
onExceptionReceived(e);
318351
}
319352

353+
if (onReconnect != null){
354+
onReconnect.apply();
355+
}
320356
} catch (Exception ex){
321357
Log.e("[SSE-CLIENT] Error whilst restarting: " + ex.getMessage(), ex);
322358
ex.printStackTrace();
@@ -377,6 +413,42 @@ private synchronized void internalStop() {
377413
stopBackgroundThread();
378414
}
379415

416+
private void onJoinReceived(ServerEventJoin e) {
417+
if (Log.isDebugEnabled())
418+
Log.d("[SSE-CLIENT] OnJoinReceived: ("
419+
+ e.getClass().getSimpleName() + ") #"
420+
+ e.getEventId() + " on #"
421+
+ getConnectionDisplayName() + " ("
422+
+ Utils.join(channels, ",") + ")");
423+
424+
if (onJoin != null)
425+
onJoin.execute(e);
426+
}
427+
428+
private void onLeaveReceived(ServerEventLeave e) {
429+
if (Log.isDebugEnabled())
430+
Log.d("[SSE-CLIENT] OnLeaveReceived: ("
431+
+ e.getClass().getSimpleName() + ") #"
432+
+ e.getEventId() + " on #"
433+
+ getConnectionDisplayName() + " ("
434+
+ Utils.join(channels, ",") + ")");
435+
436+
if (onLeave != null)
437+
onLeave.execute(e);
438+
}
439+
440+
private void onUpdateReceived(ServerEventUpdate e) {
441+
if (Log.isDebugEnabled())
442+
Log.d("[SSE-CLIENT] OnUpdateReceived: ("
443+
+ e.getClass().getSimpleName() + ") #"
444+
+ e.getEventId() + " on #"
445+
+ getConnectionDisplayName() + " ("
446+
+ Utils.join(channels, ",") + ")");
447+
448+
if (onUpdate != null)
449+
onUpdate.execute(e);
450+
}
451+
380452
private void onCommandReceived(ServerEventMessage e) {
381453
if (Log.isDebugEnabled())
382454
Log.d("[SSE-CLIENT] OnCommandReceived: ("
@@ -570,15 +642,24 @@ protected void processOnConnectMessage(ServerEventMessage e) {
570642
}
571643

572644
protected void processOnJoinMessage(ServerEventMessage e) {
573-
onCommandReceived(new ServerEventJoin().populate(e, JsonUtils.toJsonObject(e.getJson())));
645+
ServerEventJoin m = new ServerEventJoin();
646+
m.populate(e, JsonUtils.toJsonObject(e.getJson()));
647+
onJoinReceived(m);
648+
onCommandReceived(m);
574649
}
575650

576651
protected void processOnLeaveMessage(ServerEventMessage e) {
577-
onCommandReceived(new ServerEventLeave().populate(e, JsonUtils.toJsonObject(e.getJson())));
652+
ServerEventLeave m = new ServerEventLeave();
653+
m.populate(e, JsonUtils.toJsonObject(e.getJson()));
654+
onLeaveReceived(m);
655+
onCommandReceived(m);
578656
}
579657

580658
protected void processOnUpdateMessage(ServerEventMessage e) {
581-
onCommandReceived(new ServerEventUpdate().populate(e, JsonUtils.toJsonObject(e.getJson())));
659+
ServerEventUpdate m = new ServerEventUpdate();
660+
m.populate(e, JsonUtils.toJsonObject(e.getJson()));
661+
onUpdateReceived(m);
662+
onCommandReceived(m);
582663
}
583664

584665
protected void processOnHeartbeatMessage(ServerEventMessage e) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.servicestack.func;
2+
3+
/**
4+
* Created by mythz on 3/21/2017.
5+
*/
6+
7+
public interface ActionVoid {
8+
public void apply();
9+
}

src/AndroidClient/client/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.servicestack.client.Log;
1010
import net.servicestack.client.Utils;
1111
import net.servicestack.func.Action;
12+
import net.servicestack.func.ActionVoid;
1213
import net.servicestack.func.Func;
1314
import net.servicestack.func.Function;
1415

@@ -57,6 +58,7 @@ public class ServerEventsClient implements Closeable {
5758
protected ServerEventUpdateCallback onUpdate;
5859
protected ServerEventMessageCallback onCommand;
5960
protected ServerEventMessageCallback onHeartbeat;
61+
protected ActionVoid onReconnect;
6062
protected ExceptionCallback onException;
6163
protected HttpRequestFilter heartbeatRequestFilter;
6264

@@ -179,6 +181,11 @@ public ServerEventsClient setOnCommand(ServerEventMessageCallback onCommand) {
179181
return this;
180182
}
181183

184+
public ServerEventsClient setOnReconnect(ActionVoid onReconnect) {
185+
this.onReconnect = onReconnect;
186+
return this;
187+
}
188+
182189
public ServerEventsClient setOnException(ExceptionCallback onException) {
183190
this.onException = onException;
184191
return this;
@@ -343,6 +350,9 @@ public synchronized void restart() {
343350
onExceptionReceived(e);
344351
}
345352

353+
if (onReconnect != null){
354+
onReconnect.apply();
355+
}
346356
} catch (Exception ex){
347357
Log.e("[SSE-CLIENT] Error whilst restarting: " + ex.getMessage(), ex);
348358
ex.printStackTrace();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.servicestack.func;
2+
3+
/**
4+
* Created by mythz on 3/21/2017.
5+
*/
6+
7+
public interface ActionVoid {
8+
public void apply();
9+
}

0 commit comments

Comments
 (0)