Skip to content

Commit 4811368

Browse files
committed
send query on connecti
1 parent 2925cdb commit 4811368

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ public static Socket socket(URI uri, Options opts) {
8585
io = managers.get(id);
8686
}
8787

88-
return io.socket(parsed.getPath());
88+
String query = parsed.getQuery();
89+
if (query != null && (opts.query == null || opts.query.isEmpty())) {
90+
opts.query = query;
91+
}
92+
93+
return io.socket(parsed.getPath(), opts);
8994
}
9095

9196

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,13 @@ private void onerror(Exception err) {
418418
* Initializes {@link Socket} instances for each namespaces.
419419
*
420420
* @param nsp namespace.
421+
* @param opts options.
421422
* @return a socket instance for the namespace.
422423
*/
423-
public Socket socket(String nsp) {
424+
public Socket socket(String nsp, Options opts) {
424425
Socket socket = this.nsps.get(nsp);
425426
if (socket == null) {
426-
socket = new Socket(this, nsp);
427+
socket = new Socket(this, nsp, opts);
427428
Socket _socket = this.nsps.putIfAbsent(nsp, socket);
428429
if (_socket != null) {
429430
socket = _socket;
@@ -447,6 +448,10 @@ public void call(Object... objects) {
447448
return socket;
448449
}
449450

451+
public Socket socket(String nsp) {
452+
return socket(nsp, null);
453+
}
454+
450455
/*package*/ void destroy(Socket socket) {
451456
this.connecting.remove(socket);
452457
if (!this.connecting.isEmpty()) return;
@@ -458,6 +463,10 @@ public void call(Object... objects) {
458463
logger.fine(String.format("writing packet %s", packet));
459464
final Manager self = this;
460465

466+
if (packet.query != null && !packet.query.isEmpty() && packet.type == Parser.CONNECT) {
467+
packet.nsp += "?" + packet.query;
468+
}
469+
461470
if (!self.encoding) {
462471
self.encoding = true;
463472
this.encoder.encode(packet, new Parser.Encoder.Callback() {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class Socket extends Emitter {
7979
}};
8080

8181
/*package*/ String id;
82+
/*package*/ String query;
8283

8384
private volatile boolean connected;
8485
private int ids;
@@ -89,9 +90,12 @@ public class Socket extends Emitter {
8990
private final Queue<List<Object>> receiveBuffer = new LinkedList<List<Object>>();
9091
private final Queue<Packet<JSONArray>> sendBuffer = new LinkedList<Packet<JSONArray>>();
9192

92-
public Socket(Manager io, String nsp) {
93+
public Socket(Manager io, String nsp, Manager.Options opts) {
9394
this.io = io;
9495
this.nsp = nsp;
96+
if (opts != null) {
97+
this.query = opts.query;
98+
}
9599
}
96100

97101
private void subEvents() {
@@ -268,7 +272,13 @@ private void onopen() {
268272
logger.fine("transport is open - connecting");
269273

270274
if (!"/".equals(this.nsp)) {
271-
this.packet(new Packet(Parser.CONNECT));
275+
if (this.query != null && !this.query.isEmpty()) {
276+
Packet packet = new Packet(Parser.CONNECT);
277+
packet.query = this.query;
278+
this.packet(packet);
279+
} else {
280+
this.packet(new Packet(Parser.CONNECT));
281+
}
272282
}
273283
}
274284

src/main/java/io/socket/parser/Packet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class Packet<T> {
88
public String nsp;
99
public T data;
1010
public int attachments;
11+
public String query;
1112

1213
public Packet() {}
1314

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,21 @@ public void call(Object... objects) {
168168

169169
socket.disconnect();
170170
}
171+
172+
@Test(timeout = TIMEOUT)
173+
public void shouldStoreQueryStringAsAProperty() throws URISyntaxException, InterruptedException {
174+
IO.Options opts = new IO.Options();
175+
opts.query = "a=b";
176+
Socket socket = IO.socket(this.uri() + "/abc", opts);
177+
178+
Socket socket2 = IO.socket(this.uri() + "/abc?b=c&d=e");
179+
180+
IO.Options opts3 = new IO.Options();
181+
opts.query = "%26a=%26%3D%3Fa";
182+
Socket socket3 = IO.socket(this.uri() + "/abc", opts);
183+
184+
assertThat(socket.query, is("a=b"));
185+
assertThat(socket2.query, is("b=c&d=e"));
186+
assertThat(socket3.query, is("%26a=%26%3D%3Fa"));
187+
}
171188
}

0 commit comments

Comments
 (0)