livekit / client-sdk-swift

LiveKit Swift Client SDK. Easily build live audio or video experiences into your mobile app, game or website.
https://livekit.io
Apache License 2.0
176 stars 85 forks source link

Reconnection issues #307

Closed wzJun1 closed 5 months ago

wzJun1 commented 6 months ago

Describe the bug

#306

  1. When the network is disconnected and the network is restored again, the reconnection is abnormal.

    My temporary solution:

      case let .reconnect(response):
        //roll back this code: await _responseQueue.suspend()
        await _responseQueue.suspend()
        restartPingTimer()
        notify { $0.signalClient(self, didReceiveConnectResponse: .reconnect(response)) }
        _connectResponseCompleter.resume(returning: .reconnect(response))

    Although this modification is normal, I feel it will cause other problems.

wzJun1 commented 6 months ago

There is also a small problem when using BroadcastExtension for screen sharing.

To turn off screen sharing, use setScreenShare(enabled: false). This will only turn off push streaming, but will not turn off iOS screen recording. this will prevent screen sharing from being enabled again.

My temporary solution is:

iShot_2024-01-16_14.13.25.png

If you have free time, I hope there is a better solution

hiroshihorie commented 6 months ago

Thanks for the report, but I don't think it's the response queue issue. I'll be debugging this one, but this will be tricky.

How often does it fail for you? I can reproduce it like 1 in 10 re-connect attempts.

wzJun1 commented 6 months ago

Many times, none of them succeeded.

As long as the network is disconnected, reconnecting is almost ineffective.

This is my connection configuration:

ConnectOptions(
                 autoSubscribe: false,
                 reconnectAttempts: 1000,
                 reconnectAttemptDelay: 3
             )

Before submitting this #306 , it is normal to reconnect.

hiroshihorie commented 6 months ago

Comparing logs between success and failure re-connects. Working on this one please give me some time.

wzJun1 commented 6 months ago

Comparing logs between success and failure re-connects. Working on this one please give me some time.

Great! Thank you for your help!

hiroshihorie commented 6 months ago

Can you try out if this branch helps ? https://github.com/livekit/client-sdk-swift/pull/308

wzJun1 commented 6 months ago

Can you try out if this branch helps ? #308

There's a problem. I don't know exactly how many seconds it will take. Reconnecting will be effective within about 30 seconds. If it exceeds 30 seconds, it still won't work.

The network is restored after more than 30 seconds. The log is as follows:

2024-01-16T17:34:30+0800 error LiveKitSDK: [] SignalClient._sendRequest(_:) connectionState is .disconnected
2024-01-16T17:34:30+0800 error LiveKitSDK: [] Engine.transport(_:didGenerateIceCandidate:) Failed to send iceCandidate
2024-01-16T17:34:30+0800 error LiveKitSDK: [] SignalClient._sendRequest(_:) connectionState is .disconnected
2024-01-16T17:34:30+0800 error LiveKitSDK: [] Engine.transport(_:didGenerateIceCandidate:) Failed to send iceCandidate
2024-01-16 17:34:30.104876+0800 UniPluginDemo[10623:3594799] baidu maponGetPermissionState--[200]
2024-01-16 17:34:32.051072+0800 UniPluginDemo[10623:3596537] Task <212CB067-8CD6-4E7A-AA4B-98D7D1F32B81>.<1> finished with error [57] Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected"
2024-01-16T17:34:32+0800 error LiveKitSDK : [] WebSocket.urlSession(_:task:didCompleteWithError:) didCompleteWithError: Optional(Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected"

Xcode will always output in the log: Optional(Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected"

wzJun1 commented 6 months ago

To put it simply, #308 only fixes reconnection part of the time. After more than X(X: Unknown, maybe 30, 40, 50 seconds) seconds, he may keep reconnecting, but the reconnection is invalid. The same error continues to be output in the log: Optional(Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected"

wzJun1 commented 6 months ago

Version 1.0.13 does not have this problem. If it is within 1 minute, it will quickly reconnect, if it is after 1 minute, it will be full connect.

For version 1.0.13, Maybe I understand it wrong, but that's how it feels. (Because on my UI if reconnected within 1 minute, the participant will not disappear (maybe participantDidJoin and participantDidLeave), if it is reconnected after 1 minute or after 2 minutes, the participant will re-render.)

wzJun1 commented 6 months ago

I think it is that didReceiveLeave was not called in SignalClient

wzJun1 commented 6 months ago

I added a .leave to the resumed queue status, which temporarily solved the problem, but there seemed to be new troubles. The video track and screen sharing track in the room were not restored.

let isJoinOrReconnect: Bool = {
        switch response.message {
        case .join, .reconnect, .leave: return true
        default: return false
        }
    }()
hiroshihorie commented 6 months ago

Hi thanks for your feedback, I will continue investigating today.

wzJun1 commented 5 months ago

Is there any gain? 😁

Hi thanks for your feedback, I will continue investigating today.

hiroshihorie commented 5 months ago

Hello can you try out this branch ? https://github.com/livekit/client-sdk-swift/pull/335

wzJun1 commented 5 months ago

Thx! I will try.

wzJun1 commented 5 months ago

great! it is worked!

hiroshihorie commented 5 months ago

Thanks a lot, I will continue to improve network issues.

hiroshihorie commented 5 months ago

This is in v2.0.2, I will incrementally make improvements. Closing this for now.