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

"Socket is not connected" error after backgrounding #258

Open
KaylaBrady opened this issue Aug 16, 2024 · 1 comment
Open

"Socket is not connected" error after backgrounding #258

KaylaBrady opened this issue Aug 16, 2024 · 1 comment

Comments

@KaylaBrady
Copy link

I'm following the example for disconnecting from a socket when the app is backgrounded for my SwiftUI app. After calling socket.disconnect() using v5.3.3 of this library, I reliably get the following error message at PhoenixTransport.swift#L293 Error when receiving Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected" UserInfo={NSErrorFailingURLStringKey=wss://phoenix-chat.fly.dev/socket/websocket?vsn=2.0.0, NSErrorFailingURLKey=wss://phoenix-chat.fly.dev/socket/websocket?vsn=2.0.0}.

Here is an example app that doesn't involve joining any channels for simplicity, though I still get this error message when channels have been joined & left too.

I would expect no error message in this case since the socket has been intentionally closed.

struct ContentView: View {
    let socket: Socket = .init("https://phoenix-chat.fly.dev/socket/websocket")
    @Environment(\.scenePhase) var scenePhase

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .onAppear {
            socket.onOpen {
                print("Socket opened")
            }
            socket.onClose { code, reason in
                print("Socket closed \(code) \(reason)")
            }
        }.onChange(of: scenePhase) { newPhase in
            if newPhase == .active {
                socket.connect()
            }
            if newPhase == .background {
                socket.disconnect(code: .normal, reason: "Backgrounded", callback: nil)
            }
        }
        .padding()
    }

Here are the full logs from opening the app, backgrounding it, and re-opening it.

Socket opened
Socket closed 1000 Optional("Backgrounded")
Error when receiving Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected" UserInfo={NSErrorFailingURLStringKey=wss://phoenix-chat.fly.dev/socket/websocket?vsn=2.0.0, NSErrorFailingURLKey=wss://phoenix-chat.fly.dev/socket/websocket?vsn=2.0.0}
Socket opened
@dsrees
Copy link
Collaborator

dsrees commented Sep 14, 2024

This error is benign, the print statement in the receive loop can be removed.

Upon testing, self (which is the WebSocketTask) is nil and the reconnect logic is not triggered. The correct solution here is to just remove the print call on PhoenixTransport.swift:293.

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