Skip to content

Commit

Permalink
Fix reconnect (livekit#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Jan 1, 2024
1 parent 45550c1 commit ac357a9
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 159 deletions.
5 changes: 2 additions & 3 deletions Sources/LiveKit/Core/Engine+SignalClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ extension Engine: SignalClientDelegate {
// engine is currently connected state
case .connected = _state.connectionState
{
log("[reconnect] starting, reason: socket network error. connectionState: \(_state.connectionState)")
Task {
try await startReconnect()
try await startReconnect(reason: .websocket)
}
}
}
Expand Down Expand Up @@ -87,7 +86,7 @@ extension Engine: SignalClientDelegate {
_state.mutate { $0.token = token }
}

func signalClient(_: SignalClient, didReceiveJoinResponse _: Livekit_JoinResponse) {}
func signalClient(_: SignalClient, didReceiveConnectResponse _: SignalClient.ConnectResponse) {}
func signalClient(_: SignalClient, didPublishLocalTrack _: Livekit_TrackPublishedResponse) {}
func signalClient(_: SignalClient, didUnpublishLocalTrack _: Livekit_TrackUnpublishedResponse) {}
func signalClient(_: SignalClient, didUpdateParticipants _: [Livekit_ParticipantInfo]) {}
Expand Down
11 changes: 7 additions & 4 deletions Sources/LiveKit/Core/Engine+TransportDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ extension Engine: TransportDelegate {
if _state.connectionState == .connected {
// Attempt re-connect if primary or publisher transport failed
if transport.isPrimary || (_state.hasPublished && transport.target == .publisher), [.disconnected, .failed].contains(pcState) {
log("[reconnect] starting, reason: transport disconnected or failed")
Task {
try await startReconnect()
try await startReconnect(reason: .transport)
}
}
}
}

func transport(_ transport: Transport, didGenerateIceCandidate iceCandidate: LKRTCIceCandidate) {
log("didGenerate iceCandidate")
Task {
try await signalClient.sendCandidate(candidate: iceCandidate, target: transport.target)
do {
log("sending iceCandidate")
try await signalClient.sendCandidate(candidate: iceCandidate, target: transport.target)
} catch {
log("Failed to send iceCandidate", .error)
}
}
}

Expand Down
213 changes: 127 additions & 86 deletions Sources/LiveKit/Core/Engine.swift

Large diffs are not rendered by default.

32 changes: 17 additions & 15 deletions Sources/LiveKit/Core/Room+SignalClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,27 @@ extension Room: SignalClientDelegate {
}
}

func signalClient(_: SignalClient, didReceiveJoinResponse joinResponse: Livekit_JoinResponse) {
log("\(joinResponse.serverInfo)", .info)
func signalClient(_: SignalClient, didReceiveConnectResponse connectResponse: SignalClient.ConnectResponse) {
if case let .join(joinResponse) = connectResponse {
log("\(joinResponse.serverInfo)", .info)

if e2eeManager != nil, !joinResponse.sifTrailer.isEmpty {
e2eeManager?.keyProvider.setSifTrailer(trailer: joinResponse.sifTrailer)
}
if e2eeManager != nil, !joinResponse.sifTrailer.isEmpty {
e2eeManager?.keyProvider.setSifTrailer(trailer: joinResponse.sifTrailer)
}

_state.mutate {
$0.sid = joinResponse.room.sid
$0.name = joinResponse.room.name
$0.metadata = joinResponse.room.metadata
$0.isRecording = joinResponse.room.activeRecording
$0.serverInfo = joinResponse.serverInfo
_state.mutate {
$0.sid = joinResponse.room.sid
$0.name = joinResponse.room.name
$0.metadata = joinResponse.room.metadata
$0.isRecording = joinResponse.room.activeRecording
$0.serverInfo = joinResponse.serverInfo

localParticipant.updateFromInfo(info: joinResponse.participant)
localParticipant.updateFromInfo(info: joinResponse.participant)

if !joinResponse.otherParticipants.isEmpty {
for otherParticipant in joinResponse.otherParticipants {
$0.updateRemoteParticipant(info: otherParticipant, room: self)
if !joinResponse.otherParticipants.isEmpty {
for otherParticipant in joinResponse.otherParticipants {
$0.updateRemoteParticipant(info: otherParticipant, room: self)
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/LiveKit/Core/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ public extension Room {
func sendSimulate(scenario: SimulateScenario) async throws {
try await engine.signalClient.sendSimulate(scenario: scenario)
}

func debug_triggerReconnect(reason: StartReconnectReason) async throws {
try await engine.startReconnect(reason: reason)
}
}

// MARK: - Session Migration
Expand Down
Loading

0 comments on commit ac357a9

Please sign in to comment.