@livechatta/sdk 0.0.0-stage → 1.0.1
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 +228 -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 +7259 -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 +7078 -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 +7263 -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 +274 -4
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
package com.livechatta.sdk
|
|
2
|
+
|
|
3
|
+
import android.util.Base64
|
|
4
|
+
import android.util.Log
|
|
5
|
+
import io.socket.client.IO
|
|
6
|
+
import io.socket.client.Manager
|
|
7
|
+
import io.socket.client.Socket
|
|
8
|
+
import io.socket.engineio.client.transports.WebSocket
|
|
9
|
+
import org.json.JSONObject
|
|
10
|
+
import java.net.URI
|
|
11
|
+
import java.security.KeyFactory
|
|
12
|
+
import java.security.spec.X509EncodedKeySpec
|
|
13
|
+
import javax.crypto.Cipher
|
|
14
|
+
|
|
15
|
+
// Android headless client: init(LiveChattaOptions) then sock() (socket.io-client 2.1.0) and api().
|
|
16
|
+
// Usage examples: README "Android (Kotlin)".
|
|
17
|
+
|
|
18
|
+
data class LiveChattaOptions(
|
|
19
|
+
/** Required — chat link code */
|
|
20
|
+
val linkCode: String,
|
|
21
|
+
/** Required — API key for authentication */
|
|
22
|
+
val apiKey: String,
|
|
23
|
+
/** Required — RSA public key (PEM) for encrypting tokens */
|
|
24
|
+
val publicKey: String,
|
|
25
|
+
/** Base URL of the LiveChatta server */
|
|
26
|
+
val baseUrl: String = "https://api.livechatta.com",
|
|
27
|
+
/** Optional — pre-registered chat username */
|
|
28
|
+
val chatUsername: String? = null,
|
|
29
|
+
/** Enable WebSocket transport */
|
|
30
|
+
val enableSocket: Boolean = true,
|
|
31
|
+
/** Enable HTTP transport */
|
|
32
|
+
val enableHttp: Boolean = true,
|
|
33
|
+
/** Enable debug logging */
|
|
34
|
+
val debug: Boolean = false,
|
|
35
|
+
/** Optional owner account token (JWT) for owner-console HTTP methods */
|
|
36
|
+
val ownerToken: String? = null,
|
|
37
|
+
/** Optional chat-member token (JWT) for member HTTP routes (mute, reminders, …) */
|
|
38
|
+
val memberToken: String? = null,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
enum class ConnectionState {
|
|
42
|
+
DISCONNECTED,
|
|
43
|
+
CONNECTING,
|
|
44
|
+
CONNECTED,
|
|
45
|
+
RECONNECTING,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class LiveChattaClient {
|
|
49
|
+
private var options: LiveChattaOptions? = null
|
|
50
|
+
private var socket: Socket? = null
|
|
51
|
+
private var socketAPI: LiveChattaSocketAPI? = null
|
|
52
|
+
private var httpAPI: LiveChattaHttpAPI? = null
|
|
53
|
+
private var state: ConnectionState = ConnectionState.DISCONNECTED
|
|
54
|
+
private var initialized = false
|
|
55
|
+
|
|
56
|
+
private val lifecycleListeners = mutableMapOf<String, MutableList<(Any?) -> Unit>>()
|
|
57
|
+
|
|
58
|
+
companion object {
|
|
59
|
+
const val TAG = "LiveChatta"
|
|
60
|
+
|
|
61
|
+
// SDK lifecycle events
|
|
62
|
+
const val EVENT_CONNECTED = "sdk:connected"
|
|
63
|
+
const val EVENT_DISCONNECTED = "sdk:disconnected"
|
|
64
|
+
const val EVENT_RECONNECTING = "sdk:reconnecting"
|
|
65
|
+
const val EVENT_RECONNECTED = "sdk:reconnected"
|
|
66
|
+
const val EVENT_ERROR = "sdk:error"
|
|
67
|
+
const val EVENT_QUEUE_FLUSH = "sdk:queue-flush"
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Initialize the SDK with connection options.
|
|
72
|
+
* Must be called before using sock(), api(), or connect().
|
|
73
|
+
*/
|
|
74
|
+
fun init(options: LiveChattaOptions): LiveChattaClient {
|
|
75
|
+
require(options.linkCode.isNotBlank()) { "LiveChatta: linkCode is required" }
|
|
76
|
+
require(options.apiKey.isNotBlank()) { "LiveChatta: apiKey is required" }
|
|
77
|
+
require(options.publicKey.isNotBlank()) { "LiveChatta: publicKey is required" }
|
|
78
|
+
|
|
79
|
+
this.options = options
|
|
80
|
+
|
|
81
|
+
if (options.enableHttp) {
|
|
82
|
+
httpAPI =
|
|
83
|
+
LiveChattaHttpAPI(
|
|
84
|
+
baseUrl = "${options.baseUrl.trimEnd('/')}/api/v1/external",
|
|
85
|
+
apiKey = options.apiKey,
|
|
86
|
+
debug = options.debug,
|
|
87
|
+
).also {
|
|
88
|
+
it.ownerToken = options.ownerToken
|
|
89
|
+
it.memberToken = options.memberToken
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
initialized = true
|
|
94
|
+
log("SDK initialized (linkCode=${options.linkCode}, socket=${options.enableSocket}, http=${options.enableHttp})")
|
|
95
|
+
return this
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Connect the WebSocket transport.
|
|
100
|
+
* @param callback Optional callback with (success, errorMessage)
|
|
101
|
+
*/
|
|
102
|
+
fun connect(callback: ((Boolean, String?) -> Unit)? = null) {
|
|
103
|
+
assertInitialized()
|
|
104
|
+
val opts = options!!
|
|
105
|
+
|
|
106
|
+
if (!opts.enableSocket) {
|
|
107
|
+
callback?.invoke(false, "Socket transport is not enabled")
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (socket?.connected() == true) {
|
|
112
|
+
callback?.invoke(true, null)
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setState(ConnectionState.CONNECTING)
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
val token = encryptToken(opts.apiKey, opts.linkCode, opts.chatUsername, opts.publicKey)
|
|
120
|
+
|
|
121
|
+
val queryParams =
|
|
122
|
+
mutableMapOf<String, String>(
|
|
123
|
+
"linkCode" to opts.linkCode,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
if (token != opts.apiKey) {
|
|
127
|
+
queryParams["token"] = token
|
|
128
|
+
} else {
|
|
129
|
+
queryParams["apiKey"] = opts.apiKey
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
opts.chatUsername?.let { queryParams["chatUsername"] = it }
|
|
133
|
+
|
|
134
|
+
queryParams["chatLinkCode"] = opts.linkCode
|
|
135
|
+
|
|
136
|
+
val ioOptions =
|
|
137
|
+
IO.Options
|
|
138
|
+
.builder()
|
|
139
|
+
.setTransports(arrayOf(WebSocket.NAME))
|
|
140
|
+
.setQuery(queryParams.entries.joinToString("&") { "${it.key}=${it.value}" })
|
|
141
|
+
.setAuth(mapOf("token" to "Bearer $token"))
|
|
142
|
+
.setExtraHeaders(
|
|
143
|
+
mapOf(
|
|
144
|
+
"x-api-key" to listOf(opts.apiKey),
|
|
145
|
+
"x-embed" to listOf("true"),
|
|
146
|
+
),
|
|
147
|
+
).setReconnection(true)
|
|
148
|
+
.setReconnectionDelay(1000)
|
|
149
|
+
.setReconnectionDelayMax(30000)
|
|
150
|
+
.setReconnectionAttempts(Integer.MAX_VALUE)
|
|
151
|
+
.build()
|
|
152
|
+
|
|
153
|
+
socket = IO.socket(URI.create(opts.baseUrl), ioOptions)
|
|
154
|
+
|
|
155
|
+
setupLifecycleListeners(callback)
|
|
156
|
+
|
|
157
|
+
socketAPI = LiveChattaSocketAPI(socket!!, this)
|
|
158
|
+
|
|
159
|
+
socket!!.connect()
|
|
160
|
+
} catch (e: Exception) {
|
|
161
|
+
setState(ConnectionState.DISCONNECTED)
|
|
162
|
+
log("Connection error: ${e.message}")
|
|
163
|
+
callback?.invoke(false, e.message)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Disconnect the WebSocket transport.
|
|
169
|
+
*/
|
|
170
|
+
fun disconnect() {
|
|
171
|
+
socket?.off()
|
|
172
|
+
socket?.disconnect()
|
|
173
|
+
socket = null
|
|
174
|
+
socketAPI = null
|
|
175
|
+
setState(ConnectionState.DISCONNECTED)
|
|
176
|
+
log("Disconnected")
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Get the Socket API for real-time messaging.
|
|
181
|
+
*/
|
|
182
|
+
fun sock(): LiveChattaSocketAPI {
|
|
183
|
+
assertInitialized()
|
|
184
|
+
return socketAPI
|
|
185
|
+
?: throw IllegalStateException("LiveChatta: Socket not connected. Call connect() first.")
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get the HTTP API for REST operations.
|
|
190
|
+
*/
|
|
191
|
+
fun api(): LiveChattaHttpAPI {
|
|
192
|
+
assertInitialized()
|
|
193
|
+
return httpAPI
|
|
194
|
+
?: throw IllegalStateException("LiveChatta: HTTP transport not enabled. Set enableHttp=true in init().")
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Check if the socket is connected */
|
|
198
|
+
fun isConnected(): Boolean = state == ConnectionState.CONNECTED && socket?.connected() == true
|
|
199
|
+
|
|
200
|
+
/** Get current connection state */
|
|
201
|
+
fun getConnectionState(): ConnectionState = state
|
|
202
|
+
|
|
203
|
+
/** Check if SDK is initialized */
|
|
204
|
+
fun isInitialized(): Boolean = initialized
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Register a listener for SDK lifecycle events.
|
|
208
|
+
* Events: sdk:connected, sdk:disconnected, sdk:reconnecting, sdk:reconnected, sdk:error
|
|
209
|
+
*/
|
|
210
|
+
fun on(
|
|
211
|
+
event: String,
|
|
212
|
+
listener: (Any?) -> Unit,
|
|
213
|
+
): LiveChattaClient {
|
|
214
|
+
lifecycleListeners.getOrPut(event) { mutableListOf() }.add(listener)
|
|
215
|
+
return this
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Remove a lifecycle event listener.
|
|
220
|
+
*/
|
|
221
|
+
fun off(
|
|
222
|
+
event: String,
|
|
223
|
+
listener: ((Any?) -> Unit)? = null,
|
|
224
|
+
): LiveChattaClient {
|
|
225
|
+
if (listener == null) {
|
|
226
|
+
lifecycleListeners.remove(event)
|
|
227
|
+
} else {
|
|
228
|
+
lifecycleListeners[event]?.remove(listener)
|
|
229
|
+
}
|
|
230
|
+
return this
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Destroy the SDK — disconnect, clean up resources.
|
|
235
|
+
*/
|
|
236
|
+
fun destroy() {
|
|
237
|
+
disconnect()
|
|
238
|
+
httpAPI = null
|
|
239
|
+
lifecycleListeners.clear()
|
|
240
|
+
options = null
|
|
241
|
+
initialized = false
|
|
242
|
+
log("Destroyed")
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// --- Internal ---
|
|
246
|
+
|
|
247
|
+
internal fun emitLifecycleEvent(
|
|
248
|
+
event: String,
|
|
249
|
+
data: Any? = null,
|
|
250
|
+
) {
|
|
251
|
+
lifecycleListeners[event]?.forEach { it(data) }
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
internal fun log(message: String) {
|
|
255
|
+
if (options?.debug == true) {
|
|
256
|
+
Log.d(TAG, message)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private fun setState(newState: ConnectionState) {
|
|
261
|
+
state = newState
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private fun assertInitialized() {
|
|
265
|
+
if (!initialized || options == null) {
|
|
266
|
+
throw IllegalStateException("LiveChatta: SDK not initialized. Call init() first.")
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private fun setupLifecycleListeners(connectCallback: ((Boolean, String?) -> Unit)?) {
|
|
271
|
+
val sock = socket ?: return
|
|
272
|
+
var callbackFired = false
|
|
273
|
+
|
|
274
|
+
sock.on(Socket.EVENT_CONNECT) {
|
|
275
|
+
setState(ConnectionState.CONNECTED)
|
|
276
|
+
log("Connected")
|
|
277
|
+
emitLifecycleEvent(EVENT_CONNECTED)
|
|
278
|
+
// Also after a reconnect: send what was queued while offline (or before the first connect).
|
|
279
|
+
socketAPI?.flushQueue()
|
|
280
|
+
if (!callbackFired) {
|
|
281
|
+
callbackFired = true
|
|
282
|
+
connectCallback?.invoke(true, null)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
sock.on(Socket.EVENT_DISCONNECT) { args ->
|
|
287
|
+
val reason = args.firstOrNull()?.toString() ?: "unknown"
|
|
288
|
+
setState(ConnectionState.DISCONNECTED)
|
|
289
|
+
log("Disconnected: $reason")
|
|
290
|
+
emitLifecycleEvent(EVENT_DISCONNECTED, mapOf("reason" to reason))
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
sock.on(Socket.EVENT_CONNECT_ERROR) { args ->
|
|
294
|
+
val error = args.firstOrNull()?.toString() ?: "Connection error"
|
|
295
|
+
log("Connection error: $error")
|
|
296
|
+
emitLifecycleEvent(EVENT_ERROR, mapOf("error" to error, "type" to "connection"))
|
|
297
|
+
if (!callbackFired) {
|
|
298
|
+
callbackFired = true
|
|
299
|
+
connectCallback?.invoke(false, error)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
sock.io().on(io.socket.engineio.client.Socket.EVENT_TRANSPORT) {
|
|
304
|
+
// Reconnecting state
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// socket.io-client-java 2.x fires reconnection events on the Manager, not the Socket.
|
|
308
|
+
sock.io().on(Manager.EVENT_RECONNECT_ATTEMPT) { args ->
|
|
309
|
+
val attempt = (args.firstOrNull() as? Number)?.toInt() ?: 0
|
|
310
|
+
setState(ConnectionState.RECONNECTING)
|
|
311
|
+
log("Reconnecting... attempt $attempt")
|
|
312
|
+
emitLifecycleEvent(EVENT_RECONNECTING, mapOf("attempt" to attempt))
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
sock.io().on(Manager.EVENT_RECONNECT) { args ->
|
|
316
|
+
val attempt = (args.firstOrNull() as? Number)?.toInt() ?: 0
|
|
317
|
+
log("Reconnected after $attempt attempts")
|
|
318
|
+
emitLifecycleEvent(EVENT_RECONNECTED, mapOf("attempt" to attempt))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
sock.on("chat-error") { args ->
|
|
322
|
+
val error = args.firstOrNull()?.toString() ?: "Chat error"
|
|
323
|
+
log("Chat error: $error")
|
|
324
|
+
if (!callbackFired) {
|
|
325
|
+
callbackFired = true
|
|
326
|
+
connectCallback?.invoke(false, error)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private fun encryptToken(
|
|
332
|
+
apiKey: String,
|
|
333
|
+
linkCode: String,
|
|
334
|
+
chatUsername: String?,
|
|
335
|
+
publicKey: String,
|
|
336
|
+
): String {
|
|
337
|
+
val payload =
|
|
338
|
+
JSONObject()
|
|
339
|
+
.apply {
|
|
340
|
+
put("apiKey", apiKey)
|
|
341
|
+
put("linkCode", linkCode)
|
|
342
|
+
chatUsername?.let { put("chatUsername", it) }
|
|
343
|
+
}.toString()
|
|
344
|
+
|
|
345
|
+
return try {
|
|
346
|
+
val pem =
|
|
347
|
+
publicKey
|
|
348
|
+
.replace("-----BEGIN PUBLIC KEY-----", "")
|
|
349
|
+
.replace("-----END PUBLIC KEY-----", "")
|
|
350
|
+
.replace("\\s".toRegex(), "")
|
|
351
|
+
|
|
352
|
+
val keyBytes = Base64.decode(pem, Base64.DEFAULT)
|
|
353
|
+
val keySpec = X509EncodedKeySpec(keyBytes)
|
|
354
|
+
val keyFactory = KeyFactory.getInstance("RSA")
|
|
355
|
+
val key = keyFactory.generatePublic(keySpec)
|
|
356
|
+
|
|
357
|
+
val cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding")
|
|
358
|
+
cipher.init(Cipher.ENCRYPT_MODE, key)
|
|
359
|
+
val encrypted = cipher.doFinal(payload.toByteArray(Charsets.UTF_8))
|
|
360
|
+
|
|
361
|
+
Base64.encodeToString(encrypted, Base64.URL_SAFE or Base64.NO_WRAP or Base64.NO_PADDING)
|
|
362
|
+
} catch (e: Exception) {
|
|
363
|
+
log("Token encryption failed, falling back to raw apiKey: ${e.message}")
|
|
364
|
+
apiKey
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|