Post

Replies

Boosts

Views

Activity

NWConnection (web-socket) does not disconnect when server disconnects gracefully
We are building a macOS client where we make a web socket connection to our server using NWConnection. The code to to create NWParameters is: let options = NWProtocolTLS.Options() let securityProtocolOptions = options.securityProtocolOptions //....configure security options for server and client cert validation... let parameters = NWParameters(tls: options) let wsOptions = NWProtocolWebSocket.Options() wsOptions.autoReplyPing = true wsOptions.setAdditionalHeaders(additionalHeaders.map { ($0.key, $0.value) } ) parameters.defaultProtocolStack.applicationProtocols.insert(options, at: 0) With these parameters and an NWEndpoint object, we create a connection which connects and transfers data well from both sides. However, whenever server gracefully closes the connection, the client remains oblivious to this and does not close the connection. It only detects a timeout when trying to send some data over the connection after the server disconnect. We looked into Wireshark and we do not see any FIN,ACK or RST packets being received from server. However, in our windows client, when same exact server closes the connection, we are seeing FIN, ACK and connection immediately closes on client side as well. We also tried to test same behaviour in golang with a small snippet created by our team member running on Mac itself. This snippet also receives FIN,ACK from server and closes the connection immediately. Only NWConnection in our Mac client does not receive close connection. So, the question arises, why is NWConnection not receiving FIN,ACK and not closing the connection when a windows as well as a golang client on Mac is able to. Is there any extra configuration required for NWConnection or NWParameter? Is the NWParameter creation code correct? We already checked and we are continuously calling receiveMessage on the NWConnection object. So, missing read is not the issue here. Also, I do not see any connection timeout option in NWProtocolWebSocket but it exists in NWProtocolTCP. So, is there a way to set connection timeout for web socket connection using NWConnection?
6
0
2.6k
May ’23
Get HTTP Status code from NWConnection (web-socket) handshake failure
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 NWErrorwhich 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?
3
0
1.2k
Mar ’25