add comments
This commit is contained in:
9
pom.xml
9
pom.xml
@@ -41,15 +41,6 @@
|
|||||||
<target>1.6</target>
|
<target>1.6</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
|
||||||
<version>2.9</version>
|
|
||||||
<configuration>
|
|
||||||
<show>protected</show>
|
|
||||||
<windowtitle>Engine.IO Client API</windowtitle>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The event emitter which is ported from the JavaScript module.
|
||||||
|
*
|
||||||
|
* @see <a href="https://github.com/component/emitter">https://github.com/component/emitter</a>
|
||||||
|
*/
|
||||||
public class Emitter {
|
public class Emitter {
|
||||||
|
|
||||||
private ConcurrentMap<String, ConcurrentLinkedQueue<Listener>> callbacks
|
private ConcurrentMap<String, ConcurrentLinkedQueue<Listener>> callbacks
|
||||||
@@ -15,6 +21,12 @@ public class Emitter {
|
|||||||
private ConcurrentMap<Listener, Listener> onceCallbacks = new ConcurrentHashMap<Listener, Listener>();
|
private ConcurrentMap<Listener, Listener> onceCallbacks = new ConcurrentHashMap<Listener, Listener>();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listens on the event.
|
||||||
|
* @param event event name.
|
||||||
|
* @param fn
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Emitter on(String event, Listener fn) {
|
public Emitter on(String event, Listener fn) {
|
||||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||||
if (callbacks == null) {
|
if (callbacks == null) {
|
||||||
@@ -28,6 +40,13 @@ public class Emitter {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a one time listener for the event.
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @param fn
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Emitter once(final String event, final Listener fn) {
|
public Emitter once(final String event, final Listener fn) {
|
||||||
Listener on = new Listener() {
|
Listener on = new Listener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -42,12 +61,23 @@ public class Emitter {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all registered listeners.
|
||||||
|
*
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Emitter off() {
|
public Emitter off() {
|
||||||
this.callbacks.clear();
|
this.callbacks.clear();
|
||||||
this.onceCallbacks.clear();
|
this.onceCallbacks.clear();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all listeners of the specified event.
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Emitter off(String event) {
|
public Emitter off(String event) {
|
||||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.remove(event);
|
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.remove(event);
|
||||||
if (callbacks != null) {
|
if (callbacks != null) {
|
||||||
@@ -58,6 +88,13 @@ public class Emitter {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the listener.
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @param fn
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Emitter off(String event, Listener fn) {
|
public Emitter off(String event, Listener fn) {
|
||||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||||
if (callbacks != null) {
|
if (callbacks != null) {
|
||||||
@@ -67,6 +104,13 @@ public class Emitter {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes each of listeners with the given args.
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @param args
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Emitter emit(String event, Object... args) {
|
public Emitter emit(String event, Object... args) {
|
||||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||||
if (callbacks != null) {
|
if (callbacks != null) {
|
||||||
@@ -78,12 +122,24 @@ public class Emitter {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of listeners for the specified event.
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public List<Listener> listeners(String event) {
|
public List<Listener> listeners(String event) {
|
||||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||||
return callbacks != null ?
|
return callbacks != null ?
|
||||||
new ArrayList<Listener>(callbacks) : new ArrayList<Listener>();
|
new ArrayList<Listener>(callbacks) : new ArrayList<Listener>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this emitter has listeners for the specified event.
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public boolean hasListeners(String event) {
|
public boolean hasListeners(String event) {
|
||||||
return !this.listeners(event).isEmpty();
|
return !this.listeners(event).isEmpty();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,12 @@ import java.util.*;
|
|||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The socket class for Event.IO Client.
|
||||||
|
*
|
||||||
|
* @see <a href="https://github.com/LearnBoost/engine.io-client">https://github.com/LearnBoost/engine.io-client</a>
|
||||||
|
*/
|
||||||
public abstract class Socket extends Emitter {
|
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());
|
||||||
@@ -32,8 +38,36 @@ public abstract class Socket extends Emitter {
|
|||||||
put(CLOSED, "closed");
|
put(CLOSED, "closed");
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called on successful connection.
|
||||||
|
*/
|
||||||
public static final String EVENT_OPEN = "open";
|
public static final String EVENT_OPEN = "open";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called on disconnection.
|
||||||
|
*/
|
||||||
public static final String EVENT_CLOSE = "close";
|
public static final String EVENT_CLOSE = "close";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when data is received from the server.
|
||||||
|
*/
|
||||||
|
public static final String EVENT_MESSAGE = "message";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an error occurs.
|
||||||
|
*/
|
||||||
|
public static final String EVENT_ERROR = "error";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called on completing a buffer flush.
|
||||||
|
*/
|
||||||
|
public static final String EVENT_FLUSH = "flush";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called after `drain` event of transport if writeBuffer is empty.
|
||||||
|
*/
|
||||||
|
public static final String EVENT_DRAIN = "drain";
|
||||||
|
|
||||||
public static final String EVENT_HANDSHAKE = "handshake";
|
public static final String EVENT_HANDSHAKE = "handshake";
|
||||||
public static final String EVENT_UPGRADING = "upgrading";
|
public static final String EVENT_UPGRADING = "upgrading";
|
||||||
public static final String EVENT_UPGRADE = "upgrade";
|
public static final String EVENT_UPGRADE = "upgrade";
|
||||||
@@ -41,17 +75,20 @@ public abstract class Socket extends Emitter {
|
|||||||
public static final String EVENT_PACKET_CREATE = "packetCreate";
|
public static final String EVENT_PACKET_CREATE = "packetCreate";
|
||||||
public static final String EVENT_HEARTBEAT = "heartbeat";
|
public static final String EVENT_HEARTBEAT = "heartbeat";
|
||||||
public static final String EVENT_DATA = "data";
|
public static final String EVENT_DATA = "data";
|
||||||
public static final String EVENT_MESSAGE = "message";
|
|
||||||
public static final String EVENT_ERROR = "error";
|
|
||||||
public static final String EVENT_DRAIN = "drain";
|
|
||||||
public static final String EVENT_FLUSH = "flush";
|
|
||||||
|
|
||||||
private static final Runnable noop = new Runnable() {
|
private static final Runnable noop = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {}
|
public void run() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of Socket instances.
|
||||||
|
*/
|
||||||
public static final Sockets sockets = new Sockets();
|
public static final Sockets sockets = new Sockets();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The protocol version.
|
||||||
|
*/
|
||||||
public static final int protocol = Parser.protocol;
|
public static final int protocol = Parser.protocol;
|
||||||
|
|
||||||
private boolean secure;
|
private boolean secure;
|
||||||
@@ -80,6 +117,12 @@ public abstract class Socket extends Emitter {
|
|||||||
private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
|
private ScheduledExecutorService heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a socket.
|
||||||
|
*
|
||||||
|
* @param uri URI to connect.
|
||||||
|
* @throws URISyntaxException
|
||||||
|
*/
|
||||||
public Socket(String uri) throws URISyntaxException {
|
public Socket(String uri) throws URISyntaxException {
|
||||||
this(uri, null);
|
this(uri, null);
|
||||||
}
|
}
|
||||||
@@ -88,6 +131,13 @@ public abstract class Socket extends Emitter {
|
|||||||
this(uri, null);
|
this(uri, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a socket with options.
|
||||||
|
*
|
||||||
|
* @param uri URI to connect.
|
||||||
|
* @param opts options for socket
|
||||||
|
* @throws URISyntaxException
|
||||||
|
*/
|
||||||
public Socket(String uri, Options opts) throws URISyntaxException {
|
public Socket(String uri, Options opts) throws URISyntaxException {
|
||||||
this(new URI(uri), opts);
|
this(new URI(uri), opts);
|
||||||
}
|
}
|
||||||
@@ -122,6 +172,9 @@ public abstract class Socket extends Emitter {
|
|||||||
Socket.sockets.evs.emit(Sockets.EVENT_ADD, this);
|
Socket.sockets.evs.emit(Sockets.EVENT_ADD, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects the client.
|
||||||
|
*/
|
||||||
public void open() {
|
public void open() {
|
||||||
this.readyState = OPENING;
|
this.readyState = OPENING;
|
||||||
Transport transport = this.createTransport(this.transports.get(0));
|
Transport transport = this.createTransport(this.transports.get(0));
|
||||||
@@ -434,10 +487,21 @@ public abstract class Socket extends Emitter {
|
|||||||
this.send(msg, fn);
|
this.send(msg, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message.
|
||||||
|
*
|
||||||
|
* @param msg
|
||||||
|
*/
|
||||||
public void send(String msg) {
|
public void send(String msg) {
|
||||||
this.send(msg, null);
|
this.send(msg, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a message.
|
||||||
|
*
|
||||||
|
* @param msg
|
||||||
|
* @param fn callback to be called on drain
|
||||||
|
*/
|
||||||
public void send(String msg, Runnable fn) {
|
public void send(String msg, Runnable fn) {
|
||||||
this.sendPacket(Packet.MESSAGE, msg, fn);
|
this.sendPacket(Packet.MESSAGE, msg, fn);
|
||||||
}
|
}
|
||||||
@@ -459,6 +523,11 @@ public abstract class Socket extends Emitter {
|
|||||||
this.flush();
|
this.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnects the client.
|
||||||
|
*
|
||||||
|
* @return a reference to to this object.
|
||||||
|
*/
|
||||||
public Socket close() {
|
public Socket close() {
|
||||||
if (this.readyState == OPENING || this.readyState == OPEN) {
|
if (this.readyState == OPENING || this.readyState == OPEN) {
|
||||||
this.onClose("forced close");
|
this.onClose("forced close");
|
||||||
@@ -524,10 +593,18 @@ public abstract class Socket extends Emitter {
|
|||||||
|
|
||||||
public static class Options extends Transport.Options {
|
public static class Options extends Transport.Options {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of transport names.
|
||||||
|
*/
|
||||||
|
public String[] transports;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to upgrade the transport. Defaults to `true`.
|
||||||
|
*/
|
||||||
|
public boolean upgrade = true;
|
||||||
|
|
||||||
public String host;
|
public String host;
|
||||||
public String query;
|
public String query;
|
||||||
public String[] transports;
|
|
||||||
public boolean upgrade = true;
|
|
||||||
|
|
||||||
|
|
||||||
private static Options fromURI(URI uri, Options opts) {
|
private static Options fromURI(URI uri, Options opts) {
|
||||||
|
|||||||
Reference in New Issue
Block a user