@@ -24,12 +24,15 @@ public class SocketIO {
2424 /** connection of this Socket. */
2525 private IOConnection connection ;
2626
27- /** Has {@link #go(IOCallback)} already been called. */
28- boolean goCalled = false ;
29-
3027 /** namespace. */
3128 private String namespace ;
3229
30+ private URL url ;
31+
32+ public SocketIO () {
33+
34+ }
35+
3336 /**
3437 * Instantiates a new socket.io object. The object connects after calling
3538 * {@link #go(IOCallback)}
@@ -40,13 +43,12 @@ public class SocketIO {
4043 * the malformed url exception
4144 */
4245 public SocketIO (final String url ) throws MalformedURLException {
43- this ( new URL ( url ) );
46+ connect ( url , null );
4447 }
4548
4649 /**
4750 * Instantiates a new socket.io object and connects to the given url.
48- * calling {@link #go(IOCallback)} afterwards results in a
49- * {@link RuntimeException}
51+ * Do not call any of the {@link #connect(URL, IOCallback)} methods afterwards.
5052 *
5153 * @param url
5254 * the url
@@ -57,8 +59,14 @@ public SocketIO(final String url) throws MalformedURLException {
5759 */
5860 public SocketIO (final String url , final IOCallback callback )
5961 throws MalformedURLException {
60- this (url );
61- this .go (callback );
62+ connect (url , callback );
63+ }
64+
65+ /**
66+ * Connects to a given host with a given callback.
67+ */
68+ public void connect (final String url , final IOCallback callback ) throws MalformedURLException {
69+ connect (new URL (url ), callback );
6270 }
6371
6472 /**
@@ -69,14 +77,9 @@ public SocketIO(final String url, final IOCallback callback)
6977 * the url
7078 */
7179 public SocketIO (final URL url ) {
72- final String origin = url .getProtocol () + "://" + url .getAuthority ();
73- this .namespace = url .getPath ();
74- if (this .namespace .equals ("/" )) {
75- this .namespace = "" ;
76- }
77- this .connection = IOConnection .create (origin );
80+ connect (url , null );
7881 }
79-
82+
8083 /**
8184 * Instantiates a new socket.io object and connects to the given url.
8285 * calling {@link #go(IOCallback)} afterwards results in a
@@ -88,8 +91,29 @@ public SocketIO(final URL url) {
8891 * the callback
8992 */
9093 public SocketIO (final URL url , final IOCallback callback ) {
91- this (url );
92- this .go (callback );
94+ connect (url , callback );
95+ }
96+
97+ public void connect (URL url , IOCallback callback ) {
98+ if (url != null ) {
99+ this .url = url ;
100+ final String origin = url .getProtocol () + "://" + url .getAuthority ();
101+ this .namespace = url .getPath ();
102+ if (this .namespace .equals ("/" )) {
103+ this .namespace = "" ;
104+ }
105+ this .connection = IOConnection .create (origin );
106+ }
107+ if (callback != null ) {
108+ this .callback = callback ;
109+ }
110+ if (this .callback != null && this .url != null ) {
111+ this .connection .connect (this );
112+ }
113+ }
114+
115+ public void connect (IOCallback callback ) {
116+ connect ((URL )null , callback );
93117 }
94118
95119 /**
@@ -124,30 +148,6 @@ public String getNamespace() {
124148 return this .namespace ;
125149 }
126150
127- /**
128- * If you're using {@link #SocketIO(String)} or {@link #SocketIO(URL)}, this
129- * call will start connecting to the server. Make sure, you call this
130- * function only once. A second call on an object will cause a
131- * {@link RuntimeException}.
132- *
133- * {@link #SocketIO(String, IOCallback)} an
134- * {@link #SocketIO(URL, IOCallback)} will call this function in the
135- * constructor. Don't use this function if your using one of these
136- * constructors
137- *
138- * @param callback
139- * the callback
140- */
141- public void go (final IOCallback callback ) {
142- if (this .goCalled ) {
143- throw new RuntimeException (
144- "go() may only be called when using SocketIO constructor with one argument." );
145- }
146- this .callback = callback ;
147- this .connection .connect (this );
148- this .goCalled = true ;
149- }
150-
151151 /**
152152 * Send JSON data to the Socket.io server.
153153 *
0 commit comments