enable to send cookie
This commit is contained in:
@@ -36,6 +36,15 @@ socket.on(Socket.EVENT_ERROR, new Emitter.Listener() {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
How to set options:
|
||||||
|
|
||||||
|
```java
|
||||||
|
opts = new Socket.Options();
|
||||||
|
opts.cookie = "foo=1;"
|
||||||
|
|
||||||
|
socket = new Socket("ws://localhost", opts) { ... };
|
||||||
|
```
|
||||||
|
|
||||||
See the Javadoc for more details.
|
See the Javadoc for more details.
|
||||||
|
|
||||||
http://nkzawa.github.io/engine.io-client.java/apidocs/
|
http://nkzawa.github.io/engine.io-client.java/apidocs/
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ public abstract class Socket extends Emitter {
|
|||||||
private String hostname;
|
private String hostname;
|
||||||
private String path;
|
private String path;
|
||||||
private String timestampParam;
|
private String timestampParam;
|
||||||
|
private String cookie;
|
||||||
private List<String> transports;
|
private List<String> transports;
|
||||||
private List<String> upgrades;
|
private List<String> upgrades;
|
||||||
private Map<String, String> query;
|
private Map<String, String> query;
|
||||||
@@ -165,6 +166,7 @@ public abstract class Socket extends Emitter {
|
|||||||
this.transports = new ArrayList<String>(Arrays.asList(opts.transports != null ?
|
this.transports = new ArrayList<String>(Arrays.asList(opts.transports != null ?
|
||||||
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
|
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
|
||||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||||
|
this.cookie = opts.cookie;
|
||||||
|
|
||||||
Socket.sockets.add(this);
|
Socket.sockets.add(this);
|
||||||
Socket.sockets.evs.emit(Sockets.EVENT_ADD, this);
|
Socket.sockets.evs.emit(Sockets.EVENT_ADD, this);
|
||||||
@@ -204,6 +206,7 @@ public abstract class Socket extends Emitter {
|
|||||||
opts.timestampRequests = this.timestampRequests;
|
opts.timestampRequests = this.timestampRequests;
|
||||||
opts.timestampParam = this.timestampParam;
|
opts.timestampParam = this.timestampParam;
|
||||||
opts.policyPort = this.policyPort;
|
opts.policyPort = this.policyPort;
|
||||||
|
opts.cookie = this.cookie;
|
||||||
|
|
||||||
if (WebSocket.NAME.equals(name)) {
|
if (WebSocket.NAME.equals(name)) {
|
||||||
return new WebSocket(opts);
|
return new WebSocket(opts);
|
||||||
|
|||||||
@@ -121,6 +121,11 @@ public abstract class Transport extends Emitter {
|
|||||||
|
|
||||||
public static class Options {
|
public static class Options {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cookie value for handshake.
|
||||||
|
*/
|
||||||
|
public String cookie;
|
||||||
|
|
||||||
public String hostname;
|
public String hostname;
|
||||||
public String path;
|
public String path;
|
||||||
public String timestampParam;
|
public String timestampParam;
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ public class PollingXHR extends Polling {
|
|||||||
|
|
||||||
private Request sendXhr;
|
private Request sendXhr;
|
||||||
private Request pollXhr;
|
private Request pollXhr;
|
||||||
|
private String cookie;
|
||||||
|
|
||||||
public PollingXHR(Options opts) {
|
public PollingXHR(Options opts) {
|
||||||
super(opts);
|
super(opts);
|
||||||
|
this.cookie = opts.cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Request request() {
|
protected Request request() {
|
||||||
@@ -31,6 +33,7 @@ public class PollingXHR extends Polling {
|
|||||||
opts = new Request.Options();
|
opts = new Request.Options();
|
||||||
}
|
}
|
||||||
opts.uri = this.uri();
|
opts.uri = this.uri();
|
||||||
|
opts.cookie = this.cookie;
|
||||||
return new Request(opts);
|
return new Request(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,12 +113,14 @@ public class PollingXHR extends Polling {
|
|||||||
String method;
|
String method;
|
||||||
String uri;
|
String uri;
|
||||||
String data;
|
String data;
|
||||||
|
String cookie;
|
||||||
HttpURLConnection xhr;
|
HttpURLConnection xhr;
|
||||||
|
|
||||||
public Request(Options opts) {
|
public Request(Options opts) {
|
||||||
this.method = opts.method != null ? opts.method : "GET";
|
this.method = opts.method != null ? opts.method : "GET";
|
||||||
this.uri = opts.uri;
|
this.uri = opts.uri;
|
||||||
this.data = opts.data;
|
this.data = opts.data;
|
||||||
|
this.cookie = opts.cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create() {
|
public void create() {
|
||||||
@@ -134,6 +139,10 @@ public class PollingXHR extends Polling {
|
|||||||
xhr.setRequestProperty("Content-type", "text/plain;charset=UTF-8");
|
xhr.setRequestProperty("Content-type", "text/plain;charset=UTF-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.cookie != null) {
|
||||||
|
xhr.setRequestProperty("Cookie", this.cookie);
|
||||||
|
}
|
||||||
|
|
||||||
logger.fine(String.format("sending xhr with url %s | data %s", this.uri, this.data));
|
logger.fine(String.format("sending xhr with url %s | data %s", this.uri, this.data));
|
||||||
xhrService.submit(new Runnable() {
|
xhrService.submit(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -201,6 +210,7 @@ public class PollingXHR extends Polling {
|
|||||||
public String uri;
|
public String uri;
|
||||||
public String method;
|
public String method;
|
||||||
public String data;
|
public String data;
|
||||||
|
public String cookie;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -217,4 +217,31 @@ public class ServerConnectionTest {
|
|||||||
|
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(timeout = TIMEOUT)
|
||||||
|
public void cookie() throws URISyntaxException, InterruptedException {
|
||||||
|
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
|
||||||
|
|
||||||
|
Socket.Options opts = new Socket.Options();
|
||||||
|
opts.cookie = "foo=1;";
|
||||||
|
|
||||||
|
socket = new Socket("ws://localhost:" + PORT, opts) {
|
||||||
|
@Override
|
||||||
|
public void onopen() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onmessage(String data) {
|
||||||
|
System.out.println("onmessage: " + data);
|
||||||
|
messages.offer(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onclose() {}
|
||||||
|
};
|
||||||
|
socket.open();
|
||||||
|
|
||||||
|
assertThat(messages.take(), is("hello client"));
|
||||||
|
assertThat(messages.take(), is(opts.cookie));
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,11 @@ var engine = require('engine.io')
|
|||||||
server.on('connection', function(socket) {
|
server.on('connection', function(socket) {
|
||||||
socket.send('hello client');
|
socket.send('hello client');
|
||||||
|
|
||||||
|
if (socket.request.headers.cookie) {
|
||||||
|
console.log('cookie:', socket.request.headers.cookie);
|
||||||
|
socket.send(socket.request.headers.cookie);
|
||||||
|
}
|
||||||
|
|
||||||
socket.on('packet', function(packet) {
|
socket.on('packet', function(packet) {
|
||||||
console.log('packet:', packet);
|
console.log('packet:', packet);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user