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

Commit 639a5c0

Browse files
authored
Merge pull request #4 from ClusterWS/next
Next to version 3.0.0
2 parents 88f1333 + 879017d commit 639a5c0

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

docs/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<h6 align="center">Build Scalable Node.js WebSocket Applications</h6>
33

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

88
<p align="center">
9-
<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>
10-
<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>
11-
<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>
9+
<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>
10+
<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>
11+
<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>
1212
</p>
1313

1414
<p align="center">
@@ -17,5 +17,5 @@
1717

1818
<h1></h1>
1919
<h3 align="center">
20-
Find more about ClusterWS JavaScript Client in <a href="https://github.com/ClusterWS/ClusterWS-Client-Java/wiki"><strong>Wiki Documentation</strong></a>
20+
Find more about ClusterWS Java Client in <a href="https://github.com/ClusterWS/ClusterWS-Client-Java/wiki"><strong>Wiki Documentation</strong></a>
2121
</h3>

src/main/java/com/clusterws/ClusterWS.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class ClusterWS {
2121
private PingHandler mPingHandler;
2222
private List<Channel> mChannels;
2323
private ReconnectionParams mReconnectionParams;
24+
private static final byte[] PONG = "A".getBytes();
2425

2526
public ClusterWS(String url) {
2627
if (url == null) {
@@ -130,7 +131,10 @@ private void createSocket() {
130131
mSocket = new Socket(URI.create(mUrl), new ISocketEvents() {
131132
@Override
132133
public void onOpen() {
133-
mClusterWSListener.onConnected();
134+
for (Channel channel :
135+
mChannels) {
136+
channel.subscribe();
137+
}
134138
}
135139

136140
@Override
@@ -145,8 +149,10 @@ public void onClose(int code, String reason) {
145149
if (mPingHandler.getPingTimer() != null) {
146150
mPingHandler.getPingTimer().cancel();
147151
}
148-
if (mReconnectionParams.isAutoReconnect() && code != 1000 && (mReconnectionParams.getReconnectionAttempts() == 0 || mReconnectionParams.getReconnectionsAttempted() < mReconnectionParams.getReconnectionAttempts())) {
149-
if (mSocket.getReadyState() == WebSocket.READYSTATE.CLOSED || mSocket.getReadyState() == WebSocket.READYSTATE.NOT_YET_CONNECTED) {
152+
if (mReconnectionParams.isAutoReconnect()
153+
&& code != 1000
154+
&& (mReconnectionParams.getReconnectionAttempts() == 0 || mReconnectionParams.getReconnectionsAttempted() < mReconnectionParams.getReconnectionAttempts())) {
155+
if (mSocket.getReadyState() == WebSocket.READYSTATE.CLOSED || mSocket.getReadyState() == WebSocket.READYSTATE.NOT_YET_CONNECTED || mSocket.getReadyState() == WebSocket.READYSTATE.CLOSING) {
150156
mReconnectionParams.incrementReconnectionsAttempted();
151157
int randomDelay = ThreadLocalRandom.current().nextInt(1,
152158
mReconnectionParams.getReconnectionIntervalMax() -
@@ -165,8 +171,17 @@ public void run() {
165171

166172
@Override
167173
public void onBinaryMessage(ByteBuffer bytes) {
168-
String message = StandardCharsets.UTF_8.decode(bytes).toString();
169-
onMessageReceived(message);
174+
System.out.println("GOT MESSAGE");
175+
byte[] arr = new byte[bytes.remaining()];
176+
bytes.get(arr);
177+
if (arr.length == 1 && arr[0] == 57) {
178+
mPingHandler.setMissedPingToZero();
179+
mSocket.send(PONG);
180+
} else {
181+
String message = new String(arr, StandardCharsets.UTF_8);
182+
onMessageReceived(message);
183+
}
184+
170185
}
171186

172187
@Override
@@ -181,11 +196,7 @@ public void onMessage(String message) {
181196
}
182197

183198
private void onMessageReceived(String message) {
184-
if (message.equals("#0")) {
185-
mPingHandler.setMissedPingToZero();
186-
send("#1", null, "ping");
187-
} else {
188-
mMessageHandler.messageDecode(ClusterWS.this, message);
189-
}
199+
System.out.println("MESSAGE IS " + message);
200+
mMessageHandler.messageDecode(ClusterWS.this, message);
190201
}
191202
}

src/test/java/com/clusterws/ChannelServerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ChannelServerTest {
1616

1717
@Before
1818
public void init() throws Exception {
19-
mClusterWS = new ClusterWS("ws://localhost:80");
19+
mClusterWS = new ClusterWS("ws://localhost:3000");
2020
mGotTheData = false;
2121
mReceivedData = null;
2222
}
@@ -189,7 +189,7 @@ public void onDataReceived(String channelName, Object data) {
189189
}
190190
});
191191
mClusterWS.disconnect(4001, "hui");
192-
Thread.sleep(1000);
192+
Thread.sleep(3000);
193193
channel.publish("testData");
194194
Thread.sleep(1000);
195195
assertTrue("Did not get the data", mGotTheData);

src/test/java/com/clusterws/ClusterWSTest.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ClusterWSTest {
2020

2121
@Before
2222
public void init() {
23-
mClusterWS = new ClusterWS("ws://localhost:80");
23+
mClusterWS = new ClusterWS("ws://localhost:3000");
2424
receivedData = null;
2525
gotTheData = false;
2626
}
@@ -36,7 +36,7 @@ public void connect() throws Exception {
3636
mClusterWS.connect();
3737
Thread.sleep(1000);
3838

39-
assertEquals("Socket did not connect", WebSocket.READYSTATE.OPEN,mClusterWS.getState());
39+
assertEquals("Socket did not connect", WebSocket.READYSTATE.OPEN, mClusterWS.getState());
4040
}
4141

4242
@Test
@@ -233,24 +233,24 @@ public void onDisconnected(int code, String reason) {
233233
}
234234

235235
@Test
236-
public void testReconnection() throws Exception{
237-
mClusterWS.setReconnection(true,1000,2000,null);
236+
public void testReconnection() throws Exception {
237+
mClusterWS.setReconnection(true, 1000, 2000, null);
238238
mClusterWS.connect();
239239
Thread.sleep(1000);
240-
mClusterWS.disconnect(3002,"test");
241-
Thread.sleep(2000);
240+
mClusterWS.disconnect(3002, "test");
241+
Thread.sleep(4000);
242242
assertEquals("Did not reconnect", WebSocket.READYSTATE.OPEN, mClusterWS.getState());
243243
}
244244

245245
@Test
246-
public void testPingPong() throws Exception{
246+
public void testPingPong() throws Exception {
247247
mClusterWS.connect();
248248
Thread.sleep(1900);
249-
assertEquals("Websocket disconnected", WebSocket.READYSTATE.OPEN,mClusterWS.getState());
249+
assertEquals("Websocket disconnected", WebSocket.READYSTATE.OPEN, mClusterWS.getState());
250250
}
251251

252252
@Test(expected = NullPointerException.class)
253-
public void testNullUrl() throws Exception{
253+
public void testNullUrl() throws Exception {
254254
mClusterWS = new ClusterWS(null);
255255
}
256256

0 commit comments

Comments
 (0)