Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
Added README
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegorisa committed Dec 25, 2017
1 parent 7426b92 commit e134df5
Showing 1 changed file with 192 additions and 0 deletions.
192 changes: 192 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<h1 align="center">ClusterWS Java Client</h1>
<h6 align="center">WebSocket & Node JS Cluster</h6>

<p align="center">
<img alt="Node.js" src="http://u.cubeupload.com/goriunovd/clusterWS.png" width="560"/>
</p>

<p align="center">
<a title="JitPack Version" href="https://jitpack.io/#ClusterWS/ClusterWS-Client-Java"><img src="https://jitpack.io/v/ClusterWS/ClusterWS-Client-Java.svg"></a>
</p>

## Overview
This is official Java client for [ClusterWS](https://github.com/ClusterWS/ClusterWS).

[ClusterWS](https://github.com/ClusterWS/ClusterWS) - is a minimal **Node JS http & real-time** framework which allows to scale WebSocket ([uWS](https://github.com/uNetworking/uWebSockets) - one of the fastest WebSocket libraries) between **Workers** in [Node JS Cluster](https://nodejs.org/api/cluster.html) and utilize all available CPU.

**This library requires [ClusterWS](https://github.com/ClusterWS/ClusterWS) on the server**

## Installation
### Maven

```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependency>
<groupId>com.github.ClusterWS</groupId>
<artifactId>ClusterWS-Client-Java</artifactId>
<version>1.3.0</version>
</dependency>
```


### Gradle

```Gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.ClusterWS:ClusterWS-Client-Java:1.3.0'
}
```


## Socket
### 1. Connecting
You can connect to the server with the following code:
```java
/**
First parameter: string - url of the server without http or https. (must be provided)
Second parameter: string - port of the server. (must be provided)
*/
ClusterWS clusterWS = new ClusterWS("localhost","80");
clusterWS.connect();

```

in case if you are using this library on Android you have to connect on separate thread or use method connectAsynchronously:
```java
ClusterWS clusterWS = new ClusterWS("localhost","80");
clusterWS.connectAsynchronously();
```

If you want to set auto reconnection use setReconnection method
```java
/**
First parameter: boolean - allow to auto-reconnect to the server on lost connection. (default false)
Second parameter: int - how long min time wait. (default 1000) in ms
Third parameter: int - how long max time wait. (default 5000) in ms
Fourth parameter: int - how many times to try, 0 means without limit. (default 0)
*/
clusterWS.setReconnection(true, 1000, 2000, 3);
```

*Auto reconnect count random time between Max and Min interval value this will reduce amount of users which are connection at the same time on reconnection and reduce server load on restart of the server*

### 2. Listen on events
To listen on events from the server you should use `on` method witch is provided by `clusterWS`
```java
/**
event name: string - can be any string you wish
data: any - is what you send from the client
*/
clusterWS.on("event name", new EmitterListener() {
@Override
public void onDataReceived(Object data) {
// in here you can write any logic
}
});
```

*Also `clusterWS` gets **Reserved Events** such as `'onConnected'`, `'onDisconnected'` and `'onConnectError'`*
```java
clusterWS.setClusterWSListener(new ClusterWSListener() {
@Override
public void onConnected(final ClusterWS socket) {
// in here you can write any logic
}

@Override
public void onConnectError(ClusterWS socket, WebSocketException exception) {
// in here you can write any logic
}

@Override
public void onDisconnected(ClusterWS socket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {
// in here you can write any logic
}
});
```

### 3. Send events
To send events to the server use `send` method witch is provided by `clusterWS`
```java
/**
event name: string - can be any string you wish (client must listen on this event name)
data: any - is what you want to send to the client
*/
clusterWS.send("event name",data);

```

*Avoid emitting **Reserved Events** such as `'connect'`, `'connection'`, `'disconnect'` and `'error'`. Also avoid emitting event and events with `'#'` at the start.*

## Pub/Sub
You can `subscribe`, `watch`, `unsubscribe`, `publish` and `getChannelByName` to/from the channels
```java
/**
channel name: string - can be any string you wish
*/
Channel channel = clusterWS.subscribe("channel name");

/**
channelName: string - name of the channel to which data was sent to
data: any - is what you get when you or some one else publish to the channel
*/
channel.watch(new Channel.ChannelListener() {
@Override
public void onDataReceived(String channelName, Object data) {
// in here you can write any logic
}
})

/**
data: any - is what you want to publish to the channel (everyone who is subscribed will get it)
*/
channel.publish(data);

/**
This method is used to unsubscribe from the channel
*/
channel.unsubscribe();

/**
Also you can chain everything in one expression
*/
Channel channel = clusterWS.subscribe("channel name").watch(new Channel.ChannelListener() {
@Override
public void onDataReceived(String channelName, Object data) {
// in here you can write any logic
}
}).publish(data);

/**
You can get channel by channel name only if you were subscribed before
*/

clusterWS.getChannelByName("channel name");

/**
You can get all channels as an array by using getChannels method
*/
clusterWS.getChannels();
```

**To make sure that user is connected to the server before subscribing, do it on `onConnected` event or on any other events which you emit from the server, otherwise subscription may not work properly**

## See also
* [Medium ClusterWS](https://medium.com/clusterws)


#### *Docs are still under development. If you have found any errors please submit pull request or leave issue*

## Good luck and Have fun :balloon: :running:

0 comments on commit e134df5

Please sign in to comment.