diff --git a/src/main/java/com/clusterws/ClusterWS.java b/src/main/java/com/clusterws/ClusterWS.java index 43e4fc1..00ef046 100644 --- a/src/main/java/com/clusterws/ClusterWS.java +++ b/src/main/java/com/clusterws/ClusterWS.java @@ -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; @@ -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) { @@ -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 : @@ -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(); @@ -172,4 +175,5 @@ private void onMessageReceived(String message) { mMessageHandler.messageDecode(ClusterWS.this, message); } } + } diff --git a/src/main/java/com/clusterws/Options.java b/src/main/java/com/clusterws/Options.java deleted file mode 100644 index f5b063d..0000000 --- a/src/main/java/com/clusterws/Options.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.clusterws; - - -class Options { - private String mUrl; - - Options(String url) { - if (url == null){ - throw new NullPointerException("Url must be provided"); - } - mUrl = url; - } - - public String getUrl() { - return mUrl; - } -}