Skip to content

Commit 39c2e08

Browse files
committed
Fixing some potential bugs for reconnecting.
1 parent f24b784 commit 39c2e08

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/io/socket/IOConnection.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private class HearbeatTimeoutTask extends TimerTask {
148148
*/
149149
@Override
150150
public void run() {
151-
setState(STATE_INVALID);
151+
cleanup();
152152
error(new SocketIOException(
153153
"Timeout Error. No heartbeat from server within life time of the socket. closing.",
154154
lastException));
@@ -246,7 +246,7 @@ static public IOConnection register(String origin, SocketIO socket) {
246246
* @return true, if successfully registered on this transport, otherwise
247247
* false.
248248
*/
249-
public boolean register(SocketIO socket) {
249+
public synchronized boolean register(SocketIO socket) {
250250
String namespace = socket.getNamespace();
251251
if (sockets.containsKey(namespace))
252252
return false;
@@ -265,7 +265,7 @@ public boolean register(SocketIO socket) {
265265
* @param socket
266266
* the socket to be shut down
267267
*/
268-
public void unregister(SocketIO socket) {
268+
public synchronized void unregister(SocketIO socket) {
269269
sendPlain("0::" + socket.getNamespace());
270270
sockets.remove(socket.getNamespace());
271271
socket.getCallback().onDisconnect();
@@ -316,7 +316,7 @@ private void handshake() {
316316
/**
317317
* Connect transport.
318318
*/
319-
private void connectTransport() {
319+
private synchronized void connectTransport() {
320320
if (getState() == STATE_INVALID)
321321
return;
322322
setState(STATE_CONNECTING);
@@ -409,7 +409,7 @@ private IOConnection(String url, SocketIO socket) {
409409
/**
410410
* Cleanup. IOConnection is not usable after this calling this.
411411
*/
412-
private void cleanup() {
412+
private synchronized void cleanup() {
413413
setState(STATE_INVALID);
414414
if (transport != null)
415415
transport.disconnect();
@@ -747,17 +747,15 @@ public void transportMessage(String text) {
747747
* forces a reconnect. This had become useful on some android devices which
748748
* do not shut down TCP-connections when switching from HSDPA to Wifi
749749
*/
750-
public void reconnect() {
751-
synchronized (this) {
752-
if (getState() != STATE_INVALID) {
753-
invalidateTransport();
754-
setState(STATE_INTERRUPTED);
755-
if (reconnectTask != null) {
756-
reconnectTask.cancel();
757-
}
758-
reconnectTask = new ReconnectTask();
759-
backgroundTimer.schedule(reconnectTask, 1000);
750+
public synchronized void reconnect() {
751+
if (getState() != STATE_INVALID) {
752+
invalidateTransport();
753+
setState(STATE_INTERRUPTED);
754+
if (reconnectTask != null) {
755+
reconnectTask.cancel();
760756
}
757+
reconnectTask = new ReconnectTask();
758+
backgroundTimer.schedule(reconnectTask, 1000);
761759
}
762760
}
763761

src/io/socket/SocketIO.java

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ else if (this.url == null)
198198
* @return true if connecting has been initiated, false if not
199199
*/
200200
private boolean setAndConnect(URL url, IOCallback callback) {
201+
if(this.connection != null)
202+
throw new RuntimeException("You can connect your SocketIO instance only once. Use a fresh instance instead.");
201203
if ((this.url != null && url != null)
202204
|| (this.callback != null && callback != null))
203205
return false;

0 commit comments

Comments
 (0)