A URL session task that communicates over the WebSockets protocol standard.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
URLSession is a concrete subclass of
URLSession that provides a message-oriented transport protocol over TCP and TLS in the form of WebSocket framing. It follows the WebSocket Protocol defined in RFC 6455.
You create a
URLSession with either a
wss: URL. When creating the task, you can also provide a list of protocols to advertise during the handshake phase. Once the handshake completes, your app receives notifications through the session’s
You send data with
send(_: and receive data with
receive(completion. The task performs reads and writes asynchronously, and allows you to send and receive messages that contain both binary frames and UTF-8 encoded text frames. The task enqueues any reads or writes you perform prior to the handshake’s completion, and executes them after the handshake completes.
URLSession supports redirection and authentication like other types of tasks do, using the methods in
URLSession. The WebSocket task calls the redirection and authentication delegate methods prior to completing the handshake. The WebSocket task also supports cookies, by storing cookies to the session configuration’s
http, and attaches cookies to outgoing HTTP handshake requests.