pusher/pusher-websocket-swift

'PusherSwift' authenticate not called in swift

ghassanjaa opened this issue · 1 comments

Im trying to using "AuthRequestBuilderProtocol" for authenticating the Pusher connection. But it's not called, I create a class AuthRequestBuilder that inherit from AuthRequestBuilderProtocol like the code below

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
}

In the main controller i define the following variables

var client: Pusher?
var channel: PusherChannel?
var options: PusherClientOptions?
var appKey: String = "xxxx"

And in viewDidLoad i define the following:

    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)
    })}

The problem the AuthRequestBuilder not called when i called in viewDidLoad so i have a problem to subscribe to presence channel, any idea how to fix the problem

I fix the problem by returning to version 8.0.0 because the backend server is compatible with this version and not for latest version