92 lines
2.1 KiB
Markdown
92 lines
2.1 KiB
Markdown
# Socket.IO-client.java
|
|
[](https://travis-ci.org/nkzawa/socket.io-client.java)
|
|
|
|
This is the Socket.IO v1.0 Client Library for Java, which is simply ported from the [JavaScript client](https://github.com/LearnBoost/socket.io-client).
|
|
|
|
See also: [Engine.IO-client.java](https://github.com/nkzawa/engine.io-client.java)
|
|
|
|
## Installation
|
|
The latest artifact is available on Maven Central. Add the following dependency to your `pom.xml`.
|
|
|
|
```xml
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>com.github.nkzawa</groupId>
|
|
<artifactId>socket.io-client</artifactId>
|
|
<version>0.1.0</version>
|
|
</dependency>
|
|
</dependencies>
|
|
```
|
|
|
|
## Usage
|
|
Socket.IO-client.java has almost the same api and features with the original JS client. You use `IO#socket` to initialize `Socket`:
|
|
|
|
```java
|
|
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](http://www.json.org/java/) to parse and compose JSON strings:
|
|
|
|
```java
|
|
// Sending an object
|
|
JSONObject obj = new JSONObject();
|
|
obj.put("hello", "server");
|
|
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:
|
|
|
|
```java
|
|
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:
|
|
|
|
```java
|
|
socket.emit("foo", "woot", new Ack() {
|
|
@Override
|
|
public void call(Object... args) {}
|
|
});
|
|
```
|
|
|
|
See the Javadoc for more details.
|
|
|
|
http://nkzawa.github.io/socket.io-client.java/apidocs/
|
|
|
|
## License
|
|
|
|
MIT
|
|
|