Post

Replies

Boosts

Views

Activity

Reply to URLSessionWebSocketTask does not notify delegate when connection fails
Thanks for your reply. Well, I would recommend that you just try to send data on the connection and let the error handle it if your connection is unavailable and then bubble that state up through your logic. Sending messages in this state completes without any error. Only after the timeout error I mentioned has fired anyways does sending a message result in an error. So this does not really solve the problem.
Apr ’21
Reply to URLSessionWebSocketTask does not notify delegate when connection fails
Wow, that is peculiar, because I see the following (I tested this on macOS 11.2.3, and also on 10.15). 2021-04-21 10:21:37.405632+0200 TestWS[33054:327404] sending message 2 2021-04-21 10:21:37.406341+0200 TestWS[33054:327901] did send message 2, error: nil 2021-04-21 10:21:47.404558+0200 TestWS[33054:327404] sending message 3 2021-04-21 10:21:47.405089+0200 TestWS[33054:327901] did send message 3, error: nil 2021-04-21 10:21:50.075241+0200 TestWS[33054:328200] Connection 1: encountered error(1:53) == Wifi capped here 2021-04-21 10:21:57.403576+0200 TestWS[33054:327404] sending message 4 2021-04-21 10:21:57.404152+0200 TestWS[33054:328571] did send message 4, error: nil == no error sending a message 2021-04-21 10:22:07.404181+0200 TestWS[33054:327404] sending message 5 2021-04-21 10:22:07.404884+0200 TestWS[33054:328640] did send message 5, error: nil == no error sending a message 2021-04-21 10:22:17.403530+0200 TestWS[33054:327404] sending message 6 2021-04-21 10:22:17.404037+0200 TestWS[33054:328731] did send message 6, error: nil == no error sending a message 2021-04-21 10:22:27.403033+0200 TestWS[33054:327404] sending message 7 2021-04-21 10:22:27.403717+0200 TestWS[33054:328816] did send message 7, error: nil == no error sending a message 2021-04-21 10:22:30.558273+0200 TestWS[33054:328816] [connection] nw_socket_handle_socket_event [C1.1:3] Socket SO_ERROR [60: Operation timed out] == finally we get the timeout error 2021-04-21 10:22:30.558689+0200 TestWS[33054:328816] [connection] nw_read_request_report [C1] Receive failed with error "Operation timed out" 2021-04-21 10:22:30.559008+0200 TestWS[33054:328640] [websocket] Read completed with an error Operation timed out 2021-04-21 10:22:37.402583+0200 TestWS[33054:327404] sending message 8 2021-04-21 10:22:37.403010+0200 TestWS[33054:328920] did send message 8, error: Error Domain=kNWErrorDomainPOSIX Code=60 "Operation timed out" UserInfo={NSDescription=Operation timed out} == and only after that does sending a message give an error One can see that after capping the connection, sending messages still completes without any error for 40 seconds, until the timeout error occurs. This makes me wonder if I misconfigure the session somehow? Basically, what I'm doing is the following: Swift public class WSService: NSObject {     lazy var queue = OperationQueue()     lazy var request: URLRequest = {         var request = URLRequest(url: URL(string: "wss://echo.websocket.org")!)         request.networkServiceType = .responsiveData         request.timeoutInterval = 10         return request     }()     lazy var session: URLSession = {         URLSession(configuration: .ephemeral, delegate: self, delegateQueue: self.queue)     }()     lazy var task = session.webSocketTask(with: self.request)     public func start() {         self.task.resume()     }     var msgCount: Int = 0     func sendMessage(_ string: String) {         let theCount = msgCount         msgCount += 1         os_log("sending message %i", theCount)         self.task.send(.string(string)) { error in             os_log("did send message %i, error: %@", theCount, error == nil ? "nil" : error.debugDescription)             return         }     } }
Apr ’21