Skip to content

Commit

Permalink
Merge pull request #7 from angel-one/main
Browse files Browse the repository at this point in the history
on error method added
  • Loading branch information
ACC-Vaibhav-Palvi authored Nov 13, 2023
2 parents 8a64c15 + 88a5206 commit 154386d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface SmartStreamListener {
void onDisconnected();
void onError(SmartStreamError error);
void onPong();

SmartStreamError onErrorCustom();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
import java.util.TimerTask;

import com.angelbroking.smartapi.smartstream.models.*;
import com.neovisionaries.ws.client.*;
import lombok.SneakyThrows;
import org.json.JSONArray;
import org.json.JSONObject;

import com.angelbroking.smartapi.Routes;
import com.angelbroking.smartapi.http.exceptions.SmartAPIException;
import com.angelbroking.smartapi.utils.ByteUtils;
import com.angelbroking.smartapi.utils.Utils;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFactory;
import com.neovisionaries.ws.client.WebSocketFrame;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -37,6 +34,7 @@ public class SmartStreamTicker {

private final Routes routes = new Routes();
private final String wsuri = routes.getSmartStreamWSURI();

private final SmartStreamListener smartStreamListener;
private WebSocket ws;
private final String clientId;
Expand Down Expand Up @@ -71,7 +69,7 @@ private void init() {
.setVerifyHostname(false)
.createSocket(wsuri)
.setPingInterval(PING_INTERVAL);
ws.addHeader(CLIENT_ID_HEADER, clientId);
ws.addHeader(CLIENT_ID_HEADER, clientId);
ws.addHeader(FEED_TOKEN_HEADER, feedToken);
ws.addHeader(CLIENT_LIB_HEADER, "JAVA");
ws.addListener(getWebsocketAdapter());
Expand Down Expand Up @@ -186,14 +184,19 @@ public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame,
smartStreamListener.onError(error);
}
}

@Override
public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
super.onCloseFrame(websocket, frame);
}

@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
smartStreamListener.onErrorCustom();
}
};
}

private void startPingTimer(final WebSocket websocket) {

pingTimer = new Timer();
Expand Down Expand Up @@ -242,7 +245,7 @@ public void disconnect() {

/**
* Returns true if websocket connection is open.
*
*
* @return boolean
*/
public boolean isConnectionOpen() {
Expand All @@ -251,7 +254,7 @@ public boolean isConnectionOpen() {

/**
* Returns true if websocket connection is closed.
*
*
* @return boolean
*/
public boolean isConnectionClosed() {
Expand Down Expand Up @@ -328,7 +331,7 @@ private JSONArray generateExchangeTokensList(Set<TokenID> tokens) {
JSONObject exchangeTokenObj = new JSONObject();
exchangeTokenObj.put("exchangeType", ex.getVal());
exchangeTokenObj.put("tokens", t);

exchangeTokenList.put(exchangeTokenObj);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.time.ZonedDateTime;
import java.util.Arrays;

import com.angelbroking.smartapi.http.exceptions.SmartAPIException;
import com.angelbroking.smartapi.smartstream.models.*;
import com.angelbroking.smartapi.smartstream.ticker.SmartStreamListener;

Expand Down Expand Up @@ -105,6 +106,16 @@ public void onPong() {
log.info("pong received");
}

/* Custom on error method for SmartStream */
@Override
public SmartStreamError onErrorCustom() {
// User thrown error return
log.info("on custom error");
SmartStreamError smartStreamError = new SmartStreamError();
smartStreamError.setException(new SmartAPIException("custom error received"));
return smartStreamError;
}

@Override
public void onDisconnected() {
// TODO Auto-generated method stub
Expand Down

0 comments on commit 154386d

Please sign in to comment.