Skip to content

Commit 8591b0e

Browse files
committed
Merge pull request #277 from cirocosta/binary-ack
Fixes event type when emitting binary_ack
2 parents 9ed4195 + f0832ff commit 8591b0e

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/main/java/io/socket/client/Socket.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,13 @@ public void run() {
346346
sent[0] = true;
347347
logger.fine(String.format("sending ack %s", args.length != 0 ? args : null));
348348

349-
int type = HasBinary.hasBinary(args) ? Parser.BINARY_ACK : Parser.ACK;
349+
JSONArray jsonArgs = new JSONArray();
350+
for (Object arg : args) {
351+
jsonArgs.put(arg);
352+
}
353+
354+
int type = HasBinary.hasBinary(jsonArgs)
355+
? Parser.BINARY_ACK : Parser.ACK;
350356
Packet<JSONArray> packet = new Packet<JSONArray>(type, new JSONArray(Arrays.asList(args)));
351357
packet.id = id;
352358
self.packet(packet);

src/test/java/io/socket/client/ConnectionTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.socket.emitter.Emitter;
44
import org.json.JSONException;
55
import org.json.JSONObject;
6+
import org.junit.Assert;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
89
import org.junit.runners.JUnit4;
@@ -90,6 +91,7 @@ public void call(Object... args) {
9091
@Test(timeout = TIMEOUT)
9192
public void receiveDateWithAck() throws URISyntaxException, InterruptedException {
9293
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
94+
9395
socket = client();
9496
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
9597
@Override
@@ -111,6 +113,61 @@ public void call(Object... args) {
111113
socket.close();
112114
}
113115

116+
@Test(timeout = TIMEOUT)
117+
public void sendBinaryAck() throws URISyntaxException, InterruptedException {
118+
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
119+
final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8"));
120+
121+
socket = client();
122+
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
123+
@Override
124+
public void call(Object... objects) {
125+
socket.emit("callAckBinary");
126+
socket.on("ack", new Emitter.Listener() {
127+
@Override
128+
public void call(Object... args) {
129+
Ack fn = (Ack) args[0];
130+
fn.call(buf);
131+
}
132+
});
133+
134+
socket.on("ackBack", new Emitter.Listener() {
135+
@Override
136+
public void call(Object... args) {
137+
byte[] data = (byte[])args[0];
138+
values.offer(data);
139+
}
140+
});
141+
}
142+
});
143+
socket.connect();
144+
Assert.assertArrayEquals(buf, (byte[])values.take());
145+
socket.close();
146+
}
147+
148+
@Test(timeout = TIMEOUT)
149+
public void receiveBinaryDataWithAck() throws URISyntaxException, InterruptedException {
150+
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
151+
final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8"));
152+
153+
socket = client();
154+
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
155+
@Override
156+
public void call(Object... objects) {
157+
socket.emit("getAckBinary", "", new Ack() {
158+
159+
@Override
160+
public void call(Object... args) {
161+
values.offer(args[0]);
162+
}
163+
});
164+
}
165+
});
166+
socket.connect();
167+
Assert.assertArrayEquals(buf, (byte[])values.take());
168+
socket.close();
169+
}
170+
114171
@Test(timeout = TIMEOUT)
115172
public void workWithFalse() throws URISyntaxException, InterruptedException {
116173
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
@@ -731,6 +788,7 @@ public void call(Object... args) {
731788
socket.close();
732789
}
733790

791+
734792
@Test(timeout = TIMEOUT)
735793
public void sendAndGetBinaryData() throws URISyntaxException, InterruptedException {
736794
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();

src/test/resources/server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ io.of(nsp).on('connection', function(socket) {
5757
});
5858
});
5959

60+
socket.on('callAckBinary', function() {
61+
socket.emit('ack', function(buf) {
62+
socket.emit('ackBack', buf);
63+
});
64+
});
65+
66+
socket.on('getAckBinary', function(data, callback) {
67+
var buf = new Buffer('huehue', 'utf8');
68+
callback(buf);
69+
});
70+
6071
socket.on('getAckDate', function(data, callback) {
6172
callback(new Date());
6273
});

0 commit comments

Comments
 (0)