Commit 3180219d authored by Lisa (AI Assistant)'s avatar Lisa (AI Assistant)

Fix: Send pairing request on reconnect instead of on closing socket

parent e36c37af
......@@ -63,6 +63,7 @@ class NodeClient() {
private var challengeNonce: String = ""
private var challengeTimestamp: Long = 0
private var hasReceivedChallenge = false
private var pendingPairingAfterConnect = false // Flag to send pairing request after reconnect
// SharedPreferences for device identity persistence
private var prefs: SharedPreferences? = null
......@@ -223,8 +224,15 @@ class NodeClient() {
hasReceivedChallenge = true
Log.d(TAG, "Received challenge: nonce=$challengeNonce, ts=$challengeTimestamp")
// Now send connect request with proper v3 signature
sendConnectRequestWithChallenge(nodeId, token, capabilities)
// Check if we need to send pairing request instead of connect
if (pendingPairingAfterConnect) {
pendingPairingAfterConnect = false
Log.d(TAG, "Sending pairing request after reconnect")
sendPairingRequest(nodeId, token, capabilities)
} else {
// Now send connect request with proper v3 signature
sendConnectRequestWithChallenge(nodeId, token, capabilities)
}
} else if (event == "node.pair.resolved") {
// Pairing was approved, now reconnect with token
val approved = json.optJSONObject("payload")?.optBoolean("approved", false)
......@@ -252,9 +260,9 @@ class NodeClient() {
if ((error?.contains("device-required") == true ||
error?.contains("device signature") == true ||
error?.contains("invalid") == true) && isPairingMode) {
// Now send pairing request after connect failed
Log.d(TAG, "Sending pairing request after connection rejection")
sendPairingRequest(currentNodeId, currentToken, capabilities)
// Set flag to send pairing request after reconnect
pendingPairingAfterConnect = true
Log.d(TAG, "Will send pairing request on reconnect after connection rejection")
} else {
_connectionStatus.trySend(ConnectionStatus(false, nodeId))
}
......@@ -276,13 +284,13 @@ class NodeClient() {
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
Log.d(TAG, "WebSocket closing: code=$code reason=$reason")
// Check if it's a device signature error and we should send pairing request
// Check if it's a device signature error - need to reconnect with pairing request
if (isPairingMode && (reason.contains("device signature") ||
reason.contains("invalid") ||
reason.contains("device-required"))) {
Log.d(TAG, "Connection rejected due to device auth, sending pairing request")
// Send pairing request after connection rejection
sendPairingRequest(currentNodeId, currentToken, capabilities)
Log.d(TAG, "Connection rejected due to device auth, will send pairing request on reconnect")
// Set flag to send pairing request after reconnect
pendingPairingAfterConnect = true
}
webSocket.close(1000, null)
......@@ -296,8 +304,14 @@ class NodeClient() {
if (isPairingMode && (reason.contains("device signature") ||
reason.contains("invalid") ||
reason.contains("device-required"))) {
Log.d(TAG, "Connection rejected due to device auth, sending pairing request")
sendPairingRequest(currentNodeId, currentToken, capabilities)
Log.d(TAG, "Connection rejected due to device auth, will send pairing request on reconnect")
// Set flag to send pairing request after reconnect
pendingPairingAfterConnect = true
// Reconnect
scope.launch {
delay(500) // Brief delay before reconnecting
connect(currentNodeId, currentToken, capabilities)
}
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment