Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 committed Nov 28, 2024
2 parents 41746cd + fa734ff commit 48c2714
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
Expand Down Expand Up @@ -100,7 +102,7 @@ protected Map<String, Endpoint> getEndpoints() {
return endpoints;
}

public static Node connect(String url) {
public static Node connect(String url, Optional<Function<Exception, Void>> onError) {
Node node = new Node();
var responsePool = new HashMap<Integer, BlockingQueue<EdgeResult<EdgeResponseEnum, EdgeError>>>();
try {
Expand Down Expand Up @@ -165,13 +167,16 @@ public void onMessage(String message) {
@Override
public void onClose(int code, String reason, boolean remote) {
node.setAlive(false);
node.socketConnectLatch.countDown();
}

@Override
public void onError(Exception ex) {
this.close();
node.setAlive(false);
node.socketConnectLatch.countDown();
log.warn("socket onError", ex);
onError.ifPresent(fn -> fn.apply(ex));
}
};
socket.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.http.HttpResponse;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -58,8 +59,8 @@ public Process startServer() throws IOException, InterruptedException {
public void testConnection() throws IOException, InterruptedException {
startServer();
Thread.sleep(1000L);
Node node_a = Node.connect(getWsUrl());
Node node_b = Node.connect(getWsUrl());
Node node_a = Node.connect(getWsUrl(), Optional.empty());
Node node_b = Node.connect(getWsUrl(), Optional.empty());

Endpoint endpoint_b1 = node_a.createEndpoint("test", Arrays.asList("event/*"));
Endpoint endpoint_b2 = node_b.createEndpoint("test", Arrays.asList("event/**/b2"));
Expand Down

0 comments on commit 48c2714

Please sign in to comment.