Skip to content

Instantly share code, notes, and snippets.

View quangtqag's full-sized avatar
🚀
Researching things that I am lacking...

Quang quangtqag

🚀
Researching things that I am lacking...
View GitHub Profile
static func updateUser(id: Double, name: String, completionHandler: @escaping (_ userID: Double?, _ error: Error?) -> Void) {
let endpointUrl = URL(string: host + updateUserPath)!
var urlRequest = URLRequest(url: endpointUrl)
urlRequest.httpMethod = "PUT"
let userDict: [String: Any] = ["id": id, "name": name]
let userData = try! JSONSerialization.data(withJSONObject: userDict, options: [])
urlRequest.httpBody = userData
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
static func addUser(name: String, completionHandler: @escaping (_ userID: Double?, _ error: Error?) -> Void) {
let endpointUrl = URL(string: host + addUserPath)!
var urlRequest = URLRequest(url: endpointUrl)
urlRequest.httpMethod = "POST"
let newUser: [String: Any] = ["name": name]
let jsonUser = try! JSONSerialization.data(withJSONObject: newUser, options: [])
urlRequest.httpBody = jsonUser
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
static func deleteUser(id: Double, comletionHandler: @escaping (_ error: Error?) -> Void) {
let endpointUrl = URL(string: String(format: host + deleteUserPath, id))!
var urlRequest = URLRequest(url: endpointUrl)
urlRequest.httpMethod = "DELETE"
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
guard error == nil else {
print("error calling DELETE on \(deleteUserPath): \(error!)")
DispatchQueue.main.async {
comletionHandler(error)
static func getUsers(completionHandler: @escaping (_ users: [User]?, _ error: Error?) -> Void) {
let endpointUrl = URL(string: host + listUsersPath)!
let urlRequest = URLRequest(url: endpointUrl)
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
guard error == nil else {
print("error calling GET on \(listUsersPath): \(error!)")
DispatchQueue.main.async {
completionHandler(nil, error)
}
return
@IBAction func endCall(_ sender: Any) {
self.signalClient.deleteSdpAndCandidate(for: self.currentPerson)
self.webRTCClient.closePeerConnection()
self.webRTCClient.createPeerConnection()
self.hasLocalSdp = false
self.localCandidateCount = 0
self.hasRemoteSdp = false
self.remoteCandidateCount = 0
}
func answer(completion: @escaping (_ sdp: RTCSessionDescription) -> Void) {
let constrains = RTCMediaConstraints(mandatoryConstraints: self.mediaConstrains,
optionalConstraints: nil)
self.peerConnection!.answer(for: constrains) { (sdp, error) in
guard let sdp = sdp else {
return
}
self.peerConnection!.setLocalDescription(sdp, completionHandler: { (error) in
completion(sdp)
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
func send(candidate rtcIceCandidate: RTCIceCandidate, to person: String) {
do {
let dataMessage = try self.encoder.encode(IceCandidate(from: rtcIceCandidate))
let dict = try JSONSerialization.jsonObject(with: dataMessage, options: .allowFragments) as! [String: Any]
Firestore.firestore()
.collection(person)
.document("candidate")
.collection("candidates")
.addDocument(data: dict) { (err) in
if let err = err {
func offer(completion: @escaping (_ sdp: RTCSessionDescription) -> Void) {
let constrains = RTCMediaConstraints(
mandatoryConstraints: self.mediaConstrains,
optionalConstraints: nil)
self.peerConnection!.offer(for: constrains) { (sdp, error) in
guard let sdp = sdp else {
return
}
self.peerConnection!.setLocalDescription(sdp, completionHandler: { (error) in
private func createMediaSenders() {
let streamId = "stream"
// Audio
let audioTrack = self.createAudioTrack()
self.peerConnection!.add(audioTrack, streamIds: [streamId])
// Video
let videoTrack = self.createVideoTrack()
self.localVideoTrack = videoTrack