compatible with socket.io-client 1.1.0

This commit is contained in:
Naoyuki Kanezawa
2014-09-21 22:42:00 +09:00
parent 96d295aac2
commit 5f609900a0
10 changed files with 155 additions and 58 deletions

View File

@@ -1,4 +1,4 @@
package com.github.nkzawa.hasbinarydata;
package com.github.nkzawa.hasbinary;
import org.json.JSONArray;
import org.json.JSONException;
@@ -6,15 +6,15 @@ import org.json.JSONObject;
import java.util.Iterator;
public class HasBinaryData {
public class HasBinary {
private HasBinaryData() {}
private HasBinary() {}
public static boolean hasBinary(Object data) {
return recursiveCheckForBinary(data);
return _hasBinary(data);
}
private static boolean recursiveCheckForBinary(Object obj) {
private static boolean _hasBinary(Object obj) {
if (obj == null) return false;
if (obj instanceof byte[]) {
@@ -31,7 +31,7 @@ public class HasBinaryData {
} catch (JSONException e) {
return false;
}
if (recursiveCheckForBinary(v)) {
if (_hasBinary(v)) {
return true;
}
}
@@ -46,7 +46,7 @@ public class HasBinaryData {
} catch (JSONException e) {
return false;
}
if (recursiveCheckForBinary(v)) {
if (_hasBinary(v)) {
return true;
}
}

View File

@@ -196,7 +196,8 @@ public class Manager extends Emitter {
}
private void maybeReconnectOnOpen() {
if (!this.openReconnect && !this.reconnecting && this._reconnection) {
// Only try to reconnect if it's the first time we're connecting
if (!this.openReconnect && !this.reconnecting && this._reconnection && this.attempts == 0) {
this.openReconnect = true;
this.reconnect();
}
@@ -425,7 +426,7 @@ public class Manager extends Emitter {
while ((sub = this.subs.poll()) != null) sub.destroy();
}
private void close() {
/*package*/ void close() {
this.skipReconnect = true;
this.engine.close();
}

View File

@@ -1,7 +1,7 @@
package com.github.nkzawa.socketio.client;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.hasbinarydata.HasBinaryData;
import com.github.nkzawa.hasbinary.HasBinary;
import com.github.nkzawa.socketio.parser.Packet;
import com.github.nkzawa.socketio.parser.Parser;
import com.github.nkzawa.thread.EventThread;
@@ -173,7 +173,7 @@ public class Socket extends Emitter {
jsonArgs.put(arg);
}
int parserType = Parser.EVENT;
if (HasBinaryData.hasBinary(jsonArgs)) { parserType = Parser.BINARY_EVENT; }
if (HasBinary.hasBinary(jsonArgs)) { parserType = Parser.BINARY_EVENT; }
Packet<JSONArray> packet = new Packet<JSONArray>(parserType, jsonArgs);
if (_args.get(_args.size() - 1) instanceof Ack) {
@@ -324,7 +324,7 @@ public class Socket extends Emitter {
sent[0] = true;
logger.fine(String.format("sending ack %s", args.length != 0 ? args : null));
int type = HasBinaryData.hasBinary(args) ? Parser.BINARY_ACK : Parser.ACK;
int type = HasBinary.hasBinary(args) ? Parser.BINARY_ACK : Parser.ACK;
Packet<JSONArray> packet = new Packet<JSONArray>(type, new JSONArray(Arrays.asList(args)));
packet.id = id;
self.packet(packet);

View File

@@ -0,0 +1,73 @@
package com.github.nkzawa.hasbinary;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.nio.charset.Charset;
import static org.junit.Assert.assertTrue;
@RunWith(JUnit4.class)
public class HasBinaryTest {
@Test
public void byteArray() {
assertTrue(HasBinary.hasBinary(new byte[0]));
}
@Test
public void anArrayThatDoesNotContainByteArray() throws JSONException {
JSONArray arr = new JSONArray("[1, \"cool\", 2]");
assertTrue(!HasBinary.hasBinary(arr));
}
@Test
public void anArrayContainsByteArray() throws JSONException {
JSONArray arr = new JSONArray("[1, null, 2]");
arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinary.hasBinary(arr));
}
@Test
public void anObjectThatDoesNotContainByteArray() throws JSONException {
JSONObject ob = new JSONObject("{\"a\": \"a\", \"b\": [], \"c\": 1234}");
assertTrue(!HasBinary.hasBinary(ob));
}
@Test
public void anObjectThatContainsByteArray() throws JSONException {
JSONObject ob = new JSONObject("{\"a\": \"a\", \"b\": null, \"c\": 1234}");
ob.put("b", "abc".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinary.hasBinary(ob));
}
@Test
public void testNull() {
assertTrue(!HasBinary.hasBinary(null));
}
@Test
public void aComplexObjectThatContainsNoBinary() throws JSONException {
JSONObject ob = new JSONObject();
ob.put("x", new JSONArray("[\"a\", \"b\", 123]"));
ob.put("y", JSONObject.NULL);
ob.put("z", new JSONObject("{\"a\": \"x\", \"b\": \"y\", \"c\": 3, \"d\": null}"));
ob.put("w", new JSONArray());
assertTrue(!HasBinary.hasBinary(ob));
}
@Test
public void aComplexObjectThatContainsBinary() throws JSONException {
JSONObject ob = new JSONObject();
ob.put("x", new JSONArray("[\"a\", \"b\", 123]"));
ob.put("y", JSONObject.NULL);
ob.put("z", new JSONObject("{\"a\": \"x\", \"b\": \"y\", \"c\": 3, \"d\": null}"));
ob.put("w", new JSONArray());
ob.put("bin", "xxx".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinary.hasBinary(ob));
}
}

View File

@@ -1,26 +0,0 @@
package com.github.nkzawa.hasbinarydata;
import org.json.JSONArray;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.nio.charset.Charset;
import static org.junit.Assert.assertTrue;
@RunWith(JUnit4.class)
public class HasBinaryDataTest {
@Test
public void arrayContainsByteArray() throws Exception {
JSONArray arr = new JSONArray("[1, null, 2]");
arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinaryData.hasBinary(arr));
}
@Test
public void byteArray() {
assertTrue(HasBinaryData.hasBinary(new byte[0]));
}
}

View File

@@ -11,7 +11,7 @@ import java.util.concurrent.*;
public abstract class Connection {
final static int TIMEOUT = 3000;
final static int TIMEOUT = 7000;
final static int PORT = 3000;
private Process serverProcess;

View File

@@ -144,7 +144,6 @@ public class ConnectionTest extends Connection {
};
socket = client();
final int[] i = new int[] {0};
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
@@ -178,8 +177,39 @@ public class ConnectionTest extends Connection {
foo.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer("done");
foo.close();
socket.close();
manager.close();
values.offer("done");
}
});
foo.open();
}
});
socket.open();
values.take();
}
@Test(timeout = TIMEOUT)
public void connectToNamespaceAfterConnectionGetsClosed() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final Manager manager = new Manager(new URI(uri()));
socket = manager.socket("/");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
final Socket foo = manager.socket("/foo");
foo.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
foo.close();
manager.close();
values.offer("done");
}
});
foo.open();
@@ -187,7 +217,6 @@ public class ConnectionTest extends Connection {
});
socket.open();
values.take();
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -197,6 +226,7 @@ public class ConnectionTest extends Connection {
socket.io().on(Manager.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
values.offer("done");
}
});
@@ -208,7 +238,6 @@ public class ConnectionTest extends Connection {
}
}, 500);
values.take();
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -239,7 +268,7 @@ public class ConnectionTest extends Connection {
opts.reconnection = true;
opts.reconnectionAttempts = 2;
opts.reconnectionDelay = 10;
Manager manager = new Manager(new URI("http://localhost:3940"), opts);
final Manager manager = new Manager(new URI("http://localhost:3940"), opts);
socket = manager.socket("/asd");
final int[] reconnects = new int[] {0};
Emitter.Listener cb = new Emitter.Listener() {
@@ -254,13 +283,14 @@ public class ConnectionTest extends Connection {
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
manager.close();
values.offer(reconnects[0]);
}
});
socket.open();
assertThat((Integer)values.take(), is(2));
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -271,7 +301,7 @@ public class ConnectionTest extends Connection {
opts.timeout = 0;
opts.reconnectionAttempts = 2;
opts.reconnectionDelay = 10;
Manager manager = new Manager(new URI(uri()), opts);
final Manager manager = new Manager(new URI(uri()), opts);
final int[] reconnects = new int[] {0};
Emitter.Listener reconnectCb = new Emitter.Listener() {
@@ -285,6 +315,8 @@ public class ConnectionTest extends Connection {
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
manager.close();
values.offer(reconnects[0]);
}
});
@@ -292,7 +324,6 @@ public class ConnectionTest extends Connection {
socket = manager.socket("/timeout");
socket.open();
assertThat((Integer)values.take(), is(2));
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -300,7 +331,7 @@ public class ConnectionTest extends Connection {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
IO.Options opts = new IO.Options();
opts.reconnection = false;
Manager manager = new Manager(new URI("http://localhost:9823"), opts);
final Manager manager = new Manager(new URI("http://localhost:9823"), opts);
Emitter.Listener cb = new Emitter.Listener() {
@Override
public void call(Object... objects) {
@@ -316,6 +347,8 @@ public class ConnectionTest extends Connection {
timer.schedule(new TimerTask() {
@Override
public void run() {
socket.close();
manager.close();
values.offer("done");
}
}, 1000);
@@ -325,7 +358,6 @@ public class ConnectionTest extends Connection {
socket = manager.socket("/invalid");
socket.open();
values.take();
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -337,7 +369,7 @@ public class ConnectionTest extends Connection {
opts.timeout = 0;
opts.reconnectionAttempts = 2;
opts.reconnectionDelay = 10;
Manager manager = new Manager(new URI(uri()), opts);
final Manager manager = new Manager(new URI(uri()), opts);
socket = manager.socket("/timeout_socket");
final int[] reconnects = new int[] {0};
@@ -353,13 +385,14 @@ public class ConnectionTest extends Connection {
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
manager.close();
values.offer(reconnects[0]);
}
});
socket.open();
assertThat((Integer)values.take(), is(reconnects[0]));
assertThat((Integer)values.take(), is(2));
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -371,7 +404,7 @@ public class ConnectionTest extends Connection {
opts.timeout = 0;
opts.reconnectionAttempts = 2;
opts.reconnectionDelay = 10;
Manager manager = new Manager(new URI(uri()), opts);
final Manager manager = new Manager(new URI(uri()), opts);
socket = manager.socket("/timeout_socket");
final int[] reconnects = new int[] {0};
@@ -387,13 +420,14 @@ public class ConnectionTest extends Connection {
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
manager.close();
values.offer(reconnects[0]);
}
});
socket.open();
assertThat((Integer)values.take(), is(reconnects[0]));
assertThat((Integer)values.take(), is(2));
socket.close();
}
@Test(timeout = TIMEOUT)
@@ -407,15 +441,14 @@ public class ConnectionTest extends Connection {
socket.on("echoBack", new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer(args[0]);
socket.close();
values.offer(args[0]);
}
});
}
});
socket.connect();
assertThat(values.take(), instanceOf(String.class));
socket.close();
}
@Test(timeout = TIMEOUT)

View File

@@ -3,6 +3,6 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"socket.io": "1.0.6"
"socket.io": "1.1.0"
}
}

View File

@@ -15,6 +15,22 @@ var port = process.env.PORT || 3000;
var nsp = process.argv[2] || '/';
var slice = Array.prototype.slice;
io.of('/foo').on('connection', function() {
// register namespace
});
io.of('/timeout_socket').on('connection', function() {
// register namespace
});
io.of('/valid').on('connection', function() {
// register namespace
});
io.of('/asd').on('connection', function() {
// register namespace
});
io.of(nsp).on('connection', function(socket) {
socket.send('hello client');