@livechatta/sdk 0.0.0-stage → 1.0.0
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/CHANGELOG.md +218 -0
- package/LICENSE +21 -0
- package/README.md +2558 -2
- package/bin/chatta.mjs +7 -0
- package/bin/lib/cli.d.mts +46 -0
- package/bin/lib/cli.mjs +2088 -0
- package/dist/cjs/livechatta.cjs.js +7258 -0
- package/dist/cjs/livechatta.cjs.js.map +1 -0
- package/dist/cjs/livechatta.cjs.min.js +2 -0
- package/dist/cjs/livechatta.cjs.min.js.map +1 -0
- package/dist/esm/livechatta.esm.js +7077 -0
- package/dist/esm/livechatta.esm.js.map +1 -0
- package/dist/esm/livechatta.esm.min.js +2 -0
- package/dist/esm/livechatta.esm.min.js.map +1 -0
- package/dist/platforms/android/LiveChattaClient.kt +367 -0
- package/dist/platforms/android/LiveChattaHttpAPI.kt +2630 -0
- package/dist/platforms/android/LiveChattaJson.kt +83 -0
- package/dist/platforms/android/LiveChattaModels.kt +22805 -0
- package/dist/platforms/android/LiveChattaSentFrom.kt +30 -0
- package/dist/platforms/android/LiveChattaSocketAPI.kt +2113 -0
- package/dist/platforms/android/LiveChattaTyped.kt +239 -0
- package/dist/platforms/android/LiveChattaWebView.kt +158 -0
- package/dist/platforms/angular/livechatta-bridge.service.d.ts +35 -0
- package/dist/platforms/angular/livechatta-bridge.service.js +1 -0
- package/dist/platforms/angular/livechatta-bridge.service.js.map +1 -0
- package/dist/platforms/angular/livechatta-embed.component.d.ts +46 -0
- package/dist/platforms/angular/livechatta-embed.component.js +1 -0
- package/dist/platforms/angular/livechatta-embed.component.js.map +1 -0
- package/dist/platforms/angular/livechatta.service.d.ts +66 -0
- package/dist/platforms/angular/livechatta.service.js +1 -0
- package/dist/platforms/angular/livechatta.service.js.map +1 -0
- package/dist/platforms/capacitor/CapacitorBridge.d.ts +27 -0
- package/dist/platforms/capacitor/CapacitorBridge.js +1 -0
- package/dist/platforms/capacitor/CapacitorBridge.js.map +1 -0
- package/dist/platforms/capacitor/CapacitorDeepLinks.d.ts +23 -0
- package/dist/platforms/capacitor/CapacitorDeepLinks.js +1 -0
- package/dist/platforms/capacitor/CapacitorDeepLinks.js.map +1 -0
- package/dist/platforms/capacitor/CapacitorPermissions.d.ts +29 -0
- package/dist/platforms/capacitor/CapacitorPermissions.js +1 -0
- package/dist/platforms/capacitor/CapacitorPermissions.js.map +1 -0
- package/dist/platforms/capacitor/CapacitorPush.d.ts +33 -0
- package/dist/platforms/capacitor/CapacitorPush.js +1 -0
- package/dist/platforms/capacitor/CapacitorPush.js.map +1 -0
- package/dist/platforms/capacitor/index.d.ts +24 -0
- package/dist/platforms/capacitor/index.js +1 -0
- package/dist/platforms/capacitor/index.js.map +1 -0
- package/dist/platforms/flutter/livechatta_client.dart +359 -0
- package/dist/platforms/flutter/livechatta_http_api.dart +1942 -0
- package/dist/platforms/flutter/livechatta_json.dart +57 -0
- package/dist/platforms/flutter/livechatta_models.dart +23531 -0
- package/dist/platforms/flutter/livechatta_sent_from.dart +35 -0
- package/dist/platforms/flutter/livechatta_socket_api.dart +1504 -0
- package/dist/platforms/flutter/livechatta_typed.dart +234 -0
- package/dist/platforms/flutter/livechatta_webview.dart +190 -0
- package/dist/platforms/ios/LiveChattaClient.swift +374 -0
- package/dist/platforms/ios/LiveChattaHttpAPI.swift +2431 -0
- package/dist/platforms/ios/LiveChattaJSON.swift +84 -0
- package/dist/platforms/ios/LiveChattaModels.swift +27050 -0
- package/dist/platforms/ios/LiveChattaSentFrom.swift +32 -0
- package/dist/platforms/ios/LiveChattaSocketAPI.swift +1446 -0
- package/dist/platforms/ios/LiveChattaTyped.swift +268 -0
- package/dist/platforms/ios/LiveChattaWebView.swift +191 -0
- package/dist/platforms/nuxt/LiveChattaView.d.ts +1 -0
- package/dist/platforms/nuxt/LiveChattaView.js +1 -0
- package/dist/platforms/nuxt/LiveChattaView.js.map +1 -0
- package/dist/platforms/nuxt/useChattaBridge.d.ts +9 -0
- package/dist/platforms/nuxt/useChattaBridge.js +1 -0
- package/dist/platforms/nuxt/useChattaBridge.js.map +1 -0
- package/dist/platforms/nuxt/useLiveChatta.d.ts +63 -0
- package/dist/platforms/nuxt/useLiveChatta.js +1 -0
- package/dist/platforms/nuxt/useLiveChatta.js.map +1 -0
- package/dist/platforms/react/LiveChattaView.d.ts +58 -0
- package/dist/platforms/react/LiveChattaView.js +1 -0
- package/dist/platforms/react/LiveChattaView.js.map +1 -0
- package/dist/platforms/react/useChattaBridge.d.ts +1 -0
- package/dist/platforms/react/useChattaBridge.js +1 -0
- package/dist/platforms/react/useChattaBridge.js.map +1 -0
- package/dist/platforms/react/useChattaDeepLink.d.ts +2 -0
- package/dist/platforms/react/useChattaDeepLink.js +1 -0
- package/dist/platforms/react/useChattaDeepLink.js.map +1 -0
- package/dist/platforms/react/useChattaPermissions.d.ts +9 -0
- package/dist/platforms/react/useChattaPermissions.js +1 -0
- package/dist/platforms/react/useChattaPermissions.js.map +1 -0
- package/dist/platforms/react/useChattaUnread.d.ts +1 -0
- package/dist/platforms/react/useChattaUnread.js +1 -0
- package/dist/platforms/react/useChattaUnread.js.map +1 -0
- package/dist/platforms/react/useLiveChatta.d.ts +29 -0
- package/dist/platforms/react/useLiveChatta.js +1 -0
- package/dist/platforms/react/useLiveChatta.js.map +1 -0
- package/dist/platforms/react-native/LiveChattaView.d.ts +52 -0
- package/dist/platforms/react-native/LiveChattaView.js +1 -0
- package/dist/platforms/react-native/LiveChattaView.js.map +1 -0
- package/dist/platforms/react-native/useChattaBridge.d.ts +12 -0
- package/dist/platforms/react-native/useChattaBridge.js +1 -0
- package/dist/platforms/react-native/useChattaBridge.js.map +1 -0
- package/dist/platforms/react-native/useLiveChatta.d.ts +29 -0
- package/dist/platforms/react-native/useLiveChatta.js +1 -0
- package/dist/platforms/react-native/useLiveChatta.js.map +1 -0
- package/dist/platforms/svelte/LiveChattaEmbed.d.ts +54 -0
- package/dist/platforms/svelte/LiveChattaEmbed.js +1 -0
- package/dist/platforms/svelte/LiveChattaEmbed.js.map +1 -0
- package/dist/platforms/svelte/chattaBridgeStore.d.ts +19 -0
- package/dist/platforms/svelte/chattaBridgeStore.js +1 -0
- package/dist/platforms/svelte/chattaBridgeStore.js.map +1 -0
- package/dist/platforms/svelte/useLiveChatta.d.ts +61 -0
- package/dist/platforms/svelte/useLiveChatta.js +1 -0
- package/dist/platforms/svelte/useLiveChatta.js.map +1 -0
- package/dist/platforms/tauri/TauriBridge.d.ts +34 -0
- package/dist/platforms/tauri/TauriBridge.js +1 -0
- package/dist/platforms/tauri/TauriBridge.js.map +1 -0
- package/dist/platforms/tauri/TauriDeepLinks.d.ts +29 -0
- package/dist/platforms/tauri/TauriDeepLinks.js +1 -0
- package/dist/platforms/tauri/TauriDeepLinks.js.map +1 -0
- package/dist/platforms/tauri/TauriPermissions.d.ts +21 -0
- package/dist/platforms/tauri/TauriPermissions.js +1 -0
- package/dist/platforms/tauri/TauriPermissions.js.map +1 -0
- package/dist/platforms/tauri/TauriUpdater.d.ts +31 -0
- package/dist/platforms/tauri/TauriUpdater.js +1 -0
- package/dist/platforms/tauri/TauriUpdater.js.map +1 -0
- package/dist/platforms/tauri/index.d.ts +22 -0
- package/dist/platforms/tauri/index.js +1 -0
- package/dist/platforms/tauri/index.js.map +1 -0
- package/dist/platforms/vue/LiveChattaView.d.ts +1 -0
- package/dist/platforms/vue/LiveChattaView.js +1 -0
- package/dist/platforms/vue/LiveChattaView.js.map +1 -0
- package/dist/platforms/vue/useChattaBridge.d.ts +11 -0
- package/dist/platforms/vue/useChattaBridge.js +1 -0
- package/dist/platforms/vue/useChattaBridge.js.map +1 -0
- package/dist/platforms/vue/useChattaDeepLink.d.ts +2 -0
- package/dist/platforms/vue/useChattaDeepLink.js +1 -0
- package/dist/platforms/vue/useChattaDeepLink.js.map +1 -0
- package/dist/platforms/vue/useChattaPermissions.d.ts +9 -0
- package/dist/platforms/vue/useChattaPermissions.js +1 -0
- package/dist/platforms/vue/useChattaPermissions.js.map +1 -0
- package/dist/platforms/vue/useChattaUnread.d.ts +8 -0
- package/dist/platforms/vue/useChattaUnread.js +1 -0
- package/dist/platforms/vue/useChattaUnread.js.map +1 -0
- package/dist/platforms/vue/useLiveChatta.d.ts +58 -0
- package/dist/platforms/vue/useLiveChatta.js +1 -0
- package/dist/platforms/vue/useLiveChatta.js.map +1 -0
- package/dist/platforms/web/LiveChattaEmbed.d.ts +94 -0
- package/dist/platforms/web/LiveChattaEmbed.js +1 -0
- package/dist/platforms/web/LiveChattaEmbed.js.map +1 -0
- package/dist/platforms/web/bridge.d.ts +8 -0
- package/dist/platforms/web/bridge.js +1 -0
- package/dist/platforms/web/bridge.js.map +1 -0
- package/dist/platforms/web/permissions.d.ts +8 -0
- package/dist/platforms/web/permissions.js +1 -0
- package/dist/platforms/web/permissions.js.map +1 -0
- package/dist/types/billing.d.ts +355 -0
- package/dist/types/billing.js +53 -0
- package/dist/types/billing.js.map +1 -0
- package/dist/types/calendar.d.ts +132 -0
- package/dist/types/calendar.js +15 -0
- package/dist/types/calendar.js.map +1 -0
- package/dist/types/chat-core.d.ts +901 -0
- package/dist/types/chat-core.js +98 -0
- package/dist/types/chat-core.js.map +1 -0
- package/dist/types/chat-features.d.ts +775 -0
- package/dist/types/chat-features.js +50 -0
- package/dist/types/chat-features.js.map +1 -0
- package/dist/types/content.d.ts +138 -0
- package/dist/types/content.js +2 -0
- package/dist/types/content.js.map +1 -0
- package/dist/types/core/AutoRefresh.d.ts +68 -0
- package/dist/types/core/Bridge.d.ts +124 -0
- package/dist/types/core/Config.d.ts +26 -0
- package/dist/types/core/DeepLinks.d.ts +38 -0
- package/dist/types/core/EventBus.d.ts +15 -0
- package/dist/types/core/GrantStore.d.ts +29 -0
- package/dist/types/core/LiveChatta.d.ts +128 -0
- package/dist/types/core/Permissions.d.ts +34 -0
- package/dist/types/core/TokenManager.d.ts +25 -0
- package/dist/types/core/events.d.ts +140 -0
- package/dist/types/csat.d.ts +110 -0
- package/dist/types/csat.js +12 -0
- package/dist/types/csat.js.map +1 -0
- package/dist/types/events.d.ts +242 -0
- package/dist/types/events.js +275 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/extras.d.ts +353 -0
- package/dist/types/extras.js +31 -0
- package/dist/types/extras.js.map +1 -0
- package/dist/types/http/HttpAPI.d.ts +944 -0
- package/dist/types/http/HttpTransport.d.ts +66 -0
- package/dist/types/http/incomingWebhook.d.ts +17 -0
- package/dist/types/incoming-webhooks.d.ts +78 -0
- package/dist/types/incoming-webhooks.js +16 -0
- package/dist/types/incoming-webhooks.js.map +1 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/index.js +16 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/liveflex.d.ts +578 -0
- package/dist/types/liveflex.js +7 -0
- package/dist/types/liveflex.js.map +1 -0
- package/dist/types/models.d.ts +1753 -0
- package/dist/types/models.js +148 -0
- package/dist/types/models.js.map +1 -0
- package/dist/types/options.d.ts +190 -0
- package/dist/types/options.js +43 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/shared-channels.d.ts +236 -0
- package/dist/types/shared-channels.js +50 -0
- package/dist/types/shared-channels.js.map +1 -0
- package/dist/types/socket/SocketAPI.d.ts +1026 -0
- package/dist/types/socket/SocketTransport.d.ts +44 -0
- package/dist/types/types/billing.d.ts +355 -0
- package/dist/types/types/calendar.d.ts +132 -0
- package/dist/types/types/chat-core.d.ts +901 -0
- package/dist/types/types/chat-features.d.ts +775 -0
- package/dist/types/types/content.d.ts +138 -0
- package/dist/types/types/csat.d.ts +110 -0
- package/dist/types/types/events.d.ts +242 -0
- package/dist/types/types/extras.d.ts +353 -0
- package/dist/types/types/incoming-webhooks.d.ts +78 -0
- package/dist/types/types/index.d.ts +15 -0
- package/dist/types/types/liveflex.d.ts +578 -0
- package/dist/types/types/models.d.ts +1753 -0
- package/dist/types/types/options.d.ts +190 -0
- package/dist/types/types/shared-channels.d.ts +236 -0
- package/dist/types/types/workspace-gifs.d.ts +95 -0
- package/dist/types/types/workspace.d.ts +390 -0
- package/dist/types/ui/EmbedConfig.d.ts +38 -0
- package/dist/types/ui/EmbedUI.d.ts +44 -0
- package/dist/types/ui/Launcher.d.ts +122 -0
- package/dist/types/ui/brand.d.ts +27 -0
- package/dist/types/ui/embedMessages.d.ts +23 -0
- package/dist/types/ui/hours.d.ts +35 -0
- package/dist/types/ui/prechat.d.ts +35 -0
- package/dist/types/ui/widgetConfig.d.ts +4 -0
- package/dist/types/utils/crypto.d.ts +6 -0
- package/dist/types/utils/hmac.d.ts +40 -0
- package/dist/types/utils/permissions.d.ts +5 -0
- package/dist/types/utils/platform.d.ts +18 -0
- package/dist/types/utils/visitor.d.ts +2 -0
- package/dist/types/workspace-gifs.d.ts +95 -0
- package/dist/types/workspace-gifs.js +3 -0
- package/dist/types/workspace-gifs.js.map +1 -0
- package/dist/types/workspace.d.ts +390 -0
- package/dist/types/workspace.js +13 -0
- package/dist/types/workspace.js.map +1 -0
- package/dist/umd/livechatta.umd.js +7262 -0
- package/dist/umd/livechatta.umd.js.map +1 -0
- package/dist/umd/livechatta.umd.min.js +2 -0
- package/dist/umd/livechatta.umd.min.js.map +1 -0
- package/package.json +278 -4
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Security
|
|
3
|
+
import SocketIO
|
|
4
|
+
|
|
5
|
+
// iOS headless client: initialize(options:) then sock() (Socket.IO-Client-Swift) and api().
|
|
6
|
+
// Usage examples: README "iOS (Swift)".
|
|
7
|
+
|
|
8
|
+
// MARK: - Configuration
|
|
9
|
+
|
|
10
|
+
public struct LiveChattaOptions {
|
|
11
|
+
/// Required — chat link code
|
|
12
|
+
public let linkCode: String
|
|
13
|
+
/// Required — API key for authentication
|
|
14
|
+
public let apiKey: String
|
|
15
|
+
/// Required — RSA public key (PEM) for encrypting tokens
|
|
16
|
+
public let publicKey: String
|
|
17
|
+
/// Base URL of the LiveChatta server
|
|
18
|
+
public let baseUrl: String
|
|
19
|
+
/// Optional — pre-registered chat username
|
|
20
|
+
public let chatUsername: String?
|
|
21
|
+
/// Enable WebSocket transport
|
|
22
|
+
public let enableSocket: Bool
|
|
23
|
+
/// Enable HTTP transport
|
|
24
|
+
public let enableHttp: Bool
|
|
25
|
+
/// Enable debug logging
|
|
26
|
+
public let debug: Bool
|
|
27
|
+
/// Optional owner account token (JWT) for owner-console HTTP methods
|
|
28
|
+
public let ownerToken: String?
|
|
29
|
+
/// Optional chat-member token (JWT) for member HTTP routes (mute, reminders, …)
|
|
30
|
+
public let memberToken: String?
|
|
31
|
+
|
|
32
|
+
public init(
|
|
33
|
+
linkCode: String,
|
|
34
|
+
apiKey: String,
|
|
35
|
+
publicKey: String,
|
|
36
|
+
baseUrl: String = "https://api.livechatta.com",
|
|
37
|
+
chatUsername: String? = nil,
|
|
38
|
+
enableSocket: Bool = true,
|
|
39
|
+
enableHttp: Bool = true,
|
|
40
|
+
debug: Bool = false,
|
|
41
|
+
ownerToken: String? = nil,
|
|
42
|
+
memberToken: String? = nil
|
|
43
|
+
) {
|
|
44
|
+
self.linkCode = linkCode
|
|
45
|
+
self.apiKey = apiKey
|
|
46
|
+
self.publicKey = publicKey
|
|
47
|
+
self.baseUrl = baseUrl
|
|
48
|
+
self.chatUsername = chatUsername
|
|
49
|
+
self.enableSocket = enableSocket
|
|
50
|
+
self.enableHttp = enableHttp
|
|
51
|
+
self.debug = debug
|
|
52
|
+
self.ownerToken = ownerToken
|
|
53
|
+
self.memberToken = memberToken
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public enum LiveChattaConnectionState: String {
|
|
58
|
+
case disconnected = "DISCONNECTED"
|
|
59
|
+
case connecting = "CONNECTING"
|
|
60
|
+
case connected = "CONNECTED"
|
|
61
|
+
case reconnecting = "RECONNECTING"
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// MARK: - Client
|
|
65
|
+
|
|
66
|
+
public class LiveChattaClient {
|
|
67
|
+
|
|
68
|
+
private var options: LiveChattaOptions?
|
|
69
|
+
private var manager: SocketManager?
|
|
70
|
+
private var socket: SocketIOClient?
|
|
71
|
+
private var socketAPI: LiveChattaSocketAPI?
|
|
72
|
+
private var httpAPI: LiveChattaHttpAPI?
|
|
73
|
+
private(set) public var connectionState: LiveChattaConnectionState = .disconnected
|
|
74
|
+
private(set) public var isInitialized: Bool = false
|
|
75
|
+
|
|
76
|
+
private var lifecycleListeners: [String: [(Any?) -> Void]] = [:]
|
|
77
|
+
private var reconnectAttempt = 0
|
|
78
|
+
|
|
79
|
+
// SDK lifecycle events
|
|
80
|
+
public static let eventConnected = "sdk:connected"
|
|
81
|
+
public static let eventDisconnected = "sdk:disconnected"
|
|
82
|
+
public static let eventReconnecting = "sdk:reconnecting"
|
|
83
|
+
public static let eventReconnected = "sdk:reconnected"
|
|
84
|
+
public static let eventError = "sdk:error"
|
|
85
|
+
public static let eventQueueFlush = "sdk:queue-flush"
|
|
86
|
+
|
|
87
|
+
public init() {}
|
|
88
|
+
|
|
89
|
+
/// Initialize the SDK with connection options.
|
|
90
|
+
@discardableResult
|
|
91
|
+
public func initialize(options: LiveChattaOptions) -> LiveChattaClient {
|
|
92
|
+
precondition(!options.linkCode.isEmpty, "LiveChatta: linkCode is required")
|
|
93
|
+
precondition(!options.apiKey.isEmpty, "LiveChatta: apiKey is required")
|
|
94
|
+
precondition(!options.publicKey.isEmpty, "LiveChatta: publicKey is required")
|
|
95
|
+
|
|
96
|
+
self.options = options
|
|
97
|
+
|
|
98
|
+
if options.enableHttp {
|
|
99
|
+
let baseUrl = options.baseUrl.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
|
|
100
|
+
httpAPI = LiveChattaHttpAPI(
|
|
101
|
+
baseUrl: "\(baseUrl)/api/v1/external",
|
|
102
|
+
apiKey: options.apiKey,
|
|
103
|
+
debug: options.debug
|
|
104
|
+
)
|
|
105
|
+
httpAPI?.ownerToken = options.ownerToken
|
|
106
|
+
httpAPI?.memberToken = options.memberToken
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
isInitialized = true
|
|
110
|
+
log("SDK initialized (linkCode=\(options.linkCode), socket=\(options.enableSocket), http=\(options.enableHttp))")
|
|
111
|
+
return self
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// Connect the WebSocket transport.
|
|
115
|
+
public func connect(completion: ((Bool, String?) -> Void)? = nil) {
|
|
116
|
+
guard let opts = options, isInitialized else {
|
|
117
|
+
completion?(false, "SDK not initialized. Call initialize() first.")
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
guard opts.enableSocket else {
|
|
122
|
+
completion?(false, "Socket transport is not enabled")
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if socket?.status == .connected {
|
|
127
|
+
completion?(true, nil)
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
connectionState = .connecting
|
|
132
|
+
|
|
133
|
+
let token = encryptToken(
|
|
134
|
+
apiKey: opts.apiKey,
|
|
135
|
+
linkCode: opts.linkCode,
|
|
136
|
+
chatUsername: opts.chatUsername,
|
|
137
|
+
publicKey: opts.publicKey
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
var connectParams: [String: Any] = ["linkCode": opts.linkCode]
|
|
141
|
+
|
|
142
|
+
if token != opts.apiKey {
|
|
143
|
+
connectParams["token"] = token
|
|
144
|
+
} else {
|
|
145
|
+
connectParams["apiKey"] = opts.apiKey
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if let chatUsername = opts.chatUsername {
|
|
149
|
+
connectParams["chatUsername"] = chatUsername
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
connectParams["chatLinkCode"] = opts.linkCode
|
|
153
|
+
|
|
154
|
+
guard let url = URL(string: opts.baseUrl) else {
|
|
155
|
+
connectionState = .disconnected
|
|
156
|
+
completion?(false, "Invalid baseUrl")
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
manager = SocketManager(socketURL: url, config: [
|
|
160
|
+
.log(opts.debug),
|
|
161
|
+
.compress,
|
|
162
|
+
.forceWebsockets(true),
|
|
163
|
+
.reconnects(true),
|
|
164
|
+
.reconnectWait(1),
|
|
165
|
+
.reconnectWaitMax(30),
|
|
166
|
+
.reconnectAttempts(-1),
|
|
167
|
+
.connectParams(connectParams),
|
|
168
|
+
.extraHeaders([
|
|
169
|
+
"x-api-key": opts.apiKey,
|
|
170
|
+
"x-embed": "true",
|
|
171
|
+
]),
|
|
172
|
+
])
|
|
173
|
+
|
|
174
|
+
socket = manager?.defaultSocket
|
|
175
|
+
|
|
176
|
+
guard let sock = socket else {
|
|
177
|
+
completion?(false, "Failed to create socket")
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
setupLifecycleListeners(socket: sock, completion: completion)
|
|
182
|
+
socketAPI = LiveChattaSocketAPI(socket: sock, client: self)
|
|
183
|
+
// v16 has no `.auth` option: the payload becomes socket.handshake.auth (kept on reconnects).
|
|
184
|
+
sock.connect(withPayload: ["token": "Bearer \(token)"])
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/// Disconnect the WebSocket transport.
|
|
188
|
+
public func disconnect() {
|
|
189
|
+
socket?.removeAllHandlers()
|
|
190
|
+
socket?.disconnect()
|
|
191
|
+
manager?.disconnect()
|
|
192
|
+
socket = nil
|
|
193
|
+
manager = nil
|
|
194
|
+
socketAPI = nil
|
|
195
|
+
connectionState = .disconnected
|
|
196
|
+
log("Disconnected")
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/// Get the Socket API for real-time messaging.
|
|
200
|
+
public func sock() -> LiveChattaSocketAPI {
|
|
201
|
+
guard isInitialized else {
|
|
202
|
+
fatalError("LiveChatta: SDK not initialized. Call initialize() first.")
|
|
203
|
+
}
|
|
204
|
+
guard let api = socketAPI else {
|
|
205
|
+
fatalError("LiveChatta: Socket not connected. Call connect() first.")
|
|
206
|
+
}
|
|
207
|
+
return api
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// Get the HTTP API for REST operations.
|
|
211
|
+
public func api() -> LiveChattaHttpAPI {
|
|
212
|
+
guard isInitialized else {
|
|
213
|
+
fatalError("LiveChatta: SDK not initialized. Call initialize() first.")
|
|
214
|
+
}
|
|
215
|
+
guard let api = httpAPI else {
|
|
216
|
+
fatalError("LiveChatta: HTTP transport not enabled. Set enableHttp=true in initialize().")
|
|
217
|
+
}
|
|
218
|
+
return api
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/// Whether the socket is currently connected
|
|
222
|
+
public var isConnected: Bool {
|
|
223
|
+
connectionState == .connected && socket?.status == .connected
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// Register a listener for SDK lifecycle events.
|
|
227
|
+
@discardableResult
|
|
228
|
+
public func on(_ event: String, listener: @escaping (Any?) -> Void) -> LiveChattaClient {
|
|
229
|
+
lifecycleListeners[event, default: []].append(listener)
|
|
230
|
+
return self
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Remove all listeners for an event.
|
|
234
|
+
@discardableResult
|
|
235
|
+
public func off(_ event: String) -> LiveChattaClient {
|
|
236
|
+
lifecycleListeners.removeValue(forKey: event)
|
|
237
|
+
return self
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/// Destroy the SDK — disconnect, clean up resources.
|
|
241
|
+
public func destroy() {
|
|
242
|
+
disconnect()
|
|
243
|
+
httpAPI = nil
|
|
244
|
+
lifecycleListeners.removeAll()
|
|
245
|
+
options = nil
|
|
246
|
+
isInitialized = false
|
|
247
|
+
log("Destroyed")
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// MARK: - Internal
|
|
251
|
+
|
|
252
|
+
internal func emitLifecycleEvent(_ event: String, data: Any? = nil) {
|
|
253
|
+
lifecycleListeners[event]?.forEach { $0(data) }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
internal func log(_ message: String) {
|
|
257
|
+
if options?.debug == true {
|
|
258
|
+
print("[LiveChatta] \(message)")
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// MARK: - Private
|
|
263
|
+
|
|
264
|
+
private func setupLifecycleListeners(socket sock: SocketIOClient, completion: ((Bool, String?) -> Void)?) {
|
|
265
|
+
var callbackFired = false
|
|
266
|
+
|
|
267
|
+
sock.on(clientEvent: .connect) { [weak self] _, _ in
|
|
268
|
+
guard let self = self else { return }
|
|
269
|
+
let wasReconnecting = self.connectionState == .reconnecting
|
|
270
|
+
self.connectionState = .connected
|
|
271
|
+
self.log("Connected")
|
|
272
|
+
self.emitLifecycleEvent(LiveChattaClient.eventConnected)
|
|
273
|
+
if wasReconnecting { self.emitLifecycleEvent(LiveChattaClient.eventReconnected, data: ["attempt": self.reconnectAttempt]) }
|
|
274
|
+
self.reconnectAttempt = 0
|
|
275
|
+
// Send what was queued while offline (or before the first connect).
|
|
276
|
+
self.socketAPI?.flushQueue()
|
|
277
|
+
if !callbackFired {
|
|
278
|
+
callbackFired = true
|
|
279
|
+
completion?(true, nil)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
sock.on(clientEvent: .disconnect) { [weak self] data, _ in
|
|
284
|
+
guard let self = self else { return }
|
|
285
|
+
let reason = (data.first as? String) ?? "unknown"
|
|
286
|
+
self.connectionState = .disconnected
|
|
287
|
+
self.log("Disconnected: \(reason)")
|
|
288
|
+
self.emitLifecycleEvent(LiveChattaClient.eventDisconnected, data: ["reason": reason])
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
sock.on(clientEvent: .error) { [weak self] data, _ in
|
|
292
|
+
guard let self = self else { return }
|
|
293
|
+
let error = (data.first as? String) ?? "Connection error"
|
|
294
|
+
self.log("Connection error: \(error)")
|
|
295
|
+
self.emitLifecycleEvent(LiveChattaClient.eventError, data: ["error": error, "type": "connection"])
|
|
296
|
+
if !callbackFired {
|
|
297
|
+
callbackFired = true
|
|
298
|
+
completion?(false, error)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
sock.on(clientEvent: .reconnectAttempt) { [weak self] data, _ in
|
|
303
|
+
guard let self = self else { return }
|
|
304
|
+
let attempt = (data.first as? Int) ?? 0
|
|
305
|
+
self.reconnectAttempt += 1
|
|
306
|
+
self.connectionState = .reconnecting
|
|
307
|
+
self.log("Reconnecting... attempt \(attempt)")
|
|
308
|
+
self.emitLifecycleEvent(LiveChattaClient.eventReconnecting, data: ["attempt": attempt])
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// `.reconnect` fires when reconnecting *starts*; success is the next `.connect` (handled above).
|
|
312
|
+
sock.on(clientEvent: .reconnect) { [weak self] _, _ in
|
|
313
|
+
guard let self = self else { return }
|
|
314
|
+
self.connectionState = .reconnecting
|
|
315
|
+
self.log("Connection lost, reconnecting")
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
sock.on("chat-error") { [weak self] data, _ in
|
|
319
|
+
guard let self = self else { return }
|
|
320
|
+
let error = (data.first as? String) ?? "Chat error"
|
|
321
|
+
self.log("Chat error: \(error)")
|
|
322
|
+
if !callbackFired {
|
|
323
|
+
callbackFired = true
|
|
324
|
+
completion?(false, error)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private func encryptToken(apiKey: String, linkCode: String, chatUsername: String?, publicKey: String) -> String {
|
|
330
|
+
var payloadDict: [String: Any] = ["apiKey": apiKey, "linkCode": linkCode]
|
|
331
|
+
if let chatUsername = chatUsername {
|
|
332
|
+
payloadDict["chatUsername"] = chatUsername
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
guard let payloadData = try? JSONSerialization.data(withJSONObject: payloadDict) else {
|
|
336
|
+
return apiKey
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let pem = publicKey
|
|
340
|
+
.replacingOccurrences(of: "-----BEGIN PUBLIC KEY-----", with: "")
|
|
341
|
+
.replacingOccurrences(of: "-----END PUBLIC KEY-----", with: "")
|
|
342
|
+
.replacingOccurrences(of: "\n", with: "")
|
|
343
|
+
.replacingOccurrences(of: "\r", with: "")
|
|
344
|
+
.replacingOccurrences(of: " ", with: "")
|
|
345
|
+
|
|
346
|
+
guard let keyData = Data(base64Encoded: pem) else { return apiKey }
|
|
347
|
+
|
|
348
|
+
let keyDict: [String: Any] = [
|
|
349
|
+
kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
|
|
350
|
+
kSecAttrKeyClass as String: kSecAttrKeyClassPublic,
|
|
351
|
+
kSecAttrKeySizeInBits as String: 2048,
|
|
352
|
+
]
|
|
353
|
+
|
|
354
|
+
guard let secKey = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, nil) else {
|
|
355
|
+
return apiKey
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
var error: Unmanaged<CFError>?
|
|
359
|
+
guard let encryptedData = SecKeyCreateEncryptedData(
|
|
360
|
+
secKey,
|
|
361
|
+
.rsaEncryptionOAEPSHA256,
|
|
362
|
+
payloadData as CFData,
|
|
363
|
+
&error
|
|
364
|
+
) as Data? else {
|
|
365
|
+
return apiKey
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return encryptedData
|
|
369
|
+
.base64EncodedString()
|
|
370
|
+
.replacingOccurrences(of: "+", with: "-")
|
|
371
|
+
.replacingOccurrences(of: "/", with: "_")
|
|
372
|
+
.replacingOccurrences(of: "=", with: "")
|
|
373
|
+
}
|
|
374
|
+
}
|