enable to send cookie

This commit is contained in:
Naoyuki Kanezawa
2013-05-09 02:20:13 +09:00
parent c61f2b9e81
commit 080d4d8935
6 changed files with 59 additions and 0 deletions

View File

@@ -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.
http://nkzawa.github.io/engine.io-client.java/apidocs/

View File

@@ -102,6 +102,7 @@ public abstract class Socket extends Emitter {
private String hostname;
private String path;
private String timestampParam;
private String cookie;
private List<String> transports;
private List<String> upgrades;
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 ?
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
this.cookie = opts.cookie;
Socket.sockets.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.timestampParam = this.timestampParam;
opts.policyPort = this.policyPort;
opts.cookie = this.cookie;
if (WebSocket.NAME.equals(name)) {
return new WebSocket(opts);

View File

@@ -121,6 +121,11 @@ public abstract class Transport extends Emitter {
public static class Options {
/**
* Cookie value for handshake.
*/
public String cookie;
public String hostname;
public String path;
public String timestampParam;

View File

@@ -17,9 +17,11 @@ public class PollingXHR extends Polling {
private Request sendXhr;
private Request pollXhr;
private String cookie;
public PollingXHR(Options opts) {
super(opts);
this.cookie = opts.cookie;
}
protected Request request() {
@@ -31,6 +33,7 @@ public class PollingXHR extends Polling {
opts = new Request.Options();
}
opts.uri = this.uri();
opts.cookie = this.cookie;
return new Request(opts);
}
@@ -110,12 +113,14 @@ public class PollingXHR extends Polling {
String method;
String uri;
String data;
String cookie;
HttpURLConnection xhr;
public Request(Options opts) {
this.method = opts.method != null ? opts.method : "GET";
this.uri = opts.uri;
this.data = opts.data;
this.cookie = opts.cookie;
}
public void create() {
@@ -134,6 +139,10 @@ public class PollingXHR extends Polling {
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));
xhrService.submit(new Runnable() {
@Override
@@ -201,6 +210,7 @@ public class PollingXHR extends Polling {
public String uri;
public String method;
public String data;
public String cookie;
}
}
}

View File

@@ -217,4 +217,31 @@ public class ServerConnectionTest {
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();
}
}

View File

@@ -7,6 +7,11 @@ var engine = require('engine.io')
server.on('connection', function(socket) {
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) {
console.log('packet:', packet);
});