@@ -2,7 +2,7 @@ package {{ sdk.namespace | caseDot }}.services
22
33import {{ sdk .namespace | caseDot }}.Client
44import {{ sdk .namespace | caseDot }}.Service
5- import {{ sdk .namespace | caseDot }}.exceptions.AppwriteException
5+ import {{ sdk .namespace | caseDot }}.exceptions.{{ spec . title | caseUcfirst }}Exception
66import {{ sdk .namespace | caseDot }}.extensions.forEachAsync
77import {{ sdk .namespace | caseDot }}.extensions.fromJson
88import {{ sdk .namespace | caseDot }}.extensions.getSerializer
@@ -53,32 +53,31 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
5353
5454 private fun createSocket() {
5555 launch(Dispatchers.IO) {
56+ try {
5657
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
6762 }
68- }
6963
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+ }
7470
75- webSocketSession = client.httpClient.webSocketSession {
76- url("${client.endpointRealtime}/realtime?$queryParams")
77- }
71+ if (webSocketSession != null) {
72+ reconnect = false
73+ closeSocket()
74+ }
7875
79- reconnectAttempts = 0 // Reset attempts on successful connection
76+ webSocketSession = client.httpClient.webSocketSession {
77+ url("${client.endpointRealtime}/realtime?$queryParams")
78+ }
8079
81- try {
80+ reconnectAttempts = 0 // Reset attempts on successful connection
8281 webSocketSession?.let { session ->
8382 for (frame in session.incoming) {
8483 when (frame) {
@@ -87,6 +86,7 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
8786 }
8887 }
8988 }
89+
9090 } catch (e: Exception) {
9191 handleFailure(e)
9292 } finally {
@@ -149,7 +149,6 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
149149 return RealtimeSubscription {
150150 activeSubscriptions.remove(counter)
151151 cleanUp(channels)
152- createSocket()
153152 }
154153 }
155154
@@ -174,7 +173,7 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
174173 }
175174
176175 private fun handleResponseError(message: RealtimeResponse) {
177- throw message.data.jsonCast<AppwriteException >()
176+ throw message.data.jsonCast< {{ spec . title | caseUcfirst }}Exception >()
178177 }
179178
180179 @Throws(Throwable::class)
0 commit comments