SAPURLSessionWebSocketTask

@available(iOS 13.0, *)
public class SAPURLSessionWebSocketTask : SAPURLSessionTask

This class encapsulates a wrapper around Apple’s URLSessionWebSocketTask to integrate WebSocket functionality into the SDK

See: https://developer.apple.com/documentation/foundation/urlsessionwebsockettask *

  • Sends a ping frame from the client side, with a closure to receive the pong from the server endpoint.

    When sending multiple pings, the task always calls pongReceiveHandler in the order it sent the pings.

    Declaration

    Swift

    public func sendPing(pongReceiveHandler: @escaping @Sendable ((any Error)?) -> Void)

    Parameters

    pongReceiveHandler

    A closure called by the task when it receives the pong from the server. The closure receives an NSError that indicates a lost connection or other problem, or nil if no error occurred.

  • Sends a close frame with the given close code and optional close reason.

    If you call cancel() on the task instead of this method, it sends a cancellation frame with no close code or reason.

    Declaration

    Swift

    public func cancel(
        with closeCode: URLSessionWebSocketTask.CloseCode,
        reason: Data?
    )

    Parameters

    closeCode

    A URLSessionWebSocketTask.CloseCode that indicates the reason for closing the connection.

    reason

    Optional further information to explain the closing. The value of this parameter is defined by the endpoints, not by the standard.

  • Sends a WebSocket message, receiving the result in a completion handler.

    If an error occurs while sending the message, any outstanding work also fails.

    Declaration

    Swift

    @preconcurrency
    public func send(
        _ message: URLSessionWebSocketTask.Message,
        completionHandler: @escaping @Sendable ((any Error)?) -> Void
    )

    Parameters

    message

    The WebSocket message to send to the other endpoint.

    completionHandler

    A closure that receives an NSError that indicates an error encountered while sending, or nil if no error occurred.

  • Reads a WebSocket message once all the frames of the message are available.

    If the task reaches the maximumMessageSize while buffering the frames, this call fails with an error.

    Declaration

    Swift

    @preconcurrency
    public func receive(completionHandler: @escaping @Sendable (Result<URLSessionWebSocketTask.Message, any Error>) -> Void)

    Parameters

    completionHandler

    A closure that receives two parameters: the WebSocket message, and an NSError that indicates an error encountered while receiving the message. The error is nil if no error occurred.