Giter Site home page Giter Site logo

junbong / socket.io-client-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from socketio/socket.io-client-java

0.0 2.0 0.0 1.14 MB

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.

Home Page: http://socketio.github.io/socket.io-client-java

License: Other

Java 97.89% JavaScript 2.11%

socket.io-client-java's Introduction

Socket.IO-client Java

Build Status

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

See also:

Installation

The latest artifact is available on Maven Central. You'll also need dependencies to install.

WARNING: The package name was changed to "io.socket" on v0.6.1 or later. Please make sure to update your dependency settings.

Maven

Add the following dependency to your pom.xml.

<dependencies>
  <dependency>
    <groupId>io.socket</groupId>
    <artifactId>socket.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:socket.io-client:0.7.0') {
  // excluding org.json which is provided by Android
  exclude group: 'org.json', module: 'json'
}

Usage

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 supply query parameters with the query option. NB: if you don't want to reuse a cached socket instance when the query parameter changes, you should use the forceNew option, the use case might be if your app allows for a user to logout, and a new user to login again:

IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.query = "auth_token=" + authToken;
Socket 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) {}
});

And vice versa:

// ack from client to server
socket.on("foo", new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    Ack ack = (Ack) args[args.length - 1];
    ack.call();
  }
});

SSL (HTTPS, WSS) settings:

// default settings for all sockets
IO.setDefaultSSLContext(mySSLContext);
IO.setDefaultHostnameVerifier(myHostnameVerifier);

// set as an option
opts = new IO.Options();
opts.sslContext = mySSLContext;
opts.hostnameVerifier = myHostnameVerifier;
socket = IO.socket("https://localhost", opts);

See the Javadoc for more details.

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

Transports and HTTP Headers

You can access transports and their HTTP headers as follows.

// Called upon transport creation.
socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.listener() {
  @Override
  public void call(Object... args) {
    Transport transport = (Transport)args[0];

    transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
      @Override
      public void call(Object... args) {
        @SuppressWarnings("unchecked")
        Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
        // modify request headers
        headers.put("Cookie", Arrays.asList("foo=1;"));
      }
    });

    transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
      @Override
      public void call(Object... args) {
        @SuppressWarnings("unchecked")
        Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
        // access response headers
        String cookie = headers.get("Set-Cookie").get(0);
      }
    });
  }
});

Features

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

License

MIT

socket.io-client-java's People

Contributors

alfioemanuelefresta avatar civanyp avatar hellpf avatar kylestev avatar naartjie avatar niqo01 avatar nkzawa avatar pamepros avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.