66import com .microsoft .hydralab .agent .service .AgentWebSocketClientService ;
77import com .microsoft .hydralab .common .entity .common .Message ;
88import com .microsoft .hydralab .common .util .Const ;
9+ import com .microsoft .hydralab .common .util .FlowUtil ;
910import com .microsoft .hydralab .common .util .SerializeUtil ;
1011import lombok .extern .slf4j .Slf4j ;
1112import org .java_websocket .client .WebSocketClient ;
@@ -20,7 +21,6 @@ public class AgentWebSocketClient extends WebSocketClient {
2021 private final AgentWebSocketClientService agentWebSocketClientService ;
2122
2223 private boolean connectionActive = false ;
23- private boolean shouldRetryConnection = true ;
2424 private int reconnectTime = 0 ;
2525
2626 public AgentWebSocketClient (URI serverUri , AgentWebSocketClientService agentWebSocketClientService ) {
@@ -29,7 +29,14 @@ public AgentWebSocketClient(URI serverUri, AgentWebSocketClientService agentWebS
2929 agentWebSocketClientService .setSendMessageCallback (message -> {
3030 byte [] data = SerializeUtil .messageToByteArr (message );
3131 log .info ("send, path: {}, message data len: {}" , message .getPath (), data .length );
32- AgentWebSocketClient .this .send (data );
32+ try {
33+ FlowUtil .retryAndSleepWhenException (3 , 10 , () -> {
34+ AgentWebSocketClient .this .send (data );
35+ return true ;
36+ });
37+ } catch (Exception e ) {
38+ log .error ("send message to center error, message path is {}" , message .getPath (), e );
39+ }
3340 });
3441 }
3542
@@ -58,11 +65,13 @@ public void onMessage(String message) {
5865
5966 @ Override
6067 public void onClose (int code , String reason , boolean remote ) {
61- log .info ("onClose {}, {}, {}" , code , reason , remote );
68+ log .error ("onClose {}, {}, {}" , code , reason , remote );
6269 reconnectTime ++;
6370 connectionActive = false ;
64- // The remote server has stopped, we need to try reconnecting at certain frequency.
65- shouldRetryConnection = code != CloseReason .CloseCodes .CANNOT_ACCEPT .getCode ();
71+ // if the connection is closed by server with 1008,1003, exit the agent
72+ if (code == CloseReason .CloseCodes .CANNOT_ACCEPT .getCode () || code == CloseReason .CloseCodes .VIOLATED_POLICY .getCode ()) {
73+ System .exit (code );
74+ }
6675 }
6776
6877 @ Override
@@ -76,10 +85,6 @@ public boolean isConnectionActive() {
7685 return connectionActive ;
7786 }
7887
79- public boolean shouldRetryConnection () {
80- return shouldRetryConnection ;
81- }
82-
8388 public int getReconnectTime () {
8489 return reconnectTime ;
8590 }
0 commit comments