Use new JSON interface in Swift example.

This commit is contained in:
levlam 2020-11-14 22:45:18 +03:00
parent 94fc06c9fc
commit cde8acf230

View File

@ -9,8 +9,7 @@ import Foundation
// TDLib Client Swift binding // TDLib Client Swift binding
class TdClient { class TdClient {
typealias Client = UnsafeMutableRawPointer var client_id = td_create_client()!
var client = td_json_client_create()!
let tdlibMainLoop = DispatchQueue(label: "TDLib") let tdlibMainLoop = DispatchQueue(label: "TDLib")
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery") let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>() var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
@ -27,7 +26,7 @@ class TdClient {
self.queryF[nextQueryId] = f self.queryF[nextQueryId] = f
self.queryId = nextQueryId self.queryId = nextQueryId
} }
td_json_client_send(self.client, to_json(newQuery)) td_send(self.client_id, to_json(newQuery))
} }
} }
@ -46,7 +45,6 @@ class TdClient {
} }
deinit { deinit {
td_json_client_destroy(client)
} }
func run(updateHandler: @escaping (Dictionary<String,Any>)->()) { func run(updateHandler: @escaping (Dictionary<String,Any>)->()) {
@ -54,7 +52,7 @@ class TdClient {
tdlibMainLoop.async { [weak self] in tdlibMainLoop.async { [weak self] in
while (true) { while (true) {
if let s = self { if let s = self {
if let res = td_json_client_receive(s.client, 10) { if let res = td_receive(10) {
let event = String(cString: res) let event = String(cString: res)
s.queryResultAsync(event) s.queryResultAsync(event)
} }
@ -91,11 +89,11 @@ func to_json(_ obj: Any) -> String {
} }
// An example of usage
td_set_log_verbosity_level(1);
var client = TdClient() var client = TdClient()
// start the client by sending request to it
client.queryAsync(query: ["@type":"getOption", "name":"version"])
func myReadLine() -> String { func myReadLine() -> String {
while (true) { while (true) {
if let line = readLine() { if let line = readLine() {
@ -159,7 +157,7 @@ func updateAuthorizationState(authorizationState: Dictionary<String, Any>) {
case "authorizationStateClosing": case "authorizationStateClosing":
print("Closing...") print("Closing...")
case "authorizationStateLoggingOut": case "authorizationStateClosed":
print("Closed.") print("Closed.")
default: default: