In our Mac application, we are creating a web-socket connection using NWConnection
and we are able to successfully establish the connection and read/write data from both sides. We have auth tokens which are sent in headers of NWProtocolWebSocket.Options
to the server. If token is good, server accepts the web-socket connection. As per RFC 6455
, if server does not want to accept the connection for any reason during web-socket handshake, it returns 403 status code. In our case, if cookies are not valid, server returns 403 during web-socket handshake.
However, we could not find a way to read this status code in Network.framework
. We are only getting failed state with NWError
which is .posix(53)
but there is no indication of the status code 403. We tried looking into protocol metadata on NWConnection
object and they are nil.
We tested the same using URLSessionWebSocketTask
where in failure callback method, we could see 403 status code on task.response
which means client is getting the code correctly from server.
So, is there a way to read the HTTP status code returned by server during web-socket handshake using Network.framework
?
The bug mentioned above (FB9878278
) remains unresolved.
You could probably work around this by doing the initial WebSocket handshake yourself then pushing the WebSocket protocol on to the protocol stack (setting skipHandshake
to avoid it trying to handshake again).. However, it’s not clear whether that’d be a win versus using a third-party WebSocket library.
Regarding third-party libraries, there’s on obvious candidate here, namely NIOWebSocket. I believe you’ll be able to combine that with SwiftNIO Transport Services, so you can continue using NWConnection
under the covers.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"