Skip to content

Commit 599eb98

Browse files
committed
fix disconnection while in opening state
1 parent f7810c1 commit 599eb98

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/main/java/io/socket/client/Manager.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class Manager extends Emitter {
8484
private double _randomizationFactor;
8585
private Backoff backoff;
8686
private long _timeout;
87-
private Set<Socket> connected;
87+
private Set<Socket> connecting = new HashSet<Socket>();
8888
private URI uri;
8989
private List<Packet> packetBuffer;
9090
private Queue<On.Handle> subs;
@@ -139,7 +139,6 @@ public Manager(URI uri, Options opts) {
139139
this.timeout(opts.timeout);
140140
this.readyState = ReadyState.CLOSED;
141141
this.uri = uri;
142-
this.connected = new HashSet<Socket>();
143142
this.encoding = false;
144143
this.packetBuffer = new ArrayList<Packet>();
145144
this.encoder = new Parser.Encoder();
@@ -402,11 +401,16 @@ public Socket socket(String nsp) {
402401
} else {
403402
final Manager self = this;
404403
final Socket s = socket;
404+
socket.on(Socket.EVENT_CONNECTING, new Listener() {
405+
@Override
406+
public void call(Object... args) {
407+
self.connecting.add(s);
408+
}
409+
});
405410
socket.on(Socket.EVENT_CONNECT, new Listener() {
406411
@Override
407412
public void call(Object... objects) {
408413
s.id = self.engine.id();
409-
self.connected.add(s);
410414
}
411415
});
412416
}
@@ -415,8 +419,8 @@ public void call(Object... objects) {
415419
}
416420

417421
/*package*/ void destroy(Socket socket) {
418-
this.connected.remove(socket);
419-
if (!this.connected.isEmpty()) return;
422+
this.connecting.remove(socket);
423+
if (!this.connecting.isEmpty()) return;
420424

421425
this.close();
422426
}

src/main/java/io/socket/client/Socket.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class Socket extends Emitter {
2525
*/
2626
public static final String EVENT_CONNECT = "connect";
2727

28+
public static final String EVENT_CONNECTING = "connecting";
29+
2830
/**
2931
* Called on a disconnection.
3032
*/
@@ -60,6 +62,7 @@ public class Socket extends Emitter {
6062
put(EVENT_CONNECT, 1);
6163
put(EVENT_CONNECT_ERROR, 1);
6264
put(EVENT_CONNECT_TIMEOUT, 1);
65+
put(EVENT_CONNECTING, 1);
6366
put(EVENT_DISCONNECT, 1);
6467
put(EVENT_ERROR, 1);
6568
put(EVENT_RECONNECT, 1);
@@ -123,6 +126,7 @@ public void run() {
123126
Socket.this.subEvents();
124127
Socket.this.io.open(); // ensure open
125128
if (Manager.ReadyState.OPEN == Socket.this.io.readyState) Socket.this.onopen();
129+
Socket.this.emit(EVENT_CONNECTING);
126130
}
127131
});
128132
return this;

src/test/java/io/socket/client/ConnectionTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,33 @@ public void run() {
591591
assertThat((Boolean) values.take(), is(true));
592592
}
593593

594+
@Test(timeout = TIMEOUT)
595+
public void connectWhileDisconnectingAnotherSocket() throws URISyntaxException, InterruptedException {
596+
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
597+
598+
final Manager manager = new Manager(new URI(uri()));
599+
final Socket socket1 = manager.socket("/foo");
600+
socket1.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
601+
@Override
602+
public void call(Object... args) {
603+
final Socket socket2 = manager.socket("/asd");
604+
socket2.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
605+
@Override
606+
public void call(Object... args) {
607+
values.offer("done");
608+
socket2.disconnect();
609+
}
610+
});
611+
socket2.open();
612+
socket1.disconnect();
613+
}
614+
});
615+
616+
socket1.open();
617+
values.take();
618+
manager.close();
619+
}
620+
594621
@Test(timeout = TIMEOUT)
595622
public void tryToReconnectTwiceAndFailWithIncorrectAddress() throws URISyntaxException, InterruptedException {
596623
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();

0 commit comments

Comments
 (0)