|
@@ -33,7 +33,7 @@ pub struct AFCloudServer {
|
|
|
#[allow(dead_code)]
|
|
|
pub(crate) config: AFCloudConfiguration,
|
|
|
pub(crate) client: Arc<AFCloudClient>,
|
|
|
- enable_sync: AtomicBool,
|
|
|
+ enable_sync: Arc<AtomicBool>,
|
|
|
#[allow(dead_code)]
|
|
|
device_id: Arc<parking_lot::RwLock<String>>,
|
|
|
ws_client: Arc<WSClient>,
|
|
@@ -47,7 +47,7 @@ impl AFCloudServer {
|
|
|
) -> Self {
|
|
|
let api_client = AFCloudClient::new(&config.base_url, &config.ws_base_url, &config.gotrue_url);
|
|
|
let token_state_rx = api_client.subscribe_token_state();
|
|
|
- let enable_sync = AtomicBool::new(enable_sync);
|
|
|
+ let enable_sync = Arc::new(AtomicBool::new(enable_sync));
|
|
|
|
|
|
let ws_client = WSClient::new(WSClientConfig {
|
|
|
buffer_capacity: 100,
|
|
@@ -57,7 +57,13 @@ impl AFCloudServer {
|
|
|
let ws_client = Arc::new(ws_client);
|
|
|
let api_client = Arc::new(api_client);
|
|
|
|
|
|
- spawn_ws_conn(&device_id, token_state_rx, &ws_client, &api_client);
|
|
|
+ spawn_ws_conn(
|
|
|
+ &device_id,
|
|
|
+ token_state_rx,
|
|
|
+ &ws_client,
|
|
|
+ &api_client,
|
|
|
+ &enable_sync,
|
|
|
+ );
|
|
|
Self {
|
|
|
config,
|
|
|
client: api_client,
|
|
@@ -172,10 +178,12 @@ fn spawn_ws_conn(
|
|
|
mut token_state_rx: TokenStateReceiver,
|
|
|
ws_client: &Arc<WSClient>,
|
|
|
api_client: &Arc<Client>,
|
|
|
+ enable_sync: &Arc<AtomicBool>,
|
|
|
) {
|
|
|
let weak_device_id = Arc::downgrade(device_id);
|
|
|
let weak_ws_client = Arc::downgrade(ws_client);
|
|
|
let weak_api_client = Arc::downgrade(api_client);
|
|
|
+ let enable_sync = enable_sync.clone();
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
if let Some(ws_client) = weak_ws_client.upgrade() {
|
|
@@ -189,10 +197,15 @@ fn spawn_ws_conn(
|
|
|
if let (Some(api_client), Some(device_id)) =
|
|
|
(weak_api_client.upgrade(), weak_device_id.upgrade())
|
|
|
{
|
|
|
- let device_id = device_id.read().clone();
|
|
|
- if let Ok(ws_addr) = api_client.ws_url(&device_id) {
|
|
|
- info!("🟢WebSocket Reconnecting");
|
|
|
- let _ = ws_client.connect(ws_addr).await;
|
|
|
+ if enable_sync.load(Ordering::SeqCst) {
|
|
|
+ info!("🟢websocket state: {:?}, reconnecting", state);
|
|
|
+ let device_id = device_id.read().clone();
|
|
|
+ match api_client.ws_url(&device_id) {
|
|
|
+ Ok(ws_addr) => {
|
|
|
+ let _ = ws_client.connect(ws_addr).await;
|
|
|
+ },
|
|
|
+ Err(err) => error!("Failed to get ws url: {}", err),
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -204,7 +217,6 @@ fn spawn_ws_conn(
|
|
|
let weak_api_client = Arc::downgrade(api_client);
|
|
|
tokio::spawn(async move {
|
|
|
while let Ok(token_state) = token_state_rx.recv().await {
|
|
|
- info!("🟢Token state: {:?}", token_state);
|
|
|
match token_state {
|
|
|
TokenState::Refresh => {
|
|
|
if let (Some(api_client), Some(ws_client), Some(device_id)) = (
|
|
@@ -213,14 +225,18 @@ fn spawn_ws_conn(
|
|
|
weak_device_id.upgrade(),
|
|
|
) {
|
|
|
let device_id = device_id.read().clone();
|
|
|
- if let Ok(ws_addr) = api_client.ws_url(&device_id) {
|
|
|
- let _ = ws_client.connect(ws_addr).await;
|
|
|
+ match api_client.ws_url(&device_id) {
|
|
|
+ Ok(ws_addr) => {
|
|
|
+ info!("🟢token state: {:?}, reconnecting websocket", token_state);
|
|
|
+ let _ = ws_client.connect(ws_addr).await;
|
|
|
+ },
|
|
|
+ Err(err) => error!("Failed to get ws url: {}", err),
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
TokenState::Invalid => {
|
|
|
if let Some(ws_client) = weak_ws_client.upgrade() {
|
|
|
- info!("🟡WebSocket Disconnecting");
|
|
|
+ info!("🟢token state: {:?}, disconnect websocket", token_state);
|
|
|
ws_client.disconnect().await;
|
|
|
}
|
|
|
},
|