Merge pull request #277 from cirocosta/binary-ack
Fixes event type when emitting binary_ack
This commit is contained in:
@@ -346,7 +346,13 @@ public class Socket extends Emitter {
|
|||||||
sent[0] = true;
|
sent[0] = true;
|
||||||
logger.fine(String.format("sending ack %s", args.length != 0 ? args : null));
|
logger.fine(String.format("sending ack %s", args.length != 0 ? args : null));
|
||||||
|
|
||||||
int type = HasBinary.hasBinary(args) ? Parser.BINARY_ACK : Parser.ACK;
|
JSONArray jsonArgs = new JSONArray();
|
||||||
|
for (Object arg : args) {
|
||||||
|
jsonArgs.put(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
int type = HasBinary.hasBinary(jsonArgs)
|
||||||
|
? Parser.BINARY_ACK : Parser.ACK;
|
||||||
Packet<JSONArray> packet = new Packet<JSONArray>(type, new JSONArray(Arrays.asList(args)));
|
Packet<JSONArray> packet = new Packet<JSONArray>(type, new JSONArray(Arrays.asList(args)));
|
||||||
packet.id = id;
|
packet.id = id;
|
||||||
self.packet(packet);
|
self.packet(packet);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package io.socket.client;
|
|||||||
import io.socket.emitter.Emitter;
|
import io.socket.emitter.Emitter;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
@@ -90,6 +91,7 @@ public class ConnectionTest extends Connection {
|
|||||||
@Test(timeout = TIMEOUT)
|
@Test(timeout = TIMEOUT)
|
||||||
public void receiveDateWithAck() throws URISyntaxException, InterruptedException {
|
public void receiveDateWithAck() throws URISyntaxException, InterruptedException {
|
||||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
|
||||||
socket = client();
|
socket = client();
|
||||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -111,6 +113,61 @@ public class ConnectionTest extends Connection {
|
|||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(timeout = TIMEOUT)
|
||||||
|
public void sendBinaryAck() throws URISyntaxException, InterruptedException {
|
||||||
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8"));
|
||||||
|
|
||||||
|
socket = client();
|
||||||
|
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||||
|
@Override
|
||||||
|
public void call(Object... objects) {
|
||||||
|
socket.emit("callAckBinary");
|
||||||
|
socket.on("ack", new Emitter.Listener() {
|
||||||
|
@Override
|
||||||
|
public void call(Object... args) {
|
||||||
|
Ack fn = (Ack) args[0];
|
||||||
|
fn.call(buf);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("ackBack", new Emitter.Listener() {
|
||||||
|
@Override
|
||||||
|
public void call(Object... args) {
|
||||||
|
byte[] data = (byte[])args[0];
|
||||||
|
values.offer(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.connect();
|
||||||
|
Assert.assertArrayEquals(buf, (byte[])values.take());
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = TIMEOUT)
|
||||||
|
public void receiveBinaryDataWithAck() throws URISyntaxException, InterruptedException {
|
||||||
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8"));
|
||||||
|
|
||||||
|
socket = client();
|
||||||
|
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||||
|
@Override
|
||||||
|
public void call(Object... objects) {
|
||||||
|
socket.emit("getAckBinary", "", new Ack() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void call(Object... args) {
|
||||||
|
values.offer(args[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.connect();
|
||||||
|
Assert.assertArrayEquals(buf, (byte[])values.take());
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test(timeout = TIMEOUT)
|
@Test(timeout = TIMEOUT)
|
||||||
public void workWithFalse() throws URISyntaxException, InterruptedException {
|
public void workWithFalse() throws URISyntaxException, InterruptedException {
|
||||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
@@ -731,6 +788,7 @@ public class ConnectionTest extends Connection {
|
|||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test(timeout = TIMEOUT)
|
@Test(timeout = TIMEOUT)
|
||||||
public void sendAndGetBinaryData() throws URISyntaxException, InterruptedException {
|
public void sendAndGetBinaryData() throws URISyntaxException, InterruptedException {
|
||||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||||
|
|||||||
@@ -57,6 +57,17 @@ io.of(nsp).on('connection', function(socket) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('callAckBinary', function() {
|
||||||
|
socket.emit('ack', function(buf) {
|
||||||
|
socket.emit('ackBack', buf);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('getAckBinary', function(data, callback) {
|
||||||
|
var buf = new Buffer('huehue', 'utf8');
|
||||||
|
callback(buf);
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('getAckDate', function(data, callback) {
|
socket.on('getAckDate', function(data, callback) {
|
||||||
callback(new Date());
|
callback(new Date());
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user