Created
May 11, 2019 01:35
-
-
Save quangtqag/79837037b33072f6956be908bd7a510f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func listenSdp(from person: String) { | |
Firestore.firestore().collection(person).document("sdp") | |
.addSnapshotListener { documentSnapshot, error in | |
guard let document = documentSnapshot else { | |
print("Error fetching sdp: \(error!)") | |
return | |
} | |
guard let data = document.data() else { | |
print("Firestore sdp data was empty.") | |
return | |
} | |
print("Firestore sdp data: \(data)") | |
do { | |
let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted) | |
let sessionDescription = try self.decoder.decode(SessionDescription.self, from: jsonData) | |
self.delegate?.signalClient(self, didReceiveRemoteSdp: sessionDescription.rtcSessionDescription) | |
} | |
catch { | |
debugPrint("Warning: Could not decode sdp data: \(error)") | |
return | |
} | |
} | |
} | |
func listenCandidate(from person: String) { | |
Firestore.firestore() | |
.collection(person) | |
.document("candidate") | |
.collection("candidates") | |
.addSnapshotListener { (querySnapshot, err) in | |
guard let documents = querySnapshot?.documents else { | |
print("Error fetching documents: \(err!)") | |
return | |
} | |
querySnapshot!.documentChanges.forEach { diff in | |
if (diff.type == .added) { | |
do { | |
let jsonData = try JSONSerialization.data(withJSONObject: documents.first!.data(), options: .prettyPrinted) | |
let iceCandidate = try self.decoder.decode(IceCandidate.self, from: jsonData) | |
self.delegate?.signalClient(self, didReceiveCandidate: iceCandidate.rtcIceCandidate) | |
} | |
catch { | |
debugPrint("Warning: Could not decode candidate data: \(error)") | |
return | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment