Web socket - how to get error when other side closes?

I'm trying to communicate with my server using web sockets (URLSession, URLSessionWebSocketTask)

Code Block swift
let task = URLSession.shared.webSocketTask(with: url)
task.send(.data(...)) { ... }
task.receive { result in
switch result {
case .success(let message): ...
case .failure(let error): ...


When the server has an internal error and closes the connection, I don't get anything here in Swift on the client side. I've got a chain of Combine Publishers and I end up timing them out after 60 seconds.

Shouldn't the .failure path get called if the connection goes down? Is there another way to get that error when it happens, without waiting for the timeout?


Nevermind. I'd delete my post if I knew how.

The Swift code does get the failure result. It was the server side that was not closing the connection as expected.
Thanks for the follow up. Yes, one option to try as well would be forcing the error from a local server to see if it bubbles up through your failure handler.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Web socket - how to get error when other side closes?
 
 
Q