Skip to content

Latest commit

History

228 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Engine.IO-client Java

Build Status

This is the Engine.IO Client Library for Java, which is simply ported from the JavaScript client.

See also: Socket.IO-client Java

Installation

The latest artifact is available on Maven Central. To install manually, please refer dependencies.

Maven

Add the following dependency to your pom.xml.

<dependencies>
<dependency>
<groupId>io.socket</groupId>
<artifactId>engine.io-client</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>

Gradle

Add it as a gradle dependency for Android Studio, in build.gradle:

compile ('io.socket:engine.io-client:0.7.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}

Usage

Engine.IO-client Java has the similar api with the JS client. You can use Socket to connect:

socket = newSocket("ws://localhost");
socket.on(Socket.EVENT_OPEN, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
socket.send("hi");
socket.close();
}
});
socket.open();

You can listen events as follows:

socket.on(Socket.EVENT_MESSAGE, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
Stringdata = (String)args[0];
}
}).on(Socket.EVENT_ERROR, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
Exceptionerr = (Exception)args[0];
}
});

How to set options:

opts = newSocket.Options();
opts.transports = newString[] {WebSocket.NAME};
socket = newSocket(opts);

Sending and receiving binary data:

socket = newSocket();
socket.on(Socket.EVENT_OPEN, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
// send binary databyte[] data = newbyte[42];
socket.send(data);
}
}).on(Socket.EVENT_MESSAGE, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
// receive binary databyte[] data = (byte[])args[0];
}
});

Use custom SSL settings:

// default SSLContext for all socketsSocket.setDefaultSSLContext(mySSLContext);
Socket.setDefaultHostnameVerifier(myHostnameVerifier);
// set as an optionopts = newSocket.Options();
opts.sslContext = mySSLContext;
opts.hostnameVerifier = myHostnameVerifier;
socket = newSocket(opts);

Features

This library supports all of the features the JS client does, including events, options and upgrading transport. Android is fully supported.

Extra features only for Java client

Some features are added for simulating browser behavior like handling cookies.

socket.on(Socket.EVENT_TRANSPORT, newEmitter.listener() {
@Overridepublicvoidcall(Object... args) {
// Called on a new transport created.Transporttransport = (Transport)args[0];
transport.on(Transport.EVENT_REQUEST_HEADERS, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
// send cookie value to server.headers.put("Cookie", Arrays.asList("foo=1;"));
}
}).on(Transport.EVENT_RESPONSE_HEADERS, newEmitter.Listener() {
@Overridepublicvoidcall(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
// receive cookie value from server.Stringcookie = headers.get("Set-Cookie").get(0);
}
});
}
});

See the Javadoc for more details.

http://socketio.github.io/engine.io-client-java/apidocs/

License

MIT

About

Engine.IO Client Library for Java

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages