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

Commit

Permalink
Merge pull request #1 from ClusterWS/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
Yegorisa authored Dec 1, 2017
2 parents c788d9a + 67bda77 commit 121a723
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This is official Java client for [ClusterWS](https://github.com/ClusterWS/Cluste
<dependency>
<groupId>com.github.ClusterWS</groupId>
<artifactId>ClusterWS-Client-Java</artifactId>
<version>1.2.1</version>
<version>1.3.0</version>
</dependency>
```

Expand All @@ -45,7 +45,7 @@ allprojects {
}
dependencies {
compile 'com.github.ClusterWS:ClusterWS-Client-Java:1.2.1'
compile 'com.github.ClusterWS:ClusterWS-Client-Java:1.3.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version '1.2.1'
version '1.3.0'

apply plugin: 'java'
apply plugin: 'maven'
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/com/ClusterWS/ClusterWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.neovisionaries.ws.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -20,10 +21,11 @@ public class ClusterWS {
private Emitter mEmitter;
private ArrayList<Channel> mChannels;
private Message mMessageHandler;
private boolean mUseBinary;

//Ping
private Timer mPingTimer;
private int mLost;
private int mMissedPing;

private Reconnection mReconnectionHandler;

Expand All @@ -33,6 +35,7 @@ public ClusterWS(String url, String port) {
mChannels = new ArrayList<>();
mMessageHandler = new Message();
mReconnectionHandler = new Reconnection(null, null, null, null, this);
mUseBinary = false;
create();
}

Expand All @@ -44,9 +47,7 @@ private void create() {
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
mReconnectionHandler.onConnected();
if (mClusterWSListener != null) {
mClusterWSListener.onConnected(ClusterWS.this);
}
mPingTimer = new Timer();
}

@Override
Expand All @@ -63,16 +64,28 @@ public void onConnectError(WebSocket websocket, WebSocketException exception) th
@Override
public void onTextMessage(WebSocket websocket, String text) throws Exception {
if (text.equals("#0")) {
mLost = 0;
mMissedPing = 0;
send("#1", null, "ping");
} else {
mMessageHandler.messageDecode(ClusterWS.this, text);
}
}

@Override
public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
String message = new String(binary, StandardCharsets.UTF_8);
System.out.println(message);
if (message.equals("#0")) {
mMissedPing = 0;
send("#1", null, "ping");
} else {
mMessageHandler.messageDecode(ClusterWS.this, message);
}
}

@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
mLost = 0;
mMissedPing = 0;
if (mPingTimer != null) {
mPingTimer.cancel();
mPingTimer = new Timer();
Expand All @@ -98,6 +111,7 @@ public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame,
}
}


});

} catch (IOException e) {
Expand Down Expand Up @@ -141,7 +155,12 @@ public void on(String event, EmitterListener listener) {
}

public void send(String event, Object data) {
mWebSocket.sendText(mMessageHandler.messageEncode(event, data, "emit"));
System.out.println(mUseBinary);
if (mUseBinary){
mWebSocket.sendBinary(mMessageHandler.messageEncode(event, data, "emit").getBytes());
} else {
mWebSocket.sendText(mMessageHandler.messageEncode(event, data, "emit"));
}
}

public Channel subscribe(String channelName) {
Expand Down Expand Up @@ -194,15 +213,23 @@ Timer getPingTimer() {
return mPingTimer;
}

int getLost() {
return mLost;
int getMissedPing() {
return mMissedPing;
}

void incrementLost() {
mLost++;
mMissedPing++;
}

boolean isConnectedAsynchronously() {
return mIsConnectedAsynchronously;
}

void setUseBinary(boolean useBinary) {
mUseBinary = useBinary;
}

public ClusterWSListener getClusterWSListener() {
return mClusterWSListener;
}
}
24 changes: 14 additions & 10 deletions src/main/java/com/ClusterWS/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ String messageEncode(String event, Object data, String type) {
}
}

void messageDecode(final ClusterWS socket, String message){
System.out.println("Message is " + message);
void messageDecode(final ClusterWS socket, String message) {
JSONArray jsonArray = new JSONObject(message).getJSONArray("#");
switch (jsonArray.getString(0)){
switch (jsonArray.getString(0)) {
case "p":
ArrayList<Channel> channels = socket.getChannels();
String channelName = jsonArray.getString(1);
for (Channel channel:
channels) {
if (channel.getChannelName().equals(channelName)){
for (Channel channel :
channels) {
if (channel.getChannelName().equals(channelName)) {
channel.onMessage(jsonArray.get(2));
break;
}
Expand All @@ -51,18 +50,23 @@ void messageDecode(final ClusterWS socket, String message){
socket.getEmitter().emit(jsonArray.getString(1), jsonArray.get(2));
break;
case "s":
if (jsonArray.getString(1).equals("c")){
if (jsonArray.getString(1).equals("c")) {
socket.getPingTimer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (socket.getLost() < 3){
if (socket.getMissedPing() < 3) {
socket.incrementLost();
} else {
socket.disconnect(4001,"No pings");
socket.disconnect(4001, "No pings");
cancel();
}
}
},0,jsonArray.getJSONObject(2).getInt("ping"));
}, 0, jsonArray.getJSONObject(2).getInt("ping"));
boolean useBinary = jsonArray.getJSONObject(2).getBoolean("binary");
socket.setUseBinary(useBinary);
if (socket.getClusterWSListener() != null) {
socket.getClusterWSListener().onConnected(socket);
}
}
break;
}
Expand Down

0 comments on commit 121a723

Please sign in to comment.