vishal-kedia / engine.io-client.java

Engine.IO Client Library for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 to pom.xml.

Maven

Add the following dependency to your pom.xml.

<dependencies>
  <dependency>
    <groupId>com.github.nkzawa</groupId>
    <artifactId>engine.io-client</artifactId>
    <version>0.4.1</version>
  </dependency>
</dependencies>

Gradle

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

compile 'com.github.nkzawa:engine.io-client:0.4.1'

Usage

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

socket = new Socket("ws://localhost");
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    socket.send("hi");
    socket.close();
  }
});
socket.open();

You can listen events as follows:

socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    String data = (String)args[0];
  }
}).on(Socket.EVENT_ERROR, new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    Exception err = (Exception)args[0];
  }
});

How to set options:

opts = new Socket.Options();
opts.transports = new String[] {WebSocket.NAME};

socket = new Socket(opts);

Sending and receiving binary data:

socket = new Socket();
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    // send binary data
    byte[] data = new byte[42];
    socket.send(data);
  }
}).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    // receive binary data
    byte[] data = (byte[])args[0];
  }
});

Use custom SSL settings:

// default SSLContext for all sockets
Socket.setDefaultSSLContext(mySSLContext);

// set as an option
opts = new Socket.Options();
opts.sslContext = mySSLContext;
socket = new Socket(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, new Emitter.listener() {
  @Override
  public void call(Object... args) {
    // Called on a new transport created.
    Transport transport = (Transport)args[0];

    transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
      @Override
      public void call(Object... args) {
        @SuppressWarnings("unchecked")
        Map<String, String> headers = (Map<String, String>)args[0];
        // send cookies to server.
        headers.put("Cookie", "foo=1;");
      }
    }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
      @Override
      public void call(Object... args) {
        @SuppressWarnings("unchecked")
        Map<String, String> headers = (Map<String, String>)args[0];
        // get cookies from server.
        String cookie = headers.get("Set-Cookie");
      }
    });
  }
});

See the Javadoc for more details.

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

License

MIT

About

Engine.IO Client Library for Java

License:Other


Languages

Language:Java 99.1%Language:JavaScript 0.9%