Skip to content

Commit

Permalink
refactor: Address PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zurcusa committed Sep 19, 2024
1 parent b265f5d commit 27e3616
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/limechain/rpc/WebsocketState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.limechain.rpc;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum WebsocketState {

CONNECTING(0),
OPEN(1),
CLOSING(2),
CLOSED(3);

private final int intValue;
}
26 changes: 15 additions & 11 deletions src/main/java/com/limechain/rpc/WsRpcClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.limechain.constants.RpcConstants;
import lombok.SneakyThrows;
import lombok.extern.java.Log;
import org.teavm.jso.browser.Window;
import org.teavm.jso.core.JSString;
import org.teavm.jso.websocket.WebSocket;
Expand All @@ -12,8 +13,11 @@
/**
* The implementation of {@link WsRpcClient}. Uses a native JS Websocket implementation.
*/
@Log
public class WsRpcClientImpl implements WsRpcClient {

private static final int WS_OPEN_WAIT_MS = 50;

private WebSocket ws;
private final Queue<String> responseQueue;

Expand All @@ -23,25 +27,25 @@ public WsRpcClientImpl() {
}

private void openWebsocketConnection() {
System.out.println("Initializing RPC websocket connection...");
log.info("Initializing RPC websocket connection...");
//TODO change when configuring chain.
ws = new WebSocket(RpcConstants.POLKADOT_WS_RPC);
initHandlers();
}

private void initHandlers() {
ws.onClose(e -> {
System.out.println("RPC websocket connection was closed.");
System.out.println("Retrying connection...");
log.info("RPC websocket connection was closed.");
log.info("Retrying connection...");
Window.setTimeout(this::openWebsocketConnection, 1000);
});

ws.onError(e -> {
System.out.println("There was an error in the RPC websocket connection. Closing connection...");
log.warning("There was an error in the RPC websocket connection. Closing connection...");
ws.close();
});

ws.onOpen(e -> System.out.println("Websocket connection is open."));
ws.onOpen(e -> log.info("Websocket connection is open."));
ws.onMessage(e -> responseQueue.offer(e.getDataAsString()));
}

Expand All @@ -62,16 +66,16 @@ public void send(JSString rpcString) {
*/
@SneakyThrows
private void handleSocketState() {
var startState = ws.getReadyState();
int startState = ws.getReadyState();
int openedState = WebsocketState.OPEN.getIntValue();

while (startState != 1) {
var currentState = ws.getReadyState();
if (currentState > 1) {
while (startState != openedState) {
if (startState > openedState) {
throw new Exception("Calling function of a closed websocket is prohibited.");
}

startState = currentState;
Thread.sleep(50);
Thread.sleep(WS_OPEN_WAIT_MS);
startState = ws.getReadyState();
}
}

Expand Down

0 comments on commit 27e3616

Please sign in to comment.