Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Created May 11, 2019 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quangtqag/79837037b33072f6956be908bd7a510f to your computer and use it in GitHub Desktop.
Save quangtqag/79837037b33072f6956be908bd7a510f to your computer and use it in GitHub Desktop.
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