@@ -2,7 +2,7 @@ package {{ sdk.namespace | caseDot }}.services
2
2
3
3
import {{ sdk .namespace | caseDot }}.Client
4
4
import {{ sdk .namespace | caseDot }}.Service
5
- import {{ sdk .namespace | caseDot }}.exceptions.AppwriteException
5
+ import {{ sdk .namespace | caseDot }}.exceptions.{{ spec . title | caseUcfirst }}Exception
6
6
import {{ sdk .namespace | caseDot }}.extensions.forEachAsync
7
7
import {{ sdk .namespace | caseDot }}.extensions.fromJson
8
8
import {{ sdk .namespace | caseDot }}.extensions.getSerializer
@@ -53,32 +53,31 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
53
53
54
54
private fun createSocket() {
55
55
launch(Dispatchers.IO) {
56
+ try {
56
57
57
- if (activeChannels.isEmpty()) {
58
- reconnect = false
59
- closeSocket()
60
- return@launch
61
- }
62
-
63
- val queryParams = buildString {
64
- append("project=${client.config["project"]}")
65
- activeChannels.forEach {
66
- append("& channels[]=$it")
58
+ if (activeChannels.isEmpty()) {
59
+ reconnect = false
60
+ closeSocket()
61
+ return@launch
67
62
}
68
- }
69
63
70
- if (webSocketSession != null) {
71
- reconnect = false
72
- closeSocket()
73
- }
64
+ val queryParams = buildString {
65
+ append("project=${client.config["project"]}")
66
+ activeChannels.forEach {
67
+ append("& channels[]=$it")
68
+ }
69
+ }
74
70
75
- webSocketSession = client.httpClient.webSocketSession {
76
- url("${client.endpointRealtime}/realtime?$queryParams")
77
- }
71
+ if (webSocketSession != null) {
72
+ reconnect = false
73
+ closeSocket()
74
+ }
78
75
79
- reconnectAttempts = 0 // Reset attempts on successful connection
76
+ webSocketSession = client.httpClient.webSocketSession {
77
+ url("${client.endpointRealtime}/realtime?$queryParams")
78
+ }
80
79
81
- try {
80
+ reconnectAttempts = 0 // Reset attempts on successful connection
82
81
webSocketSession?.let { session ->
83
82
for (frame in session.incoming) {
84
83
when (frame) {
@@ -87,6 +86,7 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
87
86
}
88
87
}
89
88
}
89
+
90
90
} catch (e: Exception) {
91
91
handleFailure(e)
92
92
} finally {
@@ -149,7 +149,6 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
149
149
return RealtimeSubscription {
150
150
activeSubscriptions.remove(counter)
151
151
cleanUp(channels)
152
- createSocket()
153
152
}
154
153
}
155
154
@@ -174,7 +173,7 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
174
173
}
175
174
176
175
private fun handleResponseError(message: RealtimeResponse) {
177
- throw message.data.jsonCast<AppwriteException >()
176
+ throw message.data.jsonCast< {{ spec . title | caseUcfirst }}Exception >()
178
177
}
179
178
180
179
@Throws(Throwable::class)
0 commit comments