gpt4 book ai didi

Swift Pusher 示例什么都不做

转载 作者:行者123 更新时间:2023-11-30 10:37:09 27 4
gpt4 key购买 nike

我正在使用 Pusher 提供的起始代码,当我将其放入基本的 swift 项目并向其发送消息时,没有任何反应。我也安装了 cocoa pod 包。

应打印语句“已收到数据”。

也没有错误。

My code the dashboard

enter image description here

最佳答案

您是否将 Pusher 对象初始化为 var?您需要维护对该对象的强引用,否则它可能会被释放。

举个例子,它应该是这样的:

class ViewController: UIViewController {
var pusher: Pusher!

override func viewDidLoad() {
super.viewDidLoad()

let options = PusherClientOptions(
host: .cluster("mycluster")
)
pusher = Pusher(
key: "app_key",
options: options
)

// subscribe to channel and bind to event
let channel = pusher.subscribe("my-channel")

let _ = channel.bind(eventName: "my-event", callback: { (data: Any?) -> Void in
if let data = data as? [String : AnyObject] {
if let message = data["message"] as? String {
print(message)
}
}
})

pusher.connect()

}
}

关于Swift Pusher 示例什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57727653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com