This is the Socket.IO v1.x Client Library for Java, which is simply ported from the JavaScript client.
See also:
The latest artifact is available on Maven Central. To install manually, please refer dependencies to pom.xml.
Add the following dependency to your pom.xml
.
<dependencies>
<dependency>
<groupId>com.github.nkzawa</groupId>
<artifactId>socket.io-client</artifactId>
<version>0.4.1</version>
</dependency>
</dependencies>
Add it as a gradle dependency for Android Studio, in build.gradle
:
compile 'com.github.nkzawa:socket.io-client:0.4.1'
Socket.IO-client.java has almost the same api and features with the original JS client. You use IO#socket
to initialize Socket
:
socket = IO.socket("http://localhost");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("foo", "hi");
socket.disconnect();
}
}).on("event", new Emitter.Listener() {
@Override
public void call(Object... args) {}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {}
});
socket.connect();
This Library uses org.json to parse and compose JSON strings:
// Sending an object
JSONObject obj = new JSONObject();
obj.put("hello", "server");
obj.put("binary", new byte[42]);
socket.emit("foo", obj);
// Receiving an object
socket.on("foo", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject obj = (JSONObject)args[0];
}
});
Options are supplied as follows:
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
socket = IO.socket("http://localhost", opts);
You can get a callback with Ack
when the server received a message:
socket.emit("foo", "woot", new Ack() {
@Override
public void call(Object... args) {}
});
Use custom SSL settings:
// default SSLContext for all sockets
IO.setDefaultSSLContext(mySSLContext);
// set as an option
opts = new IO.Options();
opts.sslContext = mySSLContext;
socket = IO.socket("https://localhost", opts);
See the Javadoc for more details.
http://nkzawa.github.io/socket.io-client.java/apidocs/
This library supports all of the features the JS client does, including events, options and upgrading transport. Android is fully supported.
MIT