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

Commit

Permalink
Replace Options class with mUrl variable and added test for null poin…
Browse files Browse the repository at this point in the history
…ter exception when url is null
  • Loading branch information
Yegorisa committed Dec 28, 2017
1 parent 887d83c commit 1496cf4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 75 deletions.
120 changes: 62 additions & 58 deletions src/main/java/com/clusterws/ClusterWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class ClusterWS {
private Socket mSocket;
private Options mOptions;
private String mUrl;
private Emitter mEmitter;
private boolean mUseBinary;
private IClusterWSListener mClusterWSListener;
Expand All @@ -20,75 +20,32 @@ public class ClusterWS {
private ReconnectionHandler mReconnectionHandler;

public ClusterWS(String url) {
mOptions = new Options(url);
if (url == null){
throw new NullPointerException("Url must be provided");
}
mUrl = url;
mChannels = new ArrayList<>();
mReconnectionHandler = new ReconnectionHandler(null, null, null, null, this);
createSocket();
}

private void createSocket() {
mSocket = new Socket(URI.create(mOptions.getUrl()), new ISocketEvents() {
@Override
public void onOpen() {
mReconnectionHandler.onOpen();
}

@Override
public void onError(Exception exception) {
if (mClusterWSListener != null) {
mClusterWSListener.onError(exception);
}
}

@Override
public void onClose(int code, String reason) {
if (mPingHandler.getPingTimer() != null) {
mPingHandler.getPingTimer().cancel();
}

if (mReconnectionHandler.isInReconnectionState()) {
return;
}
if (mReconnectionHandler.isAutoReconnect() && code != 1000) {
mReconnectionHandler.reconnect();
}

if (mClusterWSListener != null) {
mClusterWSListener.onDisconnected(code, reason);
}
}

@Override
public void onBinaryMessage(ByteBuffer bytes) {
String message = StandardCharsets.UTF_8.decode(bytes).toString();
onMessageReceived(message);
}

@Override
public void onMessage(String message) {
onMessageReceived(message);
}
});
mUseBinary = false;
mEmitter = new Emitter();
mMessageHandler = new MessageHandler();
mPingHandler = new PingHandler();
}

public ClusterWS setReconnection(Boolean autoReconnect, Integer reconnectionIntervalMin, Integer reconnectionIntervalMax, Integer reconnectionAttempts) {
mReconnectionHandler = new ReconnectionHandler(autoReconnect, reconnectionIntervalMin, reconnectionIntervalMax, reconnectionAttempts, this);
return this;
}

public ClusterWS setClusterWSListener(IClusterWSListener clusterWSListener) {
mClusterWSListener = clusterWSListener;
return this;
}

public void connect() {
createSocket();
mSocket.connect();
}


public ClusterWS setClusterWSListener(IClusterWSListener clusterWSListener) {
mClusterWSListener = clusterWSListener;
return this;
public void disconnect(Integer closeCode, String reason) {
mSocket.close(closeCode == null ? 1000 : closeCode, reason);
}

public void on(String event, IEmitterListener listener) {
Expand All @@ -107,9 +64,6 @@ public WebSocket.READYSTATE getState() {
return mSocket.getReadyState();
}

public void disconnect(Integer closeCode, String reason) {
mSocket.close(closeCode == null ? 1000 : closeCode, reason);
}

public Channel getChannelByName(String channelName) {
for (Channel channel :
Expand Down Expand Up @@ -164,6 +118,55 @@ PingHandler getPingHandler() {
return mPingHandler;
}

private void createSocket() {
mSocket = new Socket(URI.create(mUrl), new ISocketEvents() {
@Override
public void onOpen() {
mReconnectionHandler.onOpen();
}

@Override
public void onError(Exception exception) {
if (mClusterWSListener != null) {
mClusterWSListener.onError(exception);
}
}

@Override
public void onClose(int code, String reason) {
if (mPingHandler.getPingTimer() != null) {
mPingHandler.getPingTimer().cancel();
}

if (mReconnectionHandler.isInReconnectionState()) {
return;
}
if (mReconnectionHandler.isAutoReconnect() && code != 1000) {
mReconnectionHandler.reconnect();
}

if (mClusterWSListener != null) {
mClusterWSListener.onDisconnected(code, reason);
}
}

@Override
public void onBinaryMessage(ByteBuffer bytes) {
String message = StandardCharsets.UTF_8.decode(bytes).toString();
onMessageReceived(message);
}

@Override
public void onMessage(String message) {
onMessageReceived(message);
}
});
mUseBinary = false;
mEmitter = new Emitter();
mMessageHandler = new MessageHandler();
mPingHandler = new PingHandler();
}

private void onMessageReceived(String message) {
if (message.equals("#0")) {
mPingHandler.setMissedPingToZero();
Expand All @@ -172,4 +175,5 @@ private void onMessageReceived(String message) {
mMessageHandler.messageDecode(ClusterWS.this, message);
}
}

}
17 changes: 0 additions & 17 deletions src/main/java/com/clusterws/Options.java

This file was deleted.

0 comments on commit 1496cf4

Please sign in to comment.