Skip to content

Commit bf08624

Browse files
committed
implement query string during connection
1 parent 97ac6b0 commit bf08624

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>org.java-websocket</groupId>
4343
<artifactId>Java-WebSocket</artifactId>
44-
<version>1.3.0-SNAPSHOT</version>
44+
<version>1.3.0</version>
4545
</dependency>
4646

4747
<dependency>

src/main/java/io/socket/IOConnection.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class IOConnection implements IOCallback {
104104

105105
/** Custom Request headers used while handshaking */
106106
private Properties headers;
107+
108+
private String queryString;
107109

108110
/**
109111
* The first socket to be connected. the socket.io server does not send a
@@ -151,9 +153,7 @@ private class HearbeatTimeoutTask extends TimerTask {
151153
*/
152154
@Override
153155
public void run() {
154-
error(new SocketIOException(
155-
"Timeout Error. No heartbeat from server within life time of the socket. closing.",
156-
lastException));
156+
error(new SocketIOException( "Timeout Error. No heartbeat from server within life time of the socket. closing.", lastException));
157157
}
158158
}
159159

@@ -298,19 +298,23 @@ private void handshake() {
298298
URLConnection connection;
299299
try {
300300
setState(STATE_HANDSHAKE);
301-
url = new URL(IOConnection.this.url.toString() + SOCKET_IO_1);
301+
302+
String connectionUrl = IOConnection.this.url.toString() + SOCKET_IO_1;
303+
if ( this.queryString != null ) {
304+
connectionUrl += "?" + this.queryString;
305+
}
306+
307+
url = new URL( connectionUrl );
302308
connection = url.openConnection();
303309
if (connection instanceof HttpsURLConnection) {
304-
((HttpsURLConnection) connection)
305-
.setSSLSocketFactory(sslContext.getSocketFactory());
310+
((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory());
306311
}
307312
connection.setConnectTimeout(connectTimeout);
308313
connection.setReadTimeout(connectTimeout);
309314

310315
/* Setting the request headers */
311316
for (Entry<Object, Object> entry : headers.entrySet()) {
312-
connection.setRequestProperty((String) entry.getKey(),
313-
(String) entry.getValue());
317+
connection.setRequestProperty( (String) entry.getKey(), (String) entry.getValue() );
314318
}
315319

316320
InputStream stream = connection.getInputStream();
@@ -338,8 +342,7 @@ private synchronized void connectTransport() {
338342
else if (protocols.contains(XhrTransport.TRANSPORT_NAME))
339343
transport = XhrTransport.create(url, this);
340344
else {
341-
error(new SocketIOException(
342-
"Server supports no available transports. You should reconfigure the server to support a available transport"));
345+
error(new SocketIOException( "Server supports no available transports. You should reconfigure the server to support a available transport") );
343346
return;
344347
}
345348
transport.connect();
@@ -370,13 +373,10 @@ public void ack(JsonElement... args) {
370373
try {
371374
array.add(o);
372375
} catch (Exception e) {
373-
error(new SocketIOException(
374-
"You can only put values in IOAcknowledge.ack() which can be handled by JSONArray.put()",
375-
e));
376+
error(new SocketIOException("You can only put values in IOAcknowledge.ack() which can be handled by JSONArray.put()", e));
376377
}
377378
}
378-
IOMessage ackMsg = new IOMessage(IOMessage.TYPE_ACK, endPoint,
379-
id + array.toString());
379+
IOMessage ackMsg = new IOMessage(IOMessage.TYPE_ACK, endPoint, id + array.toString());
380380
sendPlain(ackMsg.toString());
381381
}
382382
};
@@ -410,6 +410,7 @@ private IOConnection(String url, SocketIO socket) {
410410
try {
411411
this.url = new URL(url);
412412
this.urlStr = url;
413+
this.queryString = socket.getQueryString();
413414
} catch (MalformedURLException e) {
414415
throw new RuntimeException(e);
415416
}
@@ -570,7 +571,7 @@ public void transportData(String text) {
570571
.listIterator(1);
571572
while (fragments.hasNext()) {
572573
int length = Integer.parseInt(fragments.next());
573-
String string = (String) fragments.next();
574+
String string = fragments.next();
574575
// Potential BUG: it is not defined if length is in bytes or
575576
// characters. Assuming characters.
576577

src/main/java/io/socket/SocketIO.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class SocketIO {
3434
private Properties headers = new Properties();
3535

3636
private URL url;
37+
38+
private String queryString;
3739

3840
/**
3941
* Instantiates a new socket.io connection. The object connects after
@@ -210,8 +212,7 @@ private boolean setAndConnect(URL url, IOCallback callback) {
210212
this.callback = callback;
211213
}
212214
if (this.callback != null && this.url != null) {
213-
final String origin = this.url.getProtocol() + "://"
214-
+ this.url.getAuthority();
215+
final String origin = this.url.getProtocol() + "://" + this.url.getAuthority();
215216
this.namespace = this.url.getPath();
216217
if (this.namespace.equals("/")) {
217218
this.namespace = "";
@@ -363,6 +364,14 @@ public String getTransport() {
363364
public Properties getHeaders() {
364365
return headers;
365366
}
367+
368+
public String getQueryString() {
369+
return this.queryString;
370+
}
371+
372+
public void setQueryString(String query) {
373+
this.queryString = query;
374+
}
366375

367376
/**
368377
* Sets the headers used while handshaking. Internally used. Use

0 commit comments

Comments
 (0)