compitible with engine.io 0.8.2
This commit is contained in:
@@ -56,6 +56,8 @@ public abstract class Socket extends Emitter {
|
||||
*/
|
||||
public static final String EVENT_ERROR = "error";
|
||||
|
||||
public static final String EVENT_UPGRADE_ERROR = "upgradeError";
|
||||
|
||||
/**
|
||||
* Called on completing a buffer flush.
|
||||
*/
|
||||
@@ -79,11 +81,6 @@ public abstract class Socket extends Emitter {
|
||||
public void run() {}
|
||||
};
|
||||
|
||||
/**
|
||||
* List of Socket instances.
|
||||
*/
|
||||
public static final Sockets sockets = new Sockets();
|
||||
|
||||
/**
|
||||
* The protocol version.
|
||||
*/
|
||||
@@ -167,9 +164,6 @@ public abstract class Socket extends Emitter {
|
||||
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
|
||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||
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() {
|
||||
@Override
|
||||
public void run() {
|
||||
String transportName = Socket.this.transports.get(0);
|
||||
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);
|
||||
transport.open();
|
||||
}
|
||||
@@ -218,10 +213,11 @@ public abstract class Socket extends Emitter {
|
||||
}
|
||||
|
||||
private void setTransport(Transport transport) {
|
||||
logger.fine(String.format("setting transport %s", transport.name));
|
||||
final Socket self = this;
|
||||
|
||||
if (this.transport != null) {
|
||||
logger.fine("clearing existing transport");
|
||||
logger.fine(String.format("clearing existing transport %s", this.transport.name));
|
||||
this.transport.off();
|
||||
}
|
||||
|
||||
@@ -271,7 +267,7 @@ public abstract class Socket extends Emitter {
|
||||
transport[0].close();
|
||||
transport[0] = null;
|
||||
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));
|
||||
EngineIOException err = new EngineIOException("probe error");
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sockets extends ArrayList<Socket> {
|
||||
|
||||
public static final String EVENT_ADD = "add";
|
||||
|
||||
public Emitter evs = new Emitter();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,6 +126,7 @@ public class PollingXHR extends Polling {
|
||||
public void create() {
|
||||
final Request self = this;
|
||||
try {
|
||||
logger.fine(String.format("xhr open %s: %s", this.method, this.uri));
|
||||
URL url = new URL(this.uri);
|
||||
xhr = (HttpURLConnection)url.openConnection();
|
||||
xhr.setRequestMethod(this.method);
|
||||
@@ -158,13 +159,23 @@ public class PollingXHR extends Polling {
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
String line;
|
||||
StringBuilder data = new StringBuilder();
|
||||
reader = new BufferedReader(new InputStreamReader(xhr.getInputStream()));
|
||||
while ((line = reader.readLine()) != null) {
|
||||
data.append(line);
|
||||
StringBuilder data = null;
|
||||
|
||||
final int statusCode = xhr.getResponseCode();
|
||||
if (HttpURLConnection.HTTP_OK == statusCode) {
|
||||
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) {
|
||||
self.onError(e);
|
||||
} finally {
|
||||
|
||||
@@ -10,6 +10,8 @@ import org.junit.runners.JUnit4;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -47,7 +49,7 @@ public class SocketTest {
|
||||
*/
|
||||
@Test
|
||||
public void socketClosing() throws URISyntaxException, InterruptedException {
|
||||
Socket socket = new Socket("ws://localhost:8080") {
|
||||
Socket socket = new Socket("ws://0.0.0.0:8080") {
|
||||
@Override
|
||||
public void onopen() {}
|
||||
@Override
|
||||
@@ -59,6 +61,19 @@ public class SocketTest {
|
||||
};
|
||||
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() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
@@ -66,8 +81,5 @@ public class SocketTest {
|
||||
}
|
||||
});
|
||||
socket.open();
|
||||
|
||||
Thread.sleep(200);
|
||||
assertThat(closed[0], is(false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public class TransportTest {
|
||||
opt.query = new HashMap<String, String>() {{
|
||||
put("sid", "test");
|
||||
}};
|
||||
opt.timestampRequests = false;
|
||||
Polling polling = new Polling(opt);
|
||||
assertThat(polling.uri(), is("http://localhost/engine.io?sid=test"));
|
||||
}
|
||||
@@ -37,6 +38,7 @@ public class TransportTest {
|
||||
put("sid", "test");
|
||||
}};
|
||||
opt.port = 80;
|
||||
opt.timestampRequests = false;
|
||||
Polling polling = new Polling(opt);
|
||||
assertThat(polling.uri(), is("http://localhost/engine.io?sid=test"));
|
||||
}
|
||||
@@ -51,6 +53,7 @@ public class TransportTest {
|
||||
put("sid", "test");
|
||||
}};
|
||||
opt.port = 3000;
|
||||
opt.timestampRequests = false;
|
||||
Polling polling = new Polling(opt);
|
||||
assertThat(polling.uri(), is("http://localhost:3000/engine.io?sid=test"));
|
||||
}
|
||||
@@ -65,6 +68,7 @@ public class TransportTest {
|
||||
put("sid", "test");
|
||||
}};
|
||||
opt.port = 443;
|
||||
opt.timestampRequests = false;
|
||||
Polling polling = new Polling(opt);
|
||||
assertThat(polling.uri(), is("https://localhost/engine.io?sid=test"));
|
||||
}
|
||||
@@ -89,6 +93,7 @@ public class TransportTest {
|
||||
opt.query = new HashMap<String, String>() {{
|
||||
put("transport", "websocket");
|
||||
}};
|
||||
opt.timestampRequests = false;
|
||||
WS ws = new WS(opt);
|
||||
assertThat(ws.uri(), is("ws://test/engine.io?transport=websocket"));
|
||||
}
|
||||
@@ -99,6 +104,7 @@ public class TransportTest {
|
||||
opt.path ="/engine.io";
|
||||
opt.hostname = "test";
|
||||
opt.secure = true;
|
||||
opt.timestampRequests = false;
|
||||
WS ws = new WS(opt);
|
||||
assertThat(ws.uri(), is("wss://test/engine.io"));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"engine.io": "0.7.9"
|
||||
"engine.io": "0.8.2"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user