send query on connecti

This commit is contained in:
nkzawa
2017-07-12 01:09:11 +09:00
parent 2925cdb4e3
commit 4811368854
5 changed files with 47 additions and 5 deletions

View File

@@ -85,7 +85,12 @@ public class IO {
io = managers.get(id);
}
return io.socket(parsed.getPath());
String query = parsed.getQuery();
if (query != null && (opts.query == null || opts.query.isEmpty())) {
opts.query = query;
}
return io.socket(parsed.getPath(), opts);
}

View File

@@ -418,12 +418,13 @@ public class Manager extends Emitter {
* Initializes {@link Socket} instances for each namespaces.
*
* @param nsp namespace.
* @param opts options.
* @return a socket instance for the namespace.
*/
public Socket socket(String nsp) {
public Socket socket(String nsp, Options opts) {
Socket socket = this.nsps.get(nsp);
if (socket == null) {
socket = new Socket(this, nsp);
socket = new Socket(this, nsp, opts);
Socket _socket = this.nsps.putIfAbsent(nsp, socket);
if (_socket != null) {
socket = _socket;
@@ -447,6 +448,10 @@ public class Manager extends Emitter {
return socket;
}
public Socket socket(String nsp) {
return socket(nsp, null);
}
/*package*/ void destroy(Socket socket) {
this.connecting.remove(socket);
if (!this.connecting.isEmpty()) return;
@@ -458,6 +463,10 @@ public class Manager extends Emitter {
logger.fine(String.format("writing packet %s", packet));
final Manager self = this;
if (packet.query != null && !packet.query.isEmpty() && packet.type == Parser.CONNECT) {
packet.nsp += "?" + packet.query;
}
if (!self.encoding) {
self.encoding = true;
this.encoder.encode(packet, new Parser.Encoder.Callback() {

View File

@@ -79,6 +79,7 @@ public class Socket extends Emitter {
}};
/*package*/ String id;
/*package*/ String query;
private volatile boolean connected;
private int ids;
@@ -89,9 +90,12 @@ public class Socket extends Emitter {
private final Queue<List<Object>> receiveBuffer = new LinkedList<List<Object>>();
private final Queue<Packet<JSONArray>> sendBuffer = new LinkedList<Packet<JSONArray>>();
public Socket(Manager io, String nsp) {
public Socket(Manager io, String nsp, Manager.Options opts) {
this.io = io;
this.nsp = nsp;
if (opts != null) {
this.query = opts.query;
}
}
private void subEvents() {
@@ -268,7 +272,13 @@ public class Socket extends Emitter {
logger.fine("transport is open - connecting");
if (!"/".equals(this.nsp)) {
this.packet(new Packet(Parser.CONNECT));
if (this.query != null && !this.query.isEmpty()) {
Packet packet = new Packet(Parser.CONNECT);
packet.query = this.query;
this.packet(packet);
} else {
this.packet(new Packet(Parser.CONNECT));
}
}
}

View File

@@ -8,6 +8,7 @@ public class Packet<T> {
public String nsp;
public T data;
public int attachments;
public String query;
public Packet() {}

View File

@@ -168,4 +168,21 @@ public class SocketTest extends Connection {
socket.disconnect();
}
@Test(timeout = TIMEOUT)
public void shouldStoreQueryStringAsAProperty() throws URISyntaxException, InterruptedException {
IO.Options opts = new IO.Options();
opts.query = "a=b";
Socket socket = IO.socket(this.uri() + "/abc", opts);
Socket socket2 = IO.socket(this.uri() + "/abc?b=c&d=e");
IO.Options opts3 = new IO.Options();
opts.query = "%26a=%26%3D%3Fa";
Socket socket3 = IO.socket(this.uri() + "/abc", opts);
assertThat(socket.query, is("a=b"));
assertThat(socket2.query, is("b=c&d=e"));
assertThat(socket3.query, is("%26a=%26%3D%3Fa"));
}
}