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

Connecting to a faulty url, does not return an error #911

Open
MustaphaQadri opened this issue Aug 13, 2021 · 2 comments
Open

Connecting to a faulty url, does not return an error #911

MustaphaQadri opened this issue Aug 13, 2021 · 2 comments
Labels

Comments

@MustaphaQadri
Copy link
Contributor

MustaphaQadri commented Aug 13, 2021

Describe the bug

Connecting to a server while it is down, mistyped url, or no internet connection does not result in an error. The WebSocket does not emit any event to inform its delegate that the connection failed. I found that urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) is not implemented in NativeEngine. This is the place where those errors are fired.

Steps to Reproduce

Detailed steps to reproduce the problematic behavior:

        // not an existing url
        var request = URLRequest(url: URL(string: "wss://socket.test.k2.nl/match/2xj5crhn5vhy6vmys0stkqi3s8")!)
        request.setValue(url.host, forHTTPHeaderField: "Host")
        webSocket = WebSocket(request: request)
        
        webSocket.onEvent = { event in
            print(event)
        }
        
        webSocket.connect()
  • run this code and break on print(event) this will not be triggered
  • disabling internet connection and connecting to a valid url will also not trigger an event

Expected behavior

Connecting to an unreachable server should trigger an error event.

Environment:

  • OS/Version: iOS 14.4
  • Starscream Version: 4.0.4
  • Xcode version: 12.5

Additional context

Add any other context about the problem here.

@StartingZhou
Copy link

We also encounter this problem,I found the Starscream use NWConnection,when use unreachable URL, the NWConnection does not call it's handler, below code in WebSocket.swift:

public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true) {
        if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *), !useCustomEngine {
            self.init(request: request, engine: NativeEngine())
        } else if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) {
            self.init(request: request, engine: WSEngine(transport: TCPTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        } else {
            self.init(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        }
    }

The class TCPTransport use NWConnection for data transport, so just delete the else if as whole, or change TCPTransport to FoundationTransport

@eric-clover
Copy link
Contributor

I'm also seeing this issue. The fix in #821 fixes this for me and allows me to gracefully connect/reconnect when the server comes back online.

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

No branches or pull requests

3 participants