@@ -84,19 +84,31 @@ private void subEvents() {
8484
8585 final Manager io = Socket .this .io ;
8686 Socket .this .subs = new LinkedList <On .Handle >() {{
87- add (On .on (io , Manager .EVENT_OPEN , args -> {
88- Socket .this .onopen ();
87+ add (On .on (io , Manager .EVENT_OPEN , new Listener () {
88+ @ Override
89+ public void call (Object ... args ) {
90+ Socket .this .onopen ();
91+ }
8992 }));
90- add (On .on (io , Manager .EVENT_PACKET , args -> {
91- Socket .this .onpacket ((Packet <?>) args [0 ]);
93+ add (On .on (io , Manager .EVENT_PACKET , new Listener () {
94+ @ Override
95+ public void call (Object ... args ) {
96+ Socket .this .onpacket ((Packet <?>) args [0 ]);
97+ }
9298 }));
93- add (On .on (io , Manager .EVENT_ERROR , args -> {
94- if (!Socket .this .connected ) {
95- Socket .super .emit (EVENT_CONNECT_ERROR , args [0 ]);
99+ add (On .on (io , Manager .EVENT_ERROR , new Listener () {
100+ @ Override
101+ public void call (Object ... args ) {
102+ if (!Socket .this .connected ) {
103+ Socket .super .emit (EVENT_CONNECT_ERROR , args [0 ]);
104+ }
96105 }
97106 }));
98- add (On .on (io , Manager .EVENT_CLOSE , args -> {
99- Socket .this .onclose (args .length > 0 ? (String ) args [0 ] : null );
107+ add (On .on (io , Manager .EVENT_CLOSE , new Listener () {
108+ @ Override
109+ public void call (Object ... args ) {
110+ Socket .this .onclose (args .length > 0 ? (String ) args [0 ] : null );
111+ }
100112 }));
101113 }};
102114 }
@@ -109,12 +121,15 @@ public boolean isActive() {
109121 * Connects the socket.
110122 */
111123 public Socket open () {
112- EventThread .exec (() -> {
113- if (Socket .this .connected || Socket .this .io .isReconnecting ()) return ;
124+ EventThread .exec (new Runnable () {
125+ @ Override
126+ public void run () {
127+ if (Socket .this .connected || Socket .this .io .isReconnecting ()) return ;
114128
115- Socket .this .subEvents ();
116- Socket .this .io .open (); // ensure open
117- if (Manager .ReadyState .OPEN == Socket .this .io .readyState ) Socket .this .onopen ();
129+ Socket .this .subEvents ();
130+ Socket .this .io .open (); // ensure open
131+ if (Manager .ReadyState .OPEN == Socket .this .io .readyState ) Socket .this .onopen ();
132+ }
118133 });
119134 return this ;
120135 }
@@ -133,7 +148,12 @@ public Socket connect() {
133148 * @return a reference to this object.
134149 */
135150 public Socket send (final Object ... args ) {
136- EventThread .exec (() -> Socket .this .emit (EVENT_MESSAGE , args ));
151+ EventThread .exec (new Runnable () {
152+ @ Override
153+ public void run () {
154+ Socket .this .emit (EVENT_MESSAGE , args );
155+ }
156+ });
137157 return this ;
138158 }
139159
@@ -150,23 +170,26 @@ public Emitter emit(final String event, final Object... args) {
150170 throw new RuntimeException ("'" + event + "' is a reserved event name" );
151171 }
152172
153- EventThread .exec (() -> {
154- Ack ack ;
155- Object [] _args ;
156- int lastIndex = args .length - 1 ;
157-
158- if (args .length > 0 && args [lastIndex ] instanceof Ack ) {
159- _args = new Object [lastIndex ];
160- for (int i = 0 ; i < lastIndex ; i ++) {
161- _args [i ] = args [i ];
173+ EventThread .exec (new Runnable () {
174+ @ Override
175+ public void run () {
176+ Ack ack ;
177+ Object [] _args ;
178+ int lastIndex = args .length - 1 ;
179+
180+ if (args .length > 0 && args [lastIndex ] instanceof Ack ) {
181+ _args = new Object [lastIndex ];
182+ for (int i = 0 ; i < lastIndex ; i ++) {
183+ _args [i ] = args [i ];
184+ }
185+ ack = (Ack ) args [lastIndex ];
186+ } else {
187+ _args = args ;
188+ ack = null ;
162189 }
163- ack = (Ack ) args [lastIndex ];
164- } else {
165- _args = args ;
166- ack = null ;
167- }
168190
169- emit (event , _args , ack );
191+ Socket .this .emit (event , _args , ack );
192+ }
170193 });
171194 return this ;
172195 }
@@ -180,52 +203,55 @@ public Emitter emit(final String event, final Object... args) {
180203 * @return a reference to this object.
181204 */
182205 public Emitter emit (final String event , final Object [] args , final Ack ack ) {
183- EventThread .exec (() -> {
184- JSONArray jsonArgs = new JSONArray ();
185- jsonArgs .put (event );
206+ EventThread .exec (new Runnable () {
207+ @ Override
208+ public void run () {
209+ JSONArray jsonArgs = new JSONArray ();
210+ jsonArgs .put (event );
186211
187- if (args != null ) {
188- for (Object arg : args ) {
189- jsonArgs .put (arg );
212+ if (args != null ) {
213+ for (Object arg : args ) {
214+ jsonArgs .put (arg );
215+ }
190216 }
191- }
192217
193- Packet <JSONArray > packet = new Packet <>(Parser .EVENT , jsonArgs );
218+ Packet <JSONArray > packet = new Packet <>(Parser .EVENT , jsonArgs );
194219
195- if (ack != null ) {
196- final int ackId = Socket .this .ids ;
220+ if (ack != null ) {
221+ final int ackId = Socket .this .ids ;
197222
198- logger .fine (String .format ("emitting packet with ack id %d" , ackId ));
223+ logger .fine (String .format ("emitting packet with ack id %d" , ackId ));
199224
200- if (ack instanceof AckWithTimeout ) {
201- final AckWithTimeout ackWithTimeout = (AckWithTimeout ) ack ;
202- ackWithTimeout .schedule (new TimerTask () {
203- @ Override
204- public void run () {
205- // remove the ack from the map (to prevent an actual acknowledgement)
206- acks .remove (ackId );
225+ if (ack instanceof AckWithTimeout ) {
226+ final AckWithTimeout ackWithTimeout = (AckWithTimeout ) ack ;
227+ ackWithTimeout .schedule (new TimerTask () {
228+ @ Override
229+ public void run () {
230+ // remove the ack from the map (to prevent an actual acknowledgement)
231+ acks .remove (ackId );
207232
208- // remove the packet from the buffer (if applicable)
209- Iterator <Packet <JSONArray >> iterator = sendBuffer .iterator ();
210- while (iterator .hasNext ()) {
211- if (iterator .next ().id == ackId ) {
212- iterator .remove ();
233+ // remove the packet from the buffer (if applicable)
234+ Iterator <Packet <JSONArray >> iterator = sendBuffer .iterator ();
235+ while (iterator .hasNext ()) {
236+ if (iterator .next ().id == ackId ) {
237+ iterator .remove ();
238+ }
213239 }
240+
241+ ackWithTimeout .onTimeout ();
214242 }
243+ });
244+ }
215245
216- ackWithTimeout .onTimeout ();
217- }
218- });
246+ Socket .this .acks .put (ackId , ack );
247+ packet .id = ids ++;
219248 }
220249
221- Socket .this .acks .put (ackId , ack );
222- packet .id = ids ++;
223- }
224-
225- if (Socket .this .connected ) {
226- Socket .this .packet (packet );
227- } else {
228- Socket .this .sendBuffer .add (packet );
250+ if (Socket .this .connected ) {
251+ Socket .this .packet (packet );
252+ } else {
253+ Socket .this .sendBuffer .add (packet );
254+ }
229255 }
230256 });
231257 return this ;
@@ -376,23 +402,31 @@ private void onevent(Packet<JSONArray> packet) {
376402
377403 private Ack ack (final int id ) {
378404 final Socket self = this ;
379- final boolean [] sent = new boolean []{false };
380- return args -> EventThread .exec (() -> {
381- if (sent [0 ]) return ;
382- sent [0 ] = true ;
383- if (logger .isLoggable (Level .FINE )) {
384- logger .fine (String .format ("sending ack %s" , args .length != 0 ? args : null ));
385- }
405+ final boolean [] sent = new boolean [] {false };
406+ return new Ack () {
407+ @ Override
408+ public void call (final Object ... args ) {
409+ EventThread .exec (new Runnable () {
410+ @ Override
411+ public void run () {
412+ if (sent [0 ]) return ;
413+ sent [0 ] = true ;
414+ if (logger .isLoggable (Level .FINE )) {
415+ logger .fine (String .format ("sending ack %s" , args .length != 0 ? args : null ));
416+ }
386417
387- JSONArray jsonArgs = new JSONArray ();
388- for (Object arg : args ) {
389- jsonArgs .put (arg );
390- }
418+ JSONArray jsonArgs = new JSONArray ();
419+ for (Object arg : args ) {
420+ jsonArgs .put (arg );
421+ }
391422
392- Packet <JSONArray > packet = new Packet <>(Parser .ACK , jsonArgs );
393- packet .id = id ;
394- self .packet (packet );
395- });
423+ Packet <JSONArray > packet = new Packet <>(Parser .ACK , jsonArgs );
424+ packet .id = id ;
425+ self .packet (packet );
426+ }
427+ });
428+ }
429+ };
396430 }
397431
398432 private void onack (Packet <JSONArray > packet ) {
0 commit comments