socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

extraHeaders missing

Open mediavariance opened this issue 3 years ago • 2 comments

I'm unable to get an authorization header to work. This is on master, and I tried all of the recent tags on this project. I verified that the Authorization header is not reaching the web socket server.

I've tried:

  • Using connectParams instead or with extraHeaders
  • Inserting a new config in my connect method below (manager.config.insert)
  • manager.setAdditionalHeaders
  • socket.connect(withPayload)

Any ideas?

import Foundation
import SocketIO

class DeviceInviteService: ObservableObject {
    private var token: String
    
    private let manager: SocketManager
    private var socket: SocketIOClient!
    private var nameSpace = "App\\Events\\"
    
    init( token: String ) {
        self.token = token
        
        self.manager = SocketManager(
            socketURL: URL(string: "wss://laravel-echo-server/socket.io")!,
            config:
                [
                    .log(true),
                    .version(.two),
                    .compress,
                    .reconnects(true),
                    .reconnectAttempts(-1),
                    .forceWebsockets(true),
                    .forceNew(true),
                    .forcePolling(true),
                    .secure(true),
                    .extraHeaders(["Authorization": "Bearer \(token)"])
                ]
        )
    }
    
    func connect( token: String, deviceID: Int ) {
        socket = manager.defaultSocket
        socket.connect()
        
        socket.on(clientEvent: .connect) {data, ack in
            let channelData = ["channel": "private-App.Models.MyModel.\(deviceID)"]
            
            self.socket.emit("subscribe", channelData) {
                self.socket.onAny
                {
                    print("Sockets: Got event: \($0.event), with items: \($0.items)")
                }
                
                self.socket.on("messages") { (dataArray, socketAck) -> Void in
                    print("Sockets: Received Response!")
                }
            }
        }
    }
}

mediavariance avatar Apr 15 '22 03:04 mediavariance

I am also having same issue. Did you solve it?

BH102 avatar Jul 01 '22 11:07 BH102

I have solved it, I'm also using laravel-echo-server in my case it was because the middleware.

BH102 avatar Jul 01 '22 12:07 BH102