Using NSURLSessionStreamTask for WebSockets
Using NSURLSessionStreamTask for WebSockets
- Subject: Using NSURLSessionStreamTask for WebSockets
- From: Jens Alfke <email@hidden>
- Date: Tue, 14 Mar 2017 15:36:03 -0700
So I’ve been hoping that NSURLSessionStreamTask would make it easier to implement WebSockets by allowing me to make the initial HTTP handshake as a regular HTTP request with an NSURLSessionDataTask, then switch to a raw stream by returning NSURLSessionResponseBecomeStream from the response delegate method.
I just tried it (on macOS 10.12.4 beta 4) and unfortunately it doesn’t work. Upgrading protocols requires that the value of the request's “Connection:” header be “Upgrade", but the value gets changed to “keep-alive” when sent, causing the server to reject the request. I assume this is one of those headers that the client can’t control. :(
I can instead create the task as a stream from the beginning by calling -streamTaskWithHostName:port:. But then I’m basically working with raw TCP, so I need to construct and parse the HTTP messages myself, and more importantly, I lose SSL support and the semi-automatic handling of proxies, redirects and auth challenges. (It’s really no better than NSStream.)
Is this something that could be fixed in the future, or is it considered a non-feature for NSURLSession to allow HTTP connection upgrades?
—Jens
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden