1313import java .io .IOException ;
1414import java .io .InputStream ;
1515import java .net .MalformedURLException ;
16- import java .net .SocketTimeoutException ;
1716import java .net .URL ;
1817import java .net .URLConnection ;
1918import java .util .Arrays ;
@@ -48,6 +47,7 @@ public class IOConnection {
4847 private Timer reconnectTimeoutTimer ;
4948 private String urlStr ;
5049 private Exception lastException ;
50+ private boolean keepAliveInQueue ;
5151
5252 private final class ReconnectTimeoutTask extends TimerTask {
5353 @ Override
@@ -66,8 +66,10 @@ private final class ReconnectTask extends TimerTask {
6666 @ Override
6767 public void run () {
6868 connect ();
69- // TODO: Make sure this is called only once.
70- sendPlain ("2::" );
69+ if (!keepAliveInQueue ) {
70+ sendPlain ("2::" );
71+ keepAliveInQueue = true ;
72+ }
7173 }
7274 }
7375
@@ -94,26 +96,20 @@ public void run() {
9496
9597 private void handshake () throws IOException {
9698 URL url = new URL (IOConnection .this .url .toString () + SOCKET_IO_1 );
97-
99+ String response ;
98100 URLConnection connection = url .openConnection ();
99101 connection .setConnectTimeout (connectTimeout );
100102 connection .setReadTimeout (connectTimeout );
101103
102- String response ;
103- try {
104- InputStream stream = connection .getInputStream ();
105- Scanner in = new Scanner (stream );
106- response = in .nextLine ();
107- if (response .contains (":" )) {
108- String [] data = response .split (":" );
109- sessionId = data [0 ];
110- heartbeatTimeout = Long .parseLong (data [1 ]) * 1000 ;
111- closingTimout = Long .parseLong (data [2 ]) * 1000 ;
112- protocols = Arrays .asList (data [3 ].split ("," ));
113- }
114- } catch (SocketTimeoutException ex ) {
115- error (new SocketIOException (ex ));
116- return ;
104+ InputStream stream = connection .getInputStream ();
105+ Scanner in = new Scanner (stream );
106+ response = in .nextLine ();
107+ if (response .contains (":" )) {
108+ String [] data = response .split (":" );
109+ heartbeatTimeout = Long .parseLong (data [1 ]) * 1000 ;
110+ closingTimout = Long .parseLong (data [2 ]) * 1000 ;
111+ protocols = Arrays .asList (data [3 ].split ("," ));
112+ sessionId = data [0 ];
117113 }
118114 }
119115
@@ -246,6 +242,7 @@ public void transportConnected() {
246242 } catch (IOException e ) {
247243 this .outputBuffer = outputBuffer ;
248244 }
245+ this .keepAliveInQueue = false ;
249246 for (String text : outputBuffer ) {
250247 sendPlain (text );
251248 }
@@ -318,12 +315,10 @@ public void transportMessage(String text) {
318315 for (SocketIO socket : sockets .values ()) {
319316 socket .getCallback ().onError (
320317 new SocketIOException (message .getData ()));
321-
322318 }
323319 else
324320 findCallback (message ).onError (
325321 new SocketIOException (message .getData ()));
326-
327322 if (message .getData ().endsWith ("+0" )) {
328323 // We are adviced to disconnect
329324 cleanup ();
0 commit comments