Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding auth in socket request #51

Open
nandarosyadi opened this issue Mar 21, 2024 · 8 comments
Open

Adding auth in socket request #51

nandarosyadi opened this issue Mar 21, 2024 · 8 comments

Comments

@nandarosyadi
Copy link

Server need to receive auth token from client, so it needs to send the auth. Is there any ways to add auth in socket request? Thank you

@ajinumoto
Copy link

Have you try sending auth token data right after your socket connection was open?

@nandarosyadi
Copy link
Author

Thank you for the quick response. Server request to send the auth when starting connection, so it need to send when the connect() method called. Is there any way to do that in current version?

@ajinumoto
Copy link

If your token on header and sent during handshake you can try add your token in options during initialization
NWWebSocket(url: socketURL, options: options)
where
options.setAdditionalHeaders([("header_key","your_token")])

@ajinumoto
Copy link

let options = NWWebSocket.defaultOptions
options.setAdditionalHeaders([("header_key","your_token")])
NWWebSocket(url: socketURL, options: options)

@nandarosyadi
Copy link
Author

nandarosyadi commented Mar 22, 2024

Hey @ajinumoto, thanks for the help. Now, I can send my auth but the socket is not connected yet. It's not called the delegate method, so what's wrong here?

Here is my code

    func connectSocket() {
        guard let accessJWT: String = UserDefaultManager.getUserDefault(key: .userData_AccessJWT) else {
            self.delegate.socketDidError(message: "Token socket not found")
            return
        }
        
        self.socketManager?.delegate = self
        
        let socketOption = NWProtocolWebSocket.Options()
        socketOption.setAdditionalHeaders([("token", accessJWT)])
        
        self.socketManager = NWWebSocket(url: self.socketURL, 
                                         connectAutomatically: true,
                                         options: socketOption,
                                         connectionQueue: .main)
                                         
        self.socketManager?.ping(interval: 15)
        
        func webSocketDidConnect(connection: WebSocketConnection) {
        print("Socket: Connected")

        self.socketManager?.ping(interval: 15)
    }

    func webSocketDidDisconnect(connection: WebSocketConnection,
                                closeCode: NWProtocolWebSocket.CloseCode, reason: Data?) {
        print("Socket: Disconnected")

        self.connectSocket()
    }
    
    func webSocketDidConnect(connection: WebSocketConnection) {
        print("Socket: Connected")

        self.socketManager?.ping(interval: 15)
    }

@ajinumoto
Copy link

Probably because you set
self.socketManager?.delegate = self
before
self.socketManager = NWWebSocket(url: self.socketURL, connectAutomatically: true, options: socketOption, connectionQueue: .main)
so your delegate is still on old self.socketManager value (or nil)

@nandarosyadi
Copy link
Author

I've move the delegate after the NWWebSocket initialization, but the delegate method is not called either

        self.socketManager = NWWebSocket(url: self.socketURL, 
                                         connectAutomatically: true,
                                         options: socketOption,
                                         connectionQueue: .main)
        self.socketManager?.delegate = self
        self.socketManager?.ping(interval: 15)

@ajinumoto
Copy link

but the delegate method is not called

All delegate not working really, even the connect and disconnect delegate? If all of delegate are not working, there is something wrong with your code. If you didn't receive message from socket try listen using self.socketManager?.listen()?

Btw, i highly recommend you to see NWWebSocket class for deeper understanding about this library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants