WebSocketConnectionDelegate
public protocol WebSocketConnectionDelegate : AnyObject
Defines a delegate for a WebSocket connection.
-
Tells the delegate that the WebSocket did connect successfully.
Declaration
Swift
func webSocketDidConnect(connection: WebSocketConnection)
Parameters
connection
The active
WebSocketConnection
. -
Tells the delegate that the WebSocket did disconnect.
Declaration
Swift
func webSocketDidDisconnect(connection: WebSocketConnection, closeCode: NWProtocolWebSocket.CloseCode, reason: Data?)
Parameters
connection
The
WebSocketConnection
that disconnected.closeCode
A
NWProtocolWebSocket.CloseCode
describing how the connection closed.reason
Optional extra information explaining the disconnection. (Formatted as UTF-8 encoded
Data
). -
Tells the delegate that the WebSocket connection viability has changed.
An example scenario of when this method would be called is a Wi-Fi connection being lost due to a device moving out of signal range, and then the method would be called again once the device moved back in range.
Declaration
Swift
func webSocketViabilityDidChange(connection: WebSocketConnection, isViable: Bool)
Parameters
connection
The
WebSocketConnection
whose viability has changed.isViable
A
Bool
indicating if the connection is viable or not. -
Tells the delegate that the WebSocket has attempted a migration based on a better network path becoming available.
An example of when this method would be called is if a device is using a cellular connection, and a Wi-Fi connection becomes available. This method will also be called if a device loses a Wi-Fi connection, and a cellular connection is available.
Declaration
Swift
func webSocketDidAttemptBetterPathMigration(result: Result<WebSocketConnection, NWError>)
Parameters
result
A
Result
containing theWebSocketConnection
if the migration was successful, or aNWError
if the migration failed for some reason. -
Tells the delegate that the WebSocket received an error.
An error received by a WebSocket is not necessarily fatal.
Declaration
Swift
func webSocketDidReceiveError(connection: WebSocketConnection, error: NWError)
Parameters
connection
The
WebSocketConnection
that received an error.error
The
NWError
that was received. -
Tells the delegate that the WebSocket received a ‘pong’ from the server.
Declaration
Swift
func webSocketDidReceivePong(connection: WebSocketConnection)
Parameters
connection
The active
WebSocketConnection
. -
Tells the delegate that the WebSocket received a
String
message.Declaration
Swift
func webSocketDidReceiveMessage(connection: WebSocketConnection, string: String)
Parameters
connection
The active
WebSocketConnection
.string
The UTF-8 formatted
String
that was received. -
Tells the delegate that the WebSocket received a binary
Data
message.Declaration
Swift
func webSocketDidReceiveMessage(connection: WebSocketConnection, data: Data)
Parameters
connection
The active
WebSocketConnection
.data
The
Data
that was received.