use org.json instead of Gson
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -42,9 +42,9 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>org.json</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>2.2.4</version>
|
<version>20140107</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.java-websocket</groupId>
|
<groupId>org.java-websocket</groupId>
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.github.nkzawa.engineio.client;
|
||||||
|
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class HandshakeData {
|
||||||
|
|
||||||
|
public String sid;
|
||||||
|
public String[] upgrades;
|
||||||
|
public long pingInterval;
|
||||||
|
public long pingTimeout;
|
||||||
|
|
||||||
|
/*package*/ HandshakeData(JSONObject data) {
|
||||||
|
JSONArray upgrades = data.getJSONArray("upgrades");
|
||||||
|
int length = upgrades.length();
|
||||||
|
String[] _upgrades = new String[length];
|
||||||
|
for (int i = 0; i < length; i ++) {
|
||||||
|
_upgrades[i] = upgrades.getString(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sid = data.getString("sid");
|
||||||
|
this.upgrades = _upgrades;
|
||||||
|
this.pingInterval = data.getLong("pingInterval");
|
||||||
|
this.pingTimeout = data.getLong("pingTimeout");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,11 +4,10 @@ import com.github.nkzawa.emitter.Emitter;
|
|||||||
import com.github.nkzawa.engineio.client.transports.Polling;
|
import com.github.nkzawa.engineio.client.transports.Polling;
|
||||||
import com.github.nkzawa.engineio.client.transports.PollingXHR;
|
import com.github.nkzawa.engineio.client.transports.PollingXHR;
|
||||||
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
||||||
import com.github.nkzawa.engineio.parser.HandshakeData;
|
|
||||||
import com.github.nkzawa.engineio.parser.Packet;
|
import com.github.nkzawa.engineio.parser.Packet;
|
||||||
import com.github.nkzawa.engineio.parser.Parser;
|
import com.github.nkzawa.engineio.parser.Parser;
|
||||||
import com.github.nkzawa.thread.EventThread;
|
import com.github.nkzawa.thread.EventThread;
|
||||||
import com.google.gson.Gson;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@@ -29,8 +28,6 @@ public abstract class Socket extends Emitter {
|
|||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(Socket.class.getName());
|
private static final Logger logger = Logger.getLogger(Socket.class.getName());
|
||||||
|
|
||||||
private static final Gson gson = new Gson();
|
|
||||||
|
|
||||||
private enum ReadyState {
|
private enum ReadyState {
|
||||||
OPENING, OPEN, CLOSING, CLOSED;
|
OPENING, OPEN, CLOSING, CLOSED;
|
||||||
|
|
||||||
@@ -398,7 +395,7 @@ public abstract class Socket extends Emitter {
|
|||||||
this.emit(EVENT_HEARTBEAT);
|
this.emit(EVENT_HEARTBEAT);
|
||||||
|
|
||||||
if (Packet.OPEN.equals(packet.type)) {
|
if (Packet.OPEN.equals(packet.type)) {
|
||||||
this.onHandshake(gson.fromJson((String)packet.data, HandshakeData.class));
|
this.onHandshake(new HandshakeData(new JSONObject((String)packet.data)));
|
||||||
} else if (Packet.PONG.equals(packet.type)) {
|
} else if (Packet.PONG.equals(packet.type)) {
|
||||||
this.setPing();
|
this.setPing();
|
||||||
} else if (Packet.ERROR.equals(packet.type)) {
|
} else if (Packet.ERROR.equals(packet.type)) {
|
||||||
@@ -424,7 +421,7 @@ public abstract class Socket extends Emitter {
|
|||||||
this.emit(EVENT_HANDSHAKE, data);
|
this.emit(EVENT_HANDSHAKE, data);
|
||||||
this.id = data.sid;
|
this.id = data.sid;
|
||||||
this.transport.query.put("sid", data.sid);
|
this.transport.query.put("sid", data.sid);
|
||||||
this.upgrades = this.filterUpgrades(data.upgrades);
|
this.upgrades = this.filterUpgrades(Arrays.asList(data.upgrades));
|
||||||
this.pingInterval = data.pingInterval;
|
this.pingInterval = data.pingInterval;
|
||||||
this.pingTimeout = data.pingTimeout;
|
this.pingTimeout = data.pingTimeout;
|
||||||
this.onOpen();
|
this.onOpen();
|
||||||
@@ -690,7 +687,7 @@ public abstract class Socket extends Emitter {
|
|||||||
return filteredUpgrades;
|
return filteredUpgrades;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onmessage(byte[] data) {};
|
public void onmessage(byte[] data) {}
|
||||||
|
|
||||||
public abstract void onopen();
|
public abstract void onopen();
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package com.github.nkzawa.engineio.parser;
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class HandshakeData {
|
|
||||||
|
|
||||||
public String sid;
|
|
||||||
public List<String> upgrades;
|
|
||||||
public long pingInterval;
|
|
||||||
public long pingTimeout;
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,6 @@ package com.github.nkzawa.engineio.client;
|
|||||||
import com.github.nkzawa.emitter.Emitter;
|
import com.github.nkzawa.emitter.Emitter;
|
||||||
import com.github.nkzawa.engineio.client.transports.Polling;
|
import com.github.nkzawa.engineio.client.transports.Polling;
|
||||||
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
||||||
import com.github.nkzawa.engineio.parser.HandshakeData;
|
|
||||||
import com.github.nkzawa.thread.EventThread;
|
import com.github.nkzawa.thread.EventThread;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -147,7 +146,7 @@ public class ServerConnectionTest {
|
|||||||
|
|
||||||
@Test(timeout = TIMEOUT)
|
@Test(timeout = TIMEOUT)
|
||||||
public void handshake() throws URISyntaxException, InterruptedException {
|
public void handshake() throws URISyntaxException, InterruptedException {
|
||||||
final BlockingQueue<Object[]> events = new LinkedBlockingQueue<Object[]>();
|
final Semaphore semaphore = new Semaphore(0);
|
||||||
|
|
||||||
socket = new Socket("ws://localhost:" + PORT) {
|
socket = new Socket("ws://localhost:" + PORT) {
|
||||||
@Override
|
@Override
|
||||||
@@ -162,23 +161,22 @@ public class ServerConnectionTest {
|
|||||||
socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() {
|
socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void call(Object... args) {
|
public void call(Object... args) {
|
||||||
events.offer(args);
|
assertThat(args.length, is(1));
|
||||||
|
assertThat(args[0], is(instanceOf(HandshakeData.class)));
|
||||||
|
|
||||||
|
HandshakeData data = (HandshakeData)args[0];
|
||||||
|
assertThat(data.sid, is(notNullValue()));
|
||||||
|
assertThat(data.upgrades, is(notNullValue()));
|
||||||
|
assertThat(data.upgrades, is(not(emptyArray())));
|
||||||
|
assertThat(data.pingTimeout, is(greaterThan((long)0)));
|
||||||
|
assertThat(data.pingInterval, is(greaterThan((long) 0)));
|
||||||
|
|
||||||
|
socket.close();
|
||||||
|
semaphore.release();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.open();
|
socket.open();
|
||||||
|
semaphore.acquire();
|
||||||
Object[] args = events.take();
|
|
||||||
assertThat(args.length, is(1));
|
|
||||||
assertThat(args[0], is(instanceOf(HandshakeData.class)));
|
|
||||||
|
|
||||||
HandshakeData data = (HandshakeData)args[0];
|
|
||||||
assertThat(data.sid, is(notNullValue()));
|
|
||||||
assertThat(data.upgrades, is(notNullValue()));
|
|
||||||
assertThat(data.upgrades, is(not(empty())));
|
|
||||||
assertThat(data.pingTimeout, is(greaterThan((long)0)));
|
|
||||||
assertThat(data.pingInterval, is(greaterThan((long) 0)));
|
|
||||||
|
|
||||||
socket.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(timeout = TIMEOUT)
|
@Test(timeout = TIMEOUT)
|
||||||
@@ -325,20 +323,13 @@ public class ServerConnectionTest {
|
|||||||
|
|
||||||
final Socket socket = new Socket(opts) {
|
final Socket socket = new Socket(opts) {
|
||||||
@Override
|
@Override
|
||||||
public void onopen() {
|
public void onopen() {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onmessage(String data) {
|
public void onmessage(String data) {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onclose() {
|
public void onclose() {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onerror(Exception err) {
|
public void onerror(Exception err) {}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
|
socket.on(Socket.EVENT_UPGRADE, new Emitter.Listener() {
|
||||||
@@ -353,20 +344,13 @@ public class ServerConnectionTest {
|
|||||||
|
|
||||||
final Socket socket2 = new Socket(opts) {
|
final Socket socket2 = new Socket(opts) {
|
||||||
@Override
|
@Override
|
||||||
public void onopen() {
|
public void onopen() {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onmessage(String data) {
|
public void onmessage(String data) {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onclose() {
|
public void onclose() {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onerror(Exception err) {
|
public void onerror(Exception err) {}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
socket2.open();
|
socket2.open();
|
||||||
assertThat(socket2.transport.name, is(WebSocket.NAME));
|
assertThat(socket2.transport.name, is(WebSocket.NAME));
|
||||||
|
|||||||
Reference in New Issue
Block a user