Skip to content

Commit 54f35f4

Browse files
committed
Added some custom implementations.
1 parent 3576b8d commit 54f35f4

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

UTSChatAdapter/UTSChatAdapter/ChatAdapter.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,65 @@ class ChatAdapter {
5050
// GENERATED CONTENT BEGIN
5151
// ...
5252
// GENERATED CONTENT END
53+
54+
// Custom fields implementation (see `Schema.skipPaths` for reasons):
55+
56+
case "ChatClient":
57+
let requestId = rpcParams["id"] as! String
58+
let chatOptions = ClientOptions.from(rpcParams["clientOptions"])
59+
let realtimeOptions = ARTClientOptions.from(rpcParams["realtimeClientOptions"])
60+
let realtime = ARTRealtime(options: realtimeOptions)
61+
let chatClient = DefaultChatClient(realtime: realtime, clientOptions: chatOptions)
62+
let instanceId = generateId()
63+
idToChatClient[instanceId] = chatClient
64+
return jsonRpcResult(requestId, "{\"instanceId\":\"\(instanceId)\"}")
65+
66+
// This field is optional and should be included in a corresponding json schema for automatic generation
67+
case "ConnectionStatus#error":
68+
let refId = rpcParams["refId"] as! String
69+
guard let instance = idToConnectionStatus[refId] else {
70+
print("Instance with this `refId` doesn't exist."); return nil;
71+
}
72+
if let error = instance.error { // ErrorInfo
73+
return jsonRpcResult(rpcParams["id"] as! String, "{\"response\": \"\(error.json())\"}")
74+
} else {
75+
return jsonRpcResult(rpcParams["id"] as! String, "{\"response\": \(NSNull()) }")
76+
}
77+
78+
// This field is optional and should be included in a corresponding json schema for automatic generation
79+
case "Message#createdAt":
80+
let refId = rpcParams["refId"] as! String
81+
guard let message = idToMessage[refId] else {
82+
print("Message with `refId == \(refId)` doesn't exist.")
83+
return nil
84+
}
85+
if let createdAt = message.createdAt { // number
86+
return jsonRpcResult(rpcParams["id"] as! String, "{\"response\": \"\(createdAt)\"}")
87+
} else {
88+
return jsonRpcResult(rpcParams["id"] as! String, "{\"response\": \(NSNull()) }")
89+
}
90+
91+
// `events` is an array of strings in schema file which is not enougth for param auto-generation (should be `PresenceEventType`)
92+
case "Presence.subscribe_eventsAndListener":
93+
guard let events = rpcParams["events"] as? [String] else {
94+
return nil
95+
}
96+
guard let presenceRef = idToPresence[rpcParams.refId] else {
97+
print("Presence with `refId == \(rpcParams.refId)` doesn't exist.")
98+
return nil
99+
}
100+
let subscription = await presenceRef.subscribe(events: events.map { PresenceEventType.from($0) })
101+
let callback: (PresenceEvent) -> Void = {
102+
self.webSocket.send(text: jsonRpcCallback(rpcParams.callbackId, "\($0.json())"))
103+
}
104+
Task {
105+
for await event in subscription {
106+
callback(event)
107+
}
108+
}
109+
let resultRefId = generateId()
110+
idToPresenceSubscription[resultRefId] = subscription
111+
return jsonRpcResult(rpcParams.requestId, "{\"refId\":\"\(resultRefId)\"}")
53112

54113
default:
55114
print("Unknown method provided.")

0 commit comments

Comments
 (0)