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 #4 from ClusterWS/next
Browse files Browse the repository at this point in the history
Next to version 3.0.0
  • Loading branch information
Yegorisa authored Aug 21, 2018
2 parents 88f1333 + 879017d commit 639a5c0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
10 changes: 5 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<h6 align="center">Build Scalable Node.js WebSocket Applications</h6>

<p align="center">
<img src="https://cdn.rawgit.com/goriunov/159120ca6a883d8d4e75543ec395d361/raw/146220360173a2428fceb44e7fc9b2cda8a17832/clusterws.svg" width="450">
<img src="https://cdn.rawgit.com/goriunov/159120ca6a883d8d4e75543ec395d361/raw/d22028ecc726d7d3cc30a2a85cc7cc454b0afada/clusterws.svg" width="450">
</p>

<p align="center">
<a href="https://github.com/ClusterWS/ClusterWS-Client-Java/blob/master/LICENSE"><img src="https://img.shields.io/github/license/ClusterWS/ClusterWS-Client-JS.svg?style=for-the-badge" alt="GitHub license" /></a>
<a title="JitPack Version" href="https://jitpack.io/#ClusterWS/ClusterWS-Client-Java"><img src="https://img.shields.io/badge/JitPack-1.6.0-brightgreen.svg?longCache=true&style=for-the-badge"></a>
<a href="https://github.com/ClusterWS/ClusterWS-Client-JS/graphs/commit-activity"><img src="https://img.shields.io/badge/Maintain-Yes-green.svg?style=for-the-badge" alt="Maintain" /></a>
<a href="https://github.com/ClusterWS/ClusterWS-Client-Java/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-AE1E80.svg?longCache=true&style=for-the-badge"></a>
<a title="JitPack Version" href="https://jitpack.io/#ClusterWS/ClusterWS-Client-Java"><img src="https://img.shields.io/badge/JitPack-3.0.0-AE1E80.svg?longCache=true&style=for-the-badge"></a>
<a href="https://github.com/ClusterWS/ClusterWS-Client-JS/graphs/commit-activity"><img src="https://img.shields.io/badge/Maintain-Yes-AE1E80.svg?style=for-the-badge" alt="Maintain" /></a>
</p>

<p align="center">
Expand All @@ -17,5 +17,5 @@

