Skip to content

socketio/engine.io-client-java

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

Table of content

Compatibility

Client versionEngine.IO serverSocket.IO server
0.9.x1.x1.x
1.x3.x2.x
2.x4.x3.x

Installation

The latest artifact is available on Maven Central.

Maven

Add the following dependency to your pom.xml.

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

Gradle

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

compile ('io.socket:engine.io-client:2.1.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:

OkHttpClientokHttpClient = newOkHttpClient.Builder() .hostnameVerifier(myHostnameVerifier) .sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager) .build(); // default SSLContext for all socketsSocket.setDefaultOkHttpWebSocketFactory(okHttpClient); Socket.setDefaultOkHttpCallFactory(okHttpClient); // set as an optionopts = newSocket.Options(); opts.callFactory = okHttpClient; opts.webSocketFactory = okHttpClient; 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.

License

MIT

About

Engine.IO Client Library for Java

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 18

Languages