compitible with engine.io 0.8.2

This commit is contained in:
Naoyuki Kanezawa
2014-01-27 05:36:22 +09:00
parent 803ee3bb25
commit 84f97d8caf
5 changed files with 48 additions and 30 deletions

View File

@@ -56,6 +56,8 @@ public abstract class Socket extends Emitter {
*/ */
public static final String EVENT_ERROR = "error"; public static final String EVENT_ERROR = "error";
public static final String EVENT_UPGRADE_ERROR = "upgradeError";
/** /**
* Called on completing a buffer flush. * Called on completing a buffer flush.
*/ */
@@ -79,11 +81,6 @@ public abstract class Socket extends Emitter {
public void run() {} public void run() {}
}; };
/**
* List of Socket instances.
*/
public static final Sockets sockets = new Sockets();
/** /**
* The protocol version. * The protocol version.
*/ */
@@ -167,9 +164,6 @@ public abstract class Socket extends Emitter {
opts.transports : new String[]{Polling.NAME, WebSocket.NAME})); opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843; this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
this.cookie = opts.cookie; this.cookie = opts.cookie;
Socket.sockets.add(this);
Socket.sockets.evs.emit(Sockets.EVENT_ADD, this);
} }
/** /**
@@ -179,8 +173,9 @@ public abstract class Socket extends Emitter {
EventThread.exec(new Runnable() { EventThread.exec(new Runnable() {
@Override @Override
public void run() { public void run() {
String transportName = Socket.this.transports.get(0);
Socket.this.readyState = ReadyState.OPENING; Socket.this.readyState = ReadyState.OPENING;
Transport transport = Socket.this.createTransport(Socket.this.transports.get(0)); Transport transport = Socket.this.createTransport(transportName);
Socket.this.setTransport(transport); Socket.this.setTransport(transport);
transport.open(); transport.open();
} }
@@ -218,10 +213,11 @@ public abstract class Socket extends Emitter {
} }
private void setTransport(Transport transport) { private void setTransport(Transport transport) {
logger.fine(String.format("setting transport %s", transport.name));
final Socket self = this; final Socket self = this;
if (this.transport != null) { if (this.transport != null) {
logger.fine("clearing existing transport"); logger.fine(String.format("clearing existing transport %s", this.transport.name));
this.transport.off(); this.transport.off();
} }
@@ -271,7 +267,7 @@ public abstract class Socket extends Emitter {
transport[0].close(); transport[0].close();
transport[0] = null; transport[0] = null;
logger.fine(String.format("probing transport '%s' failed because of error: %s", name, err)); logger.fine(String.format("probing transport '%s' failed because of error: %s", name, err));
self.emit(EVENT_ERROR, error); self.emit(EVENT_UPGRADE_ERROR, error);
} }
}; };
@@ -317,7 +313,7 @@ public abstract class Socket extends Emitter {
logger.fine(String.format("probe transport '%s' failed", name)); logger.fine(String.format("probe transport '%s' failed", name));
EngineIOException err = new EngineIOException("probe error"); EngineIOException err = new EngineIOException("probe error");
//err.transport = transport[0].name; //err.transport = transport[0].name;
self.emit(EVENT_ERROR, err); self.emit(EVENT_UPGRADE_ERROR, err);
} }
} }
}); });
@@ -674,11 +670,4 @@ public abstract class Socket extends Emitter {
return opts; return opts;
} }
} }
public static class Sockets extends ArrayList<Socket> {
public static final String EVENT_ADD = "add";
public Emitter evs = new Emitter();
}
} }

View File

@@ -126,6 +126,7 @@ public class PollingXHR extends Polling {
public void create() { public void create() {
final Request self = this; final Request self = this;
try { try {
logger.fine(String.format("xhr open %s: %s", this.method, this.uri));
URL url = new URL(this.uri); URL url = new URL(this.uri);
xhr = (HttpURLConnection)url.openConnection(); xhr = (HttpURLConnection)url.openConnection();
xhr.setRequestMethod(this.method); xhr.setRequestMethod(this.method);
@@ -158,13 +159,23 @@ public class PollingXHR extends Polling {
writer.flush(); writer.flush();
} }
String line; StringBuilder data = null;
StringBuilder data = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(xhr.getInputStream())); final int statusCode = xhr.getResponseCode();
while ((line = reader.readLine()) != null) { if (HttpURLConnection.HTTP_OK == statusCode) {
data.append(line); String line;
data = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(xhr.getInputStream()));
while ((line = reader.readLine()) != null) {
data.append(line);
}
} else {
self.onError(new IOException(Integer.toString(statusCode)));
}
if (data != null) {
self.onData(data.toString());
} }
self.onData(data.toString());
} catch (IOException e) { } catch (IOException e) {
self.onError(e); self.onError(e);
} finally { } finally {

View File

@@ -10,6 +10,8 @@ import org.junit.runners.JUnit4;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -47,7 +49,7 @@ public class SocketTest {
*/ */
@Test @Test
public void socketClosing() throws URISyntaxException, InterruptedException { public void socketClosing() throws URISyntaxException, InterruptedException {
Socket socket = new Socket("ws://localhost:8080") { Socket socket = new Socket("ws://0.0.0.0:8080") {
@Override @Override
public void onopen() {} public void onopen() {}
@Override @Override
@@ -59,6 +61,19 @@ public class SocketTest {
}; };
final boolean[] closed = {false}; final boolean[] closed = {false};
socket.once(Socket.EVENT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
assertThat(closed[0], is(false));
}
}, 20);
}
});
socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() { socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
@@ -66,8 +81,5 @@ public class SocketTest {
} }
}); });
socket.open(); socket.open();
Thread.sleep(200);
assertThat(closed[0], is(false));
} }
} }

View File

@@ -23,6 +23,7 @@ public class TransportTest {
opt.query = new HashMap<String, String>() {{ opt.query = new HashMap<String, String>() {{
put("sid", "test"); put("sid", "test");
}}; }};
opt.timestampRequests = false;
Polling polling = new Polling(opt); Polling polling = new Polling(opt);
assertThat(polling.uri(), is("http://localhost/engine.io?sid=test")); assertThat(polling.uri(), is("http://localhost/engine.io?sid=test"));
} }
@@ -37,6 +38,7 @@ public class TransportTest {
put("sid", "test"); put("sid", "test");
}}; }};
opt.port = 80; opt.port = 80;
opt.timestampRequests = false;
Polling polling = new Polling(opt); Polling polling = new Polling(opt);
assertThat(polling.uri(), is("http://localhost/engine.io?sid=test")); assertThat(polling.uri(), is("http://localhost/engine.io?sid=test"));
} }
@@ -51,6 +53,7 @@ public class TransportTest {
put("sid", "test"); put("sid", "test");
}}; }};
opt.port = 3000; opt.port = 3000;
opt.timestampRequests = false;
Polling polling = new Polling(opt); Polling polling = new Polling(opt);
assertThat(polling.uri(), is("http://localhost:3000/engine.io?sid=test")); assertThat(polling.uri(), is("http://localhost:3000/engine.io?sid=test"));
} }
@@ -65,6 +68,7 @@ public class TransportTest {
put("sid", "test"); put("sid", "test");
}}; }};
opt.port = 443; opt.port = 443;
opt.timestampRequests = false;
Polling polling = new Polling(opt); Polling polling = new Polling(opt);
assertThat(polling.uri(), is("https://localhost/engine.io?sid=test")); assertThat(polling.uri(), is("https://localhost/engine.io?sid=test"));
} }
@@ -89,6 +93,7 @@ public class TransportTest {
opt.query = new HashMap<String, String>() {{ opt.query = new HashMap<String, String>() {{
put("transport", "websocket"); put("transport", "websocket");
}}; }};
opt.timestampRequests = false;
WS ws = new WS(opt); WS ws = new WS(opt);
assertThat(ws.uri(), is("ws://test/engine.io?transport=websocket")); assertThat(ws.uri(), is("ws://test/engine.io?transport=websocket"));
} }
@@ -99,6 +104,7 @@ public class TransportTest {
opt.path ="/engine.io"; opt.path ="/engine.io";
opt.hostname = "test"; opt.hostname = "test";
opt.secure = true; opt.secure = true;
opt.timestampRequests = false;
WS ws = new WS(opt); WS ws = new WS(opt);
assertThat(ws.uri(), is("wss://test/engine.io")); assertThat(ws.uri(), is("wss://test/engine.io"));
} }

View File

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