NWWebSocket

open class NWWebSocket : WebSocketConnection

A WebSocket client that manages a socket connection.

Public properties

  • The WebSocket connection delegate.

    Declaration

    Swift

    public weak var delegate: WebSocketConnectionDelegate?
  • The default NWProtocolWebSocket.Options for a WebSocket connection.

    These options specify that the connection automatically replies to Ping messages instead of delivering them to the receiveMessage(data:context:) method.

    Declaration

    Swift

    public static var defaultOptions: NWProtocolWebSocket.Options { get }

Initialization

  • Creates a NWWebSocket instance which connects to a socket url with some configuration options.

    Declaration

    Swift

    public convenience init(request: URLRequest,
                            connectAutomatically: Bool = false,
                            options: NWProtocolWebSocket.Options = NWWebSocket.defaultOptions,
                            connectionQueue: DispatchQueue = .main)

    Parameters

    request

    The URLRequest containing the connection endpoint URL.

    connectAutomatically

    Determines if a connection should occur automatically on initialization. The default value is false.

    options

    The configuration options for the connection. The default value is NWWebSocket.defaultOptions.

    connectionQueue

    A DispatchQueue on which to deliver all connection events. The default value is .main.

  • Creates a NWWebSocket instance which connects a socket url with some configuration options.

    Declaration

    Swift

    public init(url: URL,
                connectAutomatically: Bool = false,
                options: NWProtocolWebSocket.Options = NWWebSocket.defaultOptions,
                connectionQueue: DispatchQueue = .main)

    Parameters

    url

    The connection endpoint URL.

    connectAutomatically

    Determines if a connection should occur automatically on initialization. The default value is false.

    options

    The configuration options for the connection. The default value is NWWebSocket.defaultOptions.

    connectionQueue

    A DispatchQueue on which to deliver all connection events. The default value is .main.

WebSocketConnection conformance

  • Connect to the WebSocket.

    Declaration

    Swift

    open func connect()
  • Send a UTF-8 formatted String over the WebSocket.

    Declaration

    Swift

    open func send(string: String)

    Parameters

    string

    The String that will be sent.

  • Send some Data over the WebSocket.

    Declaration

    Swift

    open func send(data: Data)

    Parameters

    data

    The Data that will be sent.

  • Start listening for messages over the WebSocket.

    Declaration

    Swift

    public func listen()
  • Ping the WebSocket periodically.

    Declaration

    Swift

    open func ping(interval: TimeInterval)

    Parameters

    interval

    The TimeInterval (in seconds) with which to ping the server.

  • Ping the WebSocket once.

    Declaration

    Swift

    open func ping()
  • Disconnect from the WebSocket.

    Declaration

    Swift

    open func disconnect(closeCode: NWProtocolWebSocket.CloseCode = .protocolCode(.normalClosure))

    Parameters

    closeCode

    The code to use when closing the WebSocket connection.