Skip to content

Commit 0962904

Browse files
authored
Merge pull request #93 from socketio/fix/compatible-v3
Update to make compatible with engine.io v3
2 parents 8b1fbdc + ce1c29d commit 0962904

File tree

10 files changed

+354
-221
lines changed

10 files changed

+354
-221
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: java
2+
sudo: false
23
install: mvn install -DskipTests=true -Dgpg.skip=true
34
jdk:
45
- openjdk7
56
- oraclejdk8
7+
dist: trusty

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public String toString() {
121121
private String path;
122122
private String timestampParam;
123123
private List<String> transports;
124+
private Map<String, Transport.Options> transportOptions;
124125
private List<String> upgrades;
125126
private Map<String, String> query;
126127
/*package*/ LinkedList<Packet> writeBuffer = new LinkedList<Packet>();
@@ -202,6 +203,8 @@ public Socket(Options opts) {
202203
this.timestampRequests = opts.timestampRequests;
203204
this.transports = new ArrayList<String>(Arrays.asList(opts.transports != null ?
204205
opts.transports : new String[]{Polling.NAME, WebSocket.NAME}));
206+
this.transportOptions = opts.transportOptions != null ?
207+
opts.transportOptions : new HashMap<String, Transport.Options>();
205208
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
206209
this.rememberUpgrade = opts.rememberUpgrade;
207210
this.callFactory = opts.callFactory != null ? opts.callFactory : defaultCallFactory;
@@ -272,18 +275,22 @@ private Transport createTransport(String name) {
272275
query.put("sid", this.id);
273276
}
274277

278+
// per-transport options
279+
Transport.Options options = this.transportOptions.get(name);
280+
275281
Transport.Options opts = new Transport.Options();
276-
opts.hostname = this.hostname;
277-
opts.port = this.port;
278-
opts.secure = this.secure;
279-
opts.path = this.path;
280282
opts.query = query;
281-
opts.timestampRequests = this.timestampRequests;
282-
opts.timestampParam = this.timestampParam;
283-
opts.policyPort = this.policyPort;
284283
opts.socket = this;
285-
opts.callFactory = this.callFactory;
286-
opts.webSocketFactory = this.webSocketFactory;
284+
285+
opts.hostname = options != null ? options.hostname : this.hostname;
286+
opts.port = options != null ? options.port : this.port;
287+
opts.secure = options != null ? options.secure : this.secure;
288+
opts.path = options != null ? options.path : this.path;
289+
opts.timestampRequests = options != null ? options.timestampRequests : this.timestampRequests;
290+
opts.timestampParam = options != null ? options.timestampParam : this.timestampParam;
291+
opts.policyPort = options != null ? options.policyPort : this.policyPort;
292+
opts.callFactory = options != null ? options.callFactory : this.callFactory;
293+
opts.webSocketFactory = options != null ? options.webSocketFactory : this.webSocketFactory;
287294

288295
Transport transport;
289296
if (WebSocket.NAME.equals(name)) {
@@ -866,7 +873,7 @@ public static class Options extends Transport.Options {
866873
public boolean rememberUpgrade;
867874
public String host;
868875
public String query;
869-
876+
public Map<String, Transport.Options> transportOptions;
870877

871878
private static Options fromURI(URI uri, Options opts) {
872879
if (opts == null) {

src/main/java/io/socket/engineio/client/transports/Polling.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,16 @@ public void run() {
183183
}
184184
};
185185

186-
Parser.encodePayload(packets, new Parser.EncodeCallback<byte[]>() {
186+
Parser.encodePayload(packets, new Parser.EncodeCallback() {
187187
@Override
188-
public void call(byte[] data) {
189-
self.doWrite(data, callbackfn);
188+
public void call(Object data) {
189+
if (data instanceof byte[]) {
190+
self.doWrite((byte[])data, callbackfn);
191+
} else if (data instanceof String) {
192+
self.doWrite((String)data, callbackfn);
193+
} else {
194+
logger.warning("Unexpected data: " + data);
195+
}
190196
}
191197
});
192198
}
@@ -220,5 +226,7 @@ protected String uri() {
220226

221227
abstract protected void doWrite(byte[] data, Runnable fn);
222228

229+
abstract protected void doWrite(String data, Runnable fn);
230+
223231
abstract protected void doPoll();
224232
}

src/main/java/io/socket/engineio/client/transports/PollingXHR.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.Map;
1010
import java.util.TreeMap;
11+
import java.util.logging.Level;
1112
import java.util.logging.Logger;
1213

1314
import io.socket.emitter.Emitter;
@@ -26,6 +27,8 @@ public class PollingXHR extends Polling {
2627

2728
private static final Logger logger = Logger.getLogger(PollingXHR.class.getName());
2829

30+
private static boolean LOGGABLE_FINE = logger.isLoggable(Level.FINE);
31+
2932
public PollingXHR(Transport.Options opts) {
3033
super(opts);
3134
}
@@ -66,6 +69,15 @@ public void run() {
6669

6770
@Override
6871
protected void doWrite(byte[] data, final Runnable fn) {
72+
this.doWrite((Object) data, fn);
73+
}
74+
75+
@Override
76+
protected void doWrite(String data, final Runnable fn) {
77+
this.doWrite((Object) data, fn);
78+
}
79+
80+
private void doWrite(Object data, final Runnable fn) {
6981
Request.Options opts = new Request.Options();
7082
opts.method = "POST";
7183
opts.data = data;
@@ -140,13 +152,17 @@ public static class Request extends Emitter {
140152
public static final String EVENT_ERROR = "error";
141153
public static final String EVENT_REQUEST_HEADERS = "requestHeaders";
142154
public static final String EVENT_RESPONSE_HEADERS = "responseHeaders";
155+
143156
private static final String BINARY_CONTENT_TYPE = "application/octet-stream";
157+
private static final String TEXT_CONTENT_TYPE = "text/plain;charset=UTF-8";
158+
159+
private static final MediaType BINARY_MEDIA_TYPE = MediaType.parse(BINARY_CONTENT_TYPE);
160+
private static final MediaType TEXT_MEDIA_TYPE = MediaType.parse(TEXT_CONTENT_TYPE);
144161

145162
private String method;
146163
private String uri;
147164

148-
// data is always a binary
149-
private byte[] data;
165+
private Object data;
150166

151167
private Call.Factory callFactory;
152168
private Response response;
@@ -161,28 +177,42 @@ public Request(Options opts) {
161177

162178
public void create() {
163179
final Request self = this;
164-
logger.fine(String.format("xhr open %s: %s", this.method, this.uri));
180+
if (LOGGABLE_FINE) logger.fine(String.format("xhr open %s: %s", this.method, this.uri));
165181
Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
166182

167183
if ("POST".equals(this.method)) {
168-
headers.put("Content-type", new LinkedList<String>(Collections.singletonList(BINARY_CONTENT_TYPE)));
184+
if (this.data instanceof byte[]) {
185+
headers.put("Content-type", new LinkedList<String>(Collections.singletonList(BINARY_CONTENT_TYPE)));
186+
} else {
187+
headers.put("Content-type", new LinkedList<String>(Collections.singletonList(TEXT_CONTENT_TYPE)));
188+
}
169189
}
170190

171191
headers.put("Accept", new LinkedList<String>(Collections.singletonList("*/*")));
172192

173-
self.onRequestHeaders(headers);
193+
this.onRequestHeaders(headers);
194+
195+
if (LOGGABLE_FINE) {
196+
logger.fine(String.format("sending xhr with url %s | data %s", this.uri,
197+
this.data instanceof byte[] ? Arrays.toString((byte[]) this.data) : this.data));
198+
}
174199

175-
logger.fine(String.format("sending xhr with url %s | data %s", this.uri, Arrays.toString(this.data)));
176200
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();
177201
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
178202
for (String v : header.getValue()){
179203
requestBuilder.addHeader(header.getKey(), v);
180204
}
181205
}
206+
RequestBody body = null;
207+
if (this.data instanceof byte[]) {
208+
body = RequestBody.create(BINARY_MEDIA_TYPE, (byte[])this.data);
209+
} else if (this.data instanceof String) {
210+
body = RequestBody.create(TEXT_MEDIA_TYPE, (String)this.data);
211+
}
212+
182213
okhttp3.Request request = requestBuilder
183214
.url(HttpUrl.parse(self.uri))
184-
.method(self.method, (self.data != null) ?
185-
RequestBody.create(MediaType.parse(BINARY_CONTENT_TYPE), self.data) : null)
215+
.method(self.method, body)
186216
.build();
187217

188218
requestCall = callFactory.newCall(request);
@@ -255,7 +285,7 @@ public static class Options {
255285

256286
public String uri;
257287
public String method;
258-
public byte[] data;
288+
public Object data;
259289
public Call.Factory callFactory;
260290
}
261291
}

0 commit comments

Comments
 (0)