作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用“AuthRequestBuilderProtocol”来验证 Pusher 连接。但它没有被调用,我创建了一个继承自 AuthRequestBuilderProtocol 的 AuthRequestBuilder 类,如下面的代码
class AuthRequestBuilder: AuthRequestBuilderProtocol {
func requestFor(socketID: String, channelName: String) -> URLRequest? {
print("HELLO FROM AUTH REQUEST BUILDER!")
let request = NSMutableURLRequest(url: URL(string: "https://xxxxxxxxxx/broadcasting/auth")!)
request.httpMethod = "POST"
request.httpBody = "socket_id=\(socketID)&channel_name=\(channelName)".data(using: String.Encoding.utf8)
print("socketID: \(socketID), ChannelName: \(channelName)")
request.addValue("Bearer " + UserDefaults.standard.retreiveUserAccessToken(), forHTTPHeaderField: "Authorization")
return request as URLRequest
}
在主 Controller 中我定义了以下变量
var client: Pusher?
var channel: PusherChannel?
var options: PusherClientOptions?
var appKey: String = "xxxx"
在 viewDidLoad 中我定义了以下内容:
override func viewDidLoad() {
super.viewDidLoad()
options = PusherClientOptions(
authMethod: AuthMethod.authRequestBuilder(authRequestBuilder: AuthRequestBuilder()),
autoReconnect: true, host: .host("xxxxxxxxxx"),
port: 6001,
path: "/",
activityTimeout: 10
)
client = Pusher(withAppKey: appKey, options: options!)
client?.connection.delegate = self
channel = client?.subscribeToPresenceChannel(channelName: "presence-Room.\(idOfRooms ?? 0)")
client?.connect()
print(channel?.name ?? "")
print(channel?.subscribed ?? "")
print(channel?.type ?? "")
print(channel?.unsentEvents ?? "")
// bind to all events globally
let _ = client?.bind(eventCallback: { (event: PusherEvent) in
var message = "Received event: '\(event.eventName)'"
if let channel = event.channelName {
message += " on channel '\(channel)'"
}
if let userId = event.userId {
message += " from user '\(userId)'"
}
if let data = event.data {
message += " with data '\(data)'"
}
print(message)
})}
问题是当我在 viewDidLoad 中调用时 AuthRequestBuilder 没有被调用,所以我在订阅状态 channel 时遇到了问题,知道如何解决这个问题
最佳答案
我上面提供的所有方法都是正确的!问题是后端服务器版本与最新版本的 PusherSwift 不兼容,所以我从 10.0.0 降级到 8.0.0
关于ios - 'PusherSwift' 验证未在 swift 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68359414/
我正在尝试使用“AuthRequestBuilderProtocol”来验证 Pusher 连接。但它没有被调用,我创建了一个继承自 AuthRequestBuilderProtocol 的 Auth
我是一名优秀的程序员,十分优秀!