title: Websockets through URLSessionWebSocketTask
body: <p>I'm currently having some trouble using the new URLSessionWebSocketTask in my WatchOS 6 application. The server side requires me to first send the authentication data through HTTP-headers before the websocket can be established. Since URLSessionWebSocketTask contains a function that takes an URLRequest as an argument, my approach is the following: </p>
  
  <pre><code> func establishSocket(){
  
      var urlRequest = URLRequest(url: URL(string: "https://censored.url”)!)
  
      urlRequest.addValue("applicationtype”, forHTTPHeaderField: “ApplicationType”)
      urlRequest.addValue(“authkey”, forHTTPHeaderField: “Authorization”)
      urlRequest.addValue(“productid”, forHTTPHeaderField: “ProductId”)
  
      let urlSession = URLSession(configuration: .ephemeral)
      let task = urlSession.webSocketTask(with: urlRequest)
      task.resume()
  
  }
  </code></pre>
  
  <p>I add all the necessary HTTP headers to the URLRequest and then try to establish a connection with the websocket. When the HTTP headers contain correct data it results in my application crashing with the stack trace exception "EXC_BAD_ACCESS (code=1, address=0x0)". If I intentionally alter the header data in order to be denied acccess then I'm just presented with a normal "Status Code: 403" when I try to connect. </p>
  
  <p>Am I missing something crucial in my code? I assume the problem is related to the HTTP updgrade process, but I can't quite put my finger on the issue. Does it have anything to do with the configuration for URLSession? I'm thankful for any help I can get!</p>
  
  <p>EDIT: Did some further digging and the server reports status 101 for the request, which means that I'm not handling the socket upgrade correctly in my code... Hmm</p>
  
  <p>EDIT2: The exact same code is working fine in iOS 13, this issue seems to be related to WatchOS specifically. Will keep investigating and see if I can fix it somehow.</p>
  
  <p>EDIT3: Tried Starscream now and it is working as long as I run the application on the simulator, it doesn't work on an actual Apple Watch though :( This is the output when I run the app on an Apple Watch:</p>
  
  <pre><code>2019-08-14 18:28:12.616649+0200 ______________ WatchKit Extension[279:27012] [] nw_connection_get_connected_socket [C87] Client called nw_connection_get_connected_socket on unconnected nw_connection
  2019-08-14 18:28:12.616784+0200 _______________ WatchKit Extension[279:27012] TCP Conn 0x14eadd00 Failed : error 0:50 [50]
  websocket is disconnected: Optional("The operation couldn’t be completed. Network is down")
  </code></pre>
  
  <p>Of course the actual WIFI network is not down on the Apple Watch. From what I gather this seems to be a quite common problem when going from simulator to physical device with certain libraries relying on network connections.</p>
  
  <p>I've also started a ticket with Apple on their Feedback assistant service, I'm still waiting for a proper answer though. I've currently managed to narrow the problem down to the same conclusion as this post on the Apple forums: <a href="https://forums.developer.apple.com/message/377041#377041" rel="nofollow noreferrer">https://forums.developer.apple.com/message/377041#377041</a></p>
  
  <p>Will keep you updated!</p>