Skip to content

Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0

License

Notifications You must be signed in to change notification settings

hellpf/socket.io-client.java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Repository files navigation

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, newEmitter.Listener(){@Overridepublicvoidcall(Object... args){socket.emit("foo", "hi"); socket.disconnect()} }).on("event", newEmitter.Listener(){@Overridepublicvoidcall(Object... args){} }).on(Socket.EVENT_DISCONNECT, newEmitter.Listener(){@Overridepublicvoidcall(Object... args){} }); socket.connect();

This Library uses org.json to parse and compose JSON strings:

// Sending an objectJSONObjectobj = newJSONObject(); obj.put("hello", "server"); obj.put("binary", newbyte[42]); socket.emit("foo", obj); // Receiving an objectsocket.on("foo", newEmitter.Listener(){@Overridepublicvoidcall(Object... args){JSONObjectobj = (JSONObject)args[0]} });

Options are supplied as follows:

IO.Optionsopts = newIO.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.Optionsopts = newIO.Options(); opts.forceNew = true; opts.query = "auth_token=" + authToken; Socketsocket = IO.socket("http://localhost", opts);

You can get a callback with Ack when the server received a message:

socket.emit("foo", "woot", newAck(){@Overridepublicvoidcall(Object... args){} });

And vice versa:

// ack from client to serversocket.on("foo", newEmitter.Listener(){@Overridepublicvoidcall(Object... args){Ackack = (Ack) args[args.length - 1]; ack.call()} });

SSL (HTTPS, WSS) settings:

// default settings for all socketsIO.setDefaultSSLContext(mySSLContext); IO.setDefaultHostnameVerifier(myHostnameVerifier); // set as an optionopts = newIO.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, newEmitter.listener(){@Overridepublicvoidcall(Object... args){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]; // modify request headersheaders.put("Cookie", Arrays.asList("foo=1;"))} }); transport.on(Transport.EVENT_RESPONSE_HEADERS, newEmitter.Listener(){@Overridepublicvoidcall(Object... args){@SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>)args[0]; // access response headersStringcookie = 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

About

Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java97.9%
  • JavaScript2.1%