iosswiftstarscream

Swift Cannot invoke initializer for type 'WebSocket' with an argument list of type '(url: URL)


I have following code in swift for web-socket communication, but when I build the code I am getting error.

Code:

import Foundation
import Starscream

class StarscreamWebSocket: WebSocketProvider {

    var delegate: WebSocketProviderDelegate?
    private let socket: WebSocket
    
    init(url: URL) {
        self.socket = WebSocket(url: url)
        self.socket.delegate = self
    }
    
    func connect() {
        self.socket.connect()
    }
    
    func send(data: Data) {
        self.socket.write(data: data)
    }
}

Error:

Cannot invoke initializer for type 'WebSocket' with an argument list of type '(url: URL)'

What could be the problem.


Solution

  • This framework's WebSocket class doesn't have an initializer that takes a single URL parameter. It has two initializers:

    public init(request: URLRequest, engine: Engine)
    public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true)
    

    You seem to want to use the second one. Just create a URLRequest with your URL

    let request = URLRequest(url: url)
    self.socket = WebSocket(request: request )