【问题标题】:How to add custom auth header in pusher socket connection如何在推送器套接字连接中添加自定义身份验证标头
【发布时间】:2020-09-27 02:01:34
【问题描述】:
我想在 Pusher 标头中添加我的应用自定义令牌。下面是我的代码。
OCAuthMethod *authMethod = [[OCAuthMethod alloc] initWithAuthEndpoint:kPushedWebHockUrl];
OCPusherHost *host = [[OCPusherHost alloc] initWithCluster:kPusherClusterId];
PusherClientOptions *options = [[PusherClientOptions alloc] initWithOcAuthMethod:authMethod attemptToReturnJSONObject:YES autoReconnect:YES ocHost:host port:nil encrypted:YES activityTimeout:nil];
self.pusher = [[Pusher alloc] initWithAppKey:kPusherKey options:options];
self.pusher.connection.delegate = self;
[self.pusher connect];
【问题讨论】:
标签:
ios
sockets
websocket
pusher
pusherswift
【解决方案1】:
如果您尝试为 Pusher 建立授权连接,请尝试使用 PTPusherChannelAuthorizationDelegate,通过它您可以创建身份验证 API 并将响应数据共享给 Pusher。
您可以将 Pusher 对象的委托设置为
pusher.channelAuthorizationDelegate = self
然后在你的类中实现委托函数
func pusherChannel(_ channel: PTPusherChannel!, requiresAuthorizationForSocketID socketID: String!, completionHandler: PTPusherChannelAuthorizationCallback!) {
//create API call with your custom header.You can use URLSession, Alamofire or something like that. Once you get success response call the completionHandler.
completionHandler(true,response,nil)
}
希望这会有所帮助。