<h1></h1>
<h3 align="center">
Find more about ClusterWS JavaScript Client in <a href="https://github.com/ClusterWS/ClusterWS-Client-Java/wiki"><strong>Wiki Documentation</strong></a>
Find more about ClusterWS Java Client in <a href="https://github.com/ClusterWS/ClusterWS-Client-Java/wiki"><strong>Wiki Documentation</strong></a>
</h3>
33 changes: 22 additions & 11 deletions src/main/java/com/clusterws/ClusterWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ClusterWS {
private PingHandler mPingHandler;
private List<Channel> mChannels;
private ReconnectionParams mReconnectionParams;
private static final byte[] PONG = "A".getBytes();

public ClusterWS(String url) {
if (url == null) {
Expand Down Expand Up @@ -130,7 +131,10 @@ private void createSocket() {
mSocket = new Socket(URI.create(mUrl), new ISocketEvents() {
@Override
public void onOpen() {
mClusterWSListener.onConnected();
for (Channel channel :
mChannels) {
channel.subscribe();
}
}

@Override
Expand All @@ -145,8 +149,10 @@ public void onClose(int code, String reason) {
if (mPingHandler.getPingTimer() != null) {
mPingHandler.getPingTimer().cancel();
}
if (mReconnectionParams.isAutoReconnect() && code != 1000 && (mReconnectionParams.getReconnectionAttempts() == 0 || mReconnectionParams.getReconnectionsAttempted() < mReconnectionParams.getReconnectionAttempts())) {
if (mSocket.getReadyState() == WebSocket.READYSTATE.CLOSED || mSocket.getReadyState() == WebSocket.READYSTATE.NOT_YET_CONNECTED) {
if (mReconnectionParams.isAutoReconnect()
&& code != 1000
&& (mReconnectionParams.getReconnectionAttempts() == 0 || mReconnectionParams.getReconnectionsAttempted() < mReconnectionParams.getReconnectionAttempts())) {
if (mSocket.getReadyState() == WebSocket.READYSTATE.CLOSED || mSocket.getReadyState() == WebSocket.READYSTATE.NOT_YET_CONNECTED || mSocket.getReadyState() == WebSocket.READYSTATE.CLOSING) {
mReconnectionParams.incrementReconnectionsAttempted();
int randomDelay = ThreadLocalRandom.current().nextInt(1,
mReconnectionParams.getReconnectionIntervalMax() -
Expand All @@ -165,8 +171,17 @@ public void run() {

@Override
public void onBinaryMessage(ByteBuffer bytes) {
String message = StandardCharsets.UTF_8.decode(bytes).toString();
onMessageReceived(message);
System.out.println("GOT MESSAGE");
byte[] arr = new byte[bytes.remaining()];
bytes.get(arr);
if (arr.length == 1 && arr[0] == 57) {
mPingHandler.setMissedPingToZero();
mSocket.send(PONG);
} else {
String message = new String(arr, StandardCharsets.UTF_8);
onMessageReceived(message);
}

}

@Override
Expand All @@ -181,11 +196,7 @@ public void onMessage(String message) {
}

private void onMessageReceived(String message) {
if (message.equals("#0")) {
mPingHandler.setMissedPingToZero();
send("#1", null, "ping");
} else {
mMessageHandler.messageDecode(ClusterWS.this, message);
}
System.out.println("MESSAGE IS " + message);
mMessageHandler.messageDecode(ClusterWS.this, message);
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/clusterws/ChannelServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ChannelServerTest {

@Before
public void init() throws Exception {
mClusterWS = new ClusterWS("ws://localhost:80");
mClusterWS = new ClusterWS("ws://localhost:3000");
mGotTheData = false;
mReceivedData = null;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public void onDataReceived(String channelName, Object data) {
}
});
mClusterWS.disconnect(4001, "hui");
Thread.sleep(1000);
Thread.sleep(3000);
channel.publish("testData");
Thread.sleep(1000);
assertTrue("Did not get the data", mGotTheData);
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/clusterws/ClusterWSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ClusterWSTest {

@Before
public void init() {
mClusterWS = new ClusterWS("ws://localhost:80");
mClusterWS = new ClusterWS("ws://localhost:3000");
receivedData = null;
gotTheData = false;
}
Expand All @@ -36,7 +36,7 @@ public void connect() throws Exception {
mClusterWS.connect();
Thread.sleep(1000);

assertEquals("Socket did not connect", WebSocket.READYSTATE.OPEN,mClusterWS.getState());
assertEquals("Socket did not connect", WebSocket.READYSTATE.OPEN, mClusterWS.getState());
}

@Test
Expand Down Expand Up @@ -233,24 +233,24 @@ public void onDisconnected(int code, String reason) {
}

@Test
public void testReconnection() throws Exception{
mClusterWS.setReconnection(true,1000,2000,null);
public void testReconnection() throws Exception {
mClusterWS.setReconnection(true, 1000, 2000, null);
mClusterWS.connect();
Thread.sleep(1000);
mClusterWS.disconnect(3002,"test");
Thread.sleep(2000);
mClusterWS.disconnect(3002, "test");
Thread.sleep(4000);
assertEquals("Did not reconnect", WebSocket.READYSTATE.OPEN, mClusterWS.getState());
}

@Test
public void testPingPong() throws Exception{
public void testPingPong() throws Exception {
mClusterWS.connect();
Thread.sleep(1900);
assertEquals("Websocket disconnected", WebSocket.READYSTATE.OPEN,mClusterWS.getState());
assertEquals("Websocket disconnected", WebSocket.READYSTATE.OPEN, mClusterWS.getState());
}

@Test(expected = NullPointerException.class)
public void testNullUrl() throws Exception{
public void testNullUrl() throws Exception {
mClusterWS = new ClusterWS(null);
}

Expand Down

0 comments on commit 639a5c0

Please sign in to comment.