compatible with 0.6.1

This commit is contained in:
Naoyuki Kanezawa
2013-06-07 05:44:00 +09:00
parent 02ca2208a1
commit f558407a82
3 changed files with 54 additions and 8 deletions

View File

@@ -378,7 +378,7 @@ public abstract class Socket extends Emitter {
if (Packet.OPEN.equals(packet.type)) {
this.onHandshake(gson.fromJson(packet.data, HandshakeData.class));
} else if (Packet.PONG.equals(packet.type)) {
this.ping();
this.setPing();
} else if (Packet.ERROR.equals(packet.type)) {
// TODO: handle error
EngineIOException err = new EngineIOException("server error");
@@ -402,7 +402,7 @@ public abstract class Socket extends Emitter {
this.pingInterval = data.pingInterval;
this.pingTimeout = data.pingTimeout;
this.onOpen();
this.ping();
this.setPing();
this.off(EVENT_HEARTBEAT, this.onHeartbeatAsListener);
this.on(EVENT_HEARTBEAT, this.onHeartbeatAsListener);
@@ -439,7 +439,7 @@ public abstract class Socket extends Emitter {
}, timeout, TimeUnit.MILLISECONDS);
}
private void ping() {
private void setPing() {
if (this.pingIntervalTimer != null) {
pingIntervalTimer.cancel(true);
}
@@ -452,7 +452,7 @@ public abstract class Socket extends Emitter {
@Override
public void run() {
logger.fine(String.format("writing ping packet - expecting pong within %sms", self.pingTimeout));
self.sendPacket(Packet.PING);
self.ping();
self.onHeartbeat(self.pingTimeout);
}
});
@@ -460,6 +460,18 @@ public abstract class Socket extends Emitter {
}, this.pingInterval, TimeUnit.MILLISECONDS);
}
/**
* Sends a ping packet
*/
public void ping() {
EventThread.exec(new Runnable() {
@Override
public void run() {
Socket.this.sendPacket(Packet.PING);
}
});
}
private void onDrain() {
this.callbacks();
for (int i = 0; i < this.prevBufferLen; i++) {
@@ -578,6 +590,7 @@ public abstract class Socket extends Emitter {
private void onClose(String reason, Exception desc) {
if (this.readyState == ReadyState.OPENING || this.readyState == ReadyState.OPEN) {
logger.fine(String.format("socket close with reason: %s", reason));
final Socket self = this;
if (this.pingIntervalTimer != null) {
this.pingIntervalTimer.cancel(true);
}
@@ -587,12 +600,15 @@ public abstract class Socket extends Emitter {
EventThread.nextTick(new Runnable() {
@Override
public void run() {
Socket.this.writeBuffer.clear();
Socket.this.callbackBuffer.clear();
self.writeBuffer.clear();
self.callbackBuffer.clear();
}
});
ReadyState prev = this.readyState;
this.readyState = ReadyState.CLOSED;
this.emit(EVENT_CLOSE, reason, desc);
if (prev == ReadyState.OPEN) {
this.emit(EVENT_CLOSE, reason, desc);
}
this.onclose();
this.id = null;
}

View File

@@ -1,11 +1,13 @@
package com.github.nkzawa.engineio.client;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.engineio.client.transports.Polling;
import com.github.nkzawa.engineio.client.transports.WebSocket;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
@@ -35,4 +37,32 @@ public class SocketTest {
assertThat(socket.filterUpgrades(upgrades), is(expected));
}
/**
* should not emit close on incorrect connection.
*
* @throws URISyntaxException
*/
@Test
public void socketClosing() throws URISyntaxException, InterruptedException {
Socket socket = new Socket("ws://localhost:8080") {
@Override
public void onopen() {}
@Override
public void onmessage(String data) {}
@Override
public void onclose() {}
};
final boolean[] closed = {false};
socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
closed[0] = true;
}
});
socket.open();
Thread.sleep(200);
assertThat(closed[0], is(false));
}
}

View File

@@ -3,6 +3,6 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"engine.io": "0.5.0"
"engine.io": "0.6.1"
}
}