fix: ensure buffered events are sent in order
Before this commit, an event sent in the "connect" handler could be sent before the events that were buffered while disconnected. Related: https://github.com/socketio/socket.io-client/issues/1458
This commit is contained in:
@@ -367,8 +367,8 @@ public class Socket extends Emitter {
|
||||
private void onconnect(String id) {
|
||||
this.connected = true;
|
||||
this.id = id;
|
||||
super.emit(EVENT_CONNECT);
|
||||
this.emitBuffered();
|
||||
super.emit(EVENT_CONNECT);
|
||||
}
|
||||
|
||||
private void emitBuffered() {
|
||||
|
||||
@@ -257,4 +257,34 @@ public class SocketTest extends Connection {
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldEmitEventsInOrder() throws InterruptedException {
|
||||
final BlockingQueue<String> values = new LinkedBlockingQueue<>();
|
||||
|
||||
socket = client();
|
||||
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.emit("ack", "second", new Ack() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer((String) args[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.emit("ack", "first", new Ack() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer((String) args[0]);
|
||||
}
|
||||
});
|
||||
|
||||
socket.connect();
|
||||
assertThat(values.take(), is("first"));
|
||||
assertThat(values.take(), is("second"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user