URLSessionWebSocketTask
: Part 1
URLSessionWebSocketTask
is the newest addition to Apple's Foundation networking library centered around URLSession
. It provides support for HTTP connections using the new (relative to HTTP at least) websocket standard (version 13). Web sockets establish a persistent connection to an HTTP server and standardize a common framing structure to pass data back and forth between the client and server. This allows for realtime communication without supporting additional standard like MQTT.
A URLSessionWebSocketTask
is created much like any other URLSessionTask
.
let task = URLSession.shared.webSocketTask(with: URL(string: "https://some.websocket.server")!)
task.resume()
However, unlike other URLSessionTask
s, creating and resuming a URLSessionWebSocketTask
isn't very useful by itself. In fact, the above example doesn't do anything since no receive handler has been added.