Skip to content

Commit

Permalink
resolves jocosocial#253, a workaround for websocket crash
Browse files Browse the repository at this point in the history
  • Loading branch information
cohoe committed Jan 18, 2024
1 parent e06119d commit 131f442
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions Sources/swiftarr/Controllers/PhonecallController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,25 @@ struct PhonecallController: APIRouteCollection {
callerSocket: ws,
notifySockets: calleeNotificationSockets.map { $0.socket }
)
ws.onClose.whenComplete { result in
endPhoneCall(callID: callID)
}
ws.onBinary { ws, binary in
Task {
if let call = await ActivePhoneCalls.shared.getCall(withID: callID),
let calleeSocket = call.calleeSocket
{
try? await calleeSocket.send([UInt8](buffer: binary))

// https://github.com/jocosocial/swiftarr/issues/253
// https://github.com/vapor/websocket-kit/issues/139
// https://github.com/vapor/websocket-kit/issues/140
ws.eventLoop.execute {
ws.onClose.whenComplete { result in
endPhoneCall(callID: callID)
}
ws.onBinary { ws, binary in
Task {
if let call = await ActivePhoneCalls.shared.getCall(withID: callID),
let calleeSocket = call.calleeSocket
{
try? await calleeSocket.send([UInt8](buffer: binary))
}
}
}
}

}

/// `GET /api/v3/phone/socket/answer/:call_id`
Expand Down Expand Up @@ -338,14 +345,19 @@ struct PhonecallController: APIRouteCollection {
try? await call.calleeSocket?.send(raw: jsonData, opcode: .binary)
}

ws.onClose.whenComplete { result in
endPhoneCall(callID: callID)
}
// https://github.com/jocosocial/swiftarr/issues/253
// https://github.com/vapor/websocket-kit/issues/139
// https://github.com/vapor/websocket-kit/issues/140
ws.eventLoop.execute {
ws.onClose.whenComplete { result in
endPhoneCall(callID: callID)
}

ws.onBinary { ws, binary in
Task {
if let call = await ActivePhoneCalls.shared.getCall(withID: callID) {
try? await call.callerSocket?.send([UInt8](buffer: binary))
ws.onBinary { ws, binary in
Task {
if let call = await ActivePhoneCalls.shared.getCall(withID: callID) {
try? await call.callerSocket?.send([UInt8](buffer: binary))
}
}
}
}
Expand Down

0 comments on commit 131f442

Please sign in to comment.