99import net .servicestack .client .Log ;
1010import net .servicestack .client .Utils ;
1111import net .servicestack .func .Action ;
12+ import net .servicestack .func .ActionVoid ;
1213import net .servicestack .func .Func ;
1314import 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 ) {
0 commit comments