voip-callkit 0.0.5 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/Plugin/CallKitVoipPlugin.swift +183 -16
- package/package.json +1 -1
@@ -3,19 +3,23 @@ import Capacitor
|
|
3
3
|
import UIKit
|
4
4
|
import CallKit
|
5
5
|
import PushKit
|
6
|
-
|
6
|
+
import SwiftSignalRClient
|
7
7
|
/**
|
8
8
|
* CallKit Voip Plugin provides native PushKit functionality with apple CallKit to ionic capacitor
|
9
9
|
*/
|
10
|
+
@available(iOS 13.0, *)
|
10
11
|
@objc(CallKitVoipPlugin)
|
11
12
|
public class CallKitVoipPlugin: CAPPlugin {
|
12
13
|
|
13
14
|
private var provider: CXProvider?
|
14
15
|
private let voipRegistry = PKPushRegistry(queue: nil)
|
15
16
|
private var connectionIdRegistry : [UUID: CallConfig] = [:]
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
private var urlString = "wss://demo.piesocket.com/v3/channel_1?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV¬ify_self"
|
18
|
+
var webSocket: URLSessionWebSocketTask?
|
19
|
+
var callUUID:UUID?
|
20
|
+
private let serverUrl = "http://192.168.86.115:5000/chat"
|
21
|
+
private var chatHubConnection: HubConnection?
|
22
|
+
private var chatHubConnectionDelegate: HubConnectionDelegate?
|
19
23
|
@objc func register(_ call: CAPPluginCall) {
|
20
24
|
// config PushKit
|
21
25
|
voipRegistry.delegate = self
|
@@ -30,11 +34,11 @@ public class CallKitVoipPlugin: CAPPlugin {
|
|
30
34
|
|
31
35
|
call.resolve()
|
32
36
|
}
|
33
|
-
|
37
|
+
|
34
38
|
@objc func incomingCall(_ call: CAPPluginCall) {
|
35
39
|
// TODO
|
36
40
|
}
|
37
|
-
|
41
|
+
|
38
42
|
public func notifyEvent(eventName: String, uuid: UUID){
|
39
43
|
if let config = connectionIdRegistry[uuid] {
|
40
44
|
notifyListeners(eventName, data: [
|
@@ -45,7 +49,7 @@ public class CallKitVoipPlugin: CAPPlugin {
|
|
45
49
|
}
|
46
50
|
}
|
47
51
|
|
48
|
-
public func incommingCall(from: String, connectionId: String) {
|
52
|
+
public func incommingCall(from: String, connectionId: String,uuid:UUID) {
|
49
53
|
let update = CXCallUpdate()
|
50
54
|
update.remoteHandle = CXHandle(type: .generic, value: from)
|
51
55
|
update.hasVideo = true
|
@@ -55,13 +59,13 @@ public class CallKitVoipPlugin: CAPPlugin {
|
|
55
59
|
update.supportsUngrouping = false
|
56
60
|
update.hasVideo = true
|
57
61
|
|
58
|
-
let uuid = UUID()
|
62
|
+
//let uuid = UUID()
|
59
63
|
connectionIdRegistry[uuid] = .init(connectionId: connectionId, username: from)
|
60
64
|
self.provider?.reportNewIncomingCall(with: uuid, update: update, completion: { (_) in })
|
61
65
|
}
|
62
66
|
|
63
67
|
|
64
|
-
|
68
|
+
|
65
69
|
|
66
70
|
public func endCall(uuid: UUID) {
|
67
71
|
let controller = CXCallController()
|
@@ -69,11 +73,127 @@ public class CallKitVoipPlugin: CAPPlugin {
|
|
69
73
|
CXEndCallAction(call: uuid));controller.request(transaction,completion: { error in })
|
70
74
|
}
|
71
75
|
|
76
|
+
private func openHubConnection(){
|
77
|
+
self.chatHubConnectionDelegate = ChatHubConnectionDelegate(controller: self)
|
78
|
+
self.chatHubConnection = HubConnectionBuilder(url: URL(string: self.serverUrl)!)
|
79
|
+
.withLogging(minLogLevel: .debug)
|
80
|
+
.withAutoReconnect()
|
81
|
+
.withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!)
|
82
|
+
.build()
|
83
|
+
|
84
|
+
self.chatHubConnection!.on(method: "BroadcastMessage", callback: {(type: String, message: String) in
|
85
|
+
print("type : \(type) and mes : \(message)")
|
86
|
+
})
|
87
|
+
self.chatHubConnection!.start()
|
88
|
+
}
|
89
|
+
|
90
|
+
private func openWebSocket() {
|
91
|
+
if let url = URL(string: urlString) {
|
92
|
+
let request = URLRequest(url: url)
|
93
|
+
let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
|
94
|
+
let webSocket = session.webSocketTask(with: request)
|
95
|
+
self.webSocket = webSocket
|
96
|
+
//self.opened = true
|
97
|
+
print("open web socket called")
|
98
|
+
self.webSocket?.resume()
|
99
|
+
} else {
|
100
|
+
webSocket = nil
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
func closeSocket() {
|
105
|
+
webSocket?.cancel(with: .goingAway, reason: nil)
|
106
|
+
// opened = false
|
107
|
+
webSocket = nil
|
108
|
+
}
|
109
|
+
func recieve(){
|
110
|
+
webSocket?.receive(completionHandler: { [weak self] result in
|
111
|
+
switch result {
|
112
|
+
|
113
|
+
case .success(let message):
|
114
|
+
|
115
|
+
switch message {
|
116
|
+
case .string(let str):
|
117
|
+
if str.contains("video closed") {
|
118
|
+
if let uuid = self?.callUUID{
|
119
|
+
print("timeout func1")
|
120
|
+
self?.endCall(uuid:uuid)
|
121
|
+
}
|
122
|
+
}
|
123
|
+
print("got message str \(str)")
|
124
|
+
case .data(let data):
|
125
|
+
print("got message data \(data)")
|
126
|
+
default:
|
127
|
+
break
|
128
|
+
}
|
129
|
+
case .failure(let error):
|
130
|
+
print("failed reason \(error)")
|
131
|
+
}
|
132
|
+
self?.recieve()
|
133
|
+
})
|
134
|
+
}
|
135
|
+
func send(){
|
136
|
+
DispatchQueue.global().asyncAfter(deadline: .now()+2){
|
137
|
+
self.webSocket?.send(.string("video closed:\("user declined")"), completionHandler: { error in
|
138
|
+
if let error = error {
|
139
|
+
print("error sending message \(error)")
|
140
|
+
}
|
141
|
+
})
|
142
|
+
}
|
143
|
+
|
144
|
+
}
|
145
|
+
|
146
|
+
func sendMessageToHub(){
|
147
|
+
DispatchQueue.global().asyncAfter(deadline: .now()+2){
|
148
|
+
self.chatHubConnection?.invoke(method: "sendMessage", ["type":"message","message":"video ended"]) { error in
|
149
|
+
if let e = error {
|
150
|
+
print(e)
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
fileprivate func connectionDidOpen() {
|
157
|
+
|
158
|
+
}
|
159
|
+
|
160
|
+
fileprivate func connectionDidFailToOpen(error: Error) {
|
161
|
+
|
162
|
+
}
|
163
|
+
|
164
|
+
fileprivate func connectionDidClose(error: Error?) {
|
165
|
+
|
166
|
+
}
|
167
|
+
|
168
|
+
fileprivate func connectionWillReconnect(error: Error?) {
|
169
|
+
|
170
|
+
}
|
171
|
+
|
172
|
+
fileprivate func connectionDidReconnect() {
|
173
|
+
|
174
|
+
}
|
175
|
+
|
72
176
|
}
|
73
177
|
|
74
178
|
|
75
|
-
// MARK: CallKit events handler
|
76
179
|
|
180
|
+
@available(iOS 13.0, *)
|
181
|
+
extension CallKitVoipPlugin: URLSessionWebSocketDelegate {
|
182
|
+
public func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) {
|
183
|
+
// opened = true
|
184
|
+
print("did connect to server")
|
185
|
+
//self.recieve()
|
186
|
+
}
|
187
|
+
|
188
|
+
|
189
|
+
public func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) {
|
190
|
+
self.webSocket = nil
|
191
|
+
//self.opened = false
|
192
|
+
print(" did close connection")
|
193
|
+
}
|
194
|
+
}
|
195
|
+
// MARK: CallKit events handler
|
196
|
+
@available(iOS 13.0, *)
|
77
197
|
extension CallKitVoipPlugin: CXProviderDelegate {
|
78
198
|
|
79
199
|
public func providerDidReset(_ provider: CXProvider) {
|
@@ -83,21 +203,26 @@ extension CallKitVoipPlugin: CXProviderDelegate {
|
|
83
203
|
public func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
|
84
204
|
print("call answered")
|
85
205
|
notifyEvent(eventName: "callAnswered", uuid: action.callUUID)
|
86
|
-
// endCall(uuid: action.callUUID)
|
206
|
+
// endCall(uuid: action.callUUID)
|
207
|
+
//closeSocket()
|
87
208
|
action.fulfill()
|
88
209
|
}
|
89
210
|
|
90
211
|
public func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
|
212
|
+
//closeSocket()
|
91
213
|
action.fulfill()
|
92
214
|
}
|
93
215
|
|
94
216
|
public func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
|
95
217
|
notifyEvent(eventName: "callStarted", uuid: action.callUUID)
|
218
|
+
callUUID = action.callUUID
|
219
|
+
//send()
|
96
220
|
action.fulfill()
|
97
221
|
}
|
98
222
|
}
|
99
223
|
|
100
224
|
// MARK: PushKit events handler
|
225
|
+
@available(iOS 13.0, *)
|
101
226
|
extension CallKitVoipPlugin: PKPushRegistryDelegate {
|
102
227
|
|
103
228
|
public func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
|
@@ -108,22 +233,64 @@ extension CallKitVoipPlugin: PKPushRegistryDelegate {
|
|
108
233
|
|
109
234
|
public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
|
110
235
|
|
111
|
-
// guard let connectionId = payload.dictionaryPayload["ConnectionId"] as? String else {
|
112
|
-
// return
|
113
|
-
// }
|
236
|
+
// guard let connectionId = payload.dictionaryPayload["ConnectionId"] as? String else {
|
237
|
+
// return
|
238
|
+
// }
|
114
239
|
|
115
|
-
|
240
|
+
// let username = (payload.dictionaryPayload["Username"] as? String) ?? "Anonymus"
|
116
241
|
|
242
|
+
let backgroundTaskIdentifier =
|
243
|
+
UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
|
244
|
+
let uuid = UUID()
|
245
|
+
self.callUUID = uuid
|
246
|
+
// openWebSocket()
|
247
|
+
openHubConnection()
|
248
|
+
self.incommingCall(from: "username", connectionId: "123",uuid:uuid)
|
117
249
|
|
118
|
-
|
250
|
+
DispatchQueue.global().asyncAfter(deadline: .now()+20){
|
251
|
+
print("timeout func")
|
252
|
+
|
253
|
+
self.sendMessageToHub()
|
254
|
+
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
|
255
|
+
}
|
119
256
|
}
|
120
257
|
|
121
258
|
}
|
122
259
|
|
123
260
|
|
261
|
+
@available(iOS 13.0, *)
|
124
262
|
extension CallKitVoipPlugin {
|
125
263
|
struct CallConfig {
|
126
264
|
let connectionId: String
|
127
265
|
let username : String
|
128
266
|
}
|
129
267
|
}
|
268
|
+
@available(iOS 13.0, *)
|
269
|
+
class ChatHubConnectionDelegate: HubConnectionDelegate {
|
270
|
+
|
271
|
+
weak var controller: CallKitVoipPlugin?
|
272
|
+
|
273
|
+
init(controller: CallKitVoipPlugin) {
|
274
|
+
self.controller = controller
|
275
|
+
}
|
276
|
+
|
277
|
+
func connectionDidOpen(hubConnection: HubConnection) {
|
278
|
+
controller?.connectionDidOpen()
|
279
|
+
}
|
280
|
+
|
281
|
+
func connectionDidFailToOpen(error: Error) {
|
282
|
+
controller?.connectionDidFailToOpen(error: error)
|
283
|
+
}
|
284
|
+
|
285
|
+
func connectionDidClose(error: Error?) {
|
286
|
+
controller?.connectionDidClose(error: error)
|
287
|
+
}
|
288
|
+
|
289
|
+
func connectionWillReconnect(error: Error) {
|
290
|
+
controller?.connectionWillReconnect(error: error)
|
291
|
+
}
|
292
|
+
|
293
|
+
func connectionDidReconnect() {
|
294
|
+
controller?.connectionDidReconnect()
|
295
|
+
}
|
296
|
+
}
|