ARTSRWebSocket

Objective-C

@interface ARTSRWebSocket : NSObject <ARTWebSocket, NSStreamDelegate>

Swift

class ARTSRWebSocket : NSObject, ARTWebSocket, StreamDelegate

A ARTSRWebSocket object lets you connect, send and receive data to a remote Web Socket.

  • The delegate of the web socket.

    The web socket delegate is notified on all state changes that happen to the web socket.

    Declaration

    Objective-C

    @property (nonatomic, weak) id<ARTWebSocketDelegate> _Nullable delegate;

    Swift

    weak var delegate: (any ARTWebSocketDelegate)? { get set }
  • A dispatch queue for scheduling the delegate calls. The queue doesn’t need be a serial queue.

    If nil and delegateOperationQueue is nil, the socket uses main queue for performing all delegate method calls.

    Declaration

    Objective-C

    @property (nonatomic, nullable) dispatch_queue_t delegateDispatchQueue;

    Swift

    var delegateDispatchQueue: dispatch_queue_t? { get set }
  • An operation queue for scheduling the delegate calls.

    If nil and delegateOperationQueue is nil, the socket uses main queue for performing all delegate method calls.

    Declaration

    Objective-C

    @property (nonatomic, nullable) NSOperationQueue *delegateOperationQueue;

    Swift

    var delegateOperationQueue: OperationQueue? { get set }
  • Current ready state of the socket. Default: ARTWebSocketReadyStateConnecting.

    This property is Key-Value Observable and fully thread-safe.

    Declaration

    Objective-C

    @property (readonly) ARTWebSocketReadyState readyState;

    Swift

    var readyState: ARTWebSocketReadyState { get }
  • url

    An instance of NSURL that this socket connects to.

    Declaration

    Objective-C

    @property (nonatomic, readonly, nullable) NSURL *url;

    Swift

    var url: URL? { get }
  • All HTTP headers that were received by socket or nil if none were received so far.

    Declaration

    Objective-C

    @property (nonatomic, readonly, nullable) CFHTTPMessageRef receivedHTTPHeaders;

    Swift

    var receivedHTTPHeaders: CFHTTPMessage? { get }
  • Array of NSHTTPCookie cookies to apply to the connection.

    Declaration

    Objective-C

    @property (nonatomic, copy, nullable) NSArray<NSHTTPCookie *> *requestCookies;

    Swift

    var requestCookies: [HTTPCookie]? { get set }
  • The negotiated web socket protocol or nil if handshake did not yet complete.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly, nullable) NSString *protocol;

    Swift

    var `protocol`: String? { get }
  • A boolean value indicating whether this socket will allow connection without SSL trust chain evaluation. For DEBUG builds this flag is ignored, and SSL connections are allowed regardless of the certificate trust configuration

    Declaration

    Objective-C

    @property (nonatomic, readonly) BOOL allowsUntrustedSSLCertificates;

    Swift

    var allowsUntrustedSSLCertificates: Bool { get }

Constructors

  • Initializes a web socket with a given NSURLRequest.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURLRequest:(nonnull NSURLRequest *)request
                                        logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(urlRequest request: URLRequest, logger: InternalLog?)

    Parameters

    request

    Request to initialize with.

  • Initializes a web socket with a given NSURLRequest, specifying a transport security policy (e.g. SSL configuration).

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURLRequest:(nonnull NSURLRequest *)request
                                securityPolicy:
                                    (nonnull ARTSRSecurityPolicy *)securityPolicy
                                        logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(urlRequest request: URLRequest, securityPolicy: ARTSRSecurityPolicy, logger: InternalLog?)

    Parameters

    request

    Request to initialize with.

    securityPolicy

    Policy object describing transport security behavior.

  • Initializes a web socket with a given NSURLRequest and list of sub-protocols.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURLRequest:(nonnull NSURLRequest *)request
                                     protocols:
                                         (nullable NSArray<NSString *> *)protocols
                                        logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(urlRequest request: URLRequest, protocols: [String]?, logger: InternalLog?)

    Parameters

    request

    Request to initialize with.

    protocols

    An array of strings that turn into Sec-WebSocket-Protocol. Default: nil.

  • Deprecated

    Disabling certificate chain validation is unsafe. Please use a proper Certificate Authority to issue your TLS certificates.

    Initializes a web socket with a given NSURLRequest, list of sub-protocols and whether untrusted SSL certificates are allowed.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURLRequest:(nonnull NSURLRequest *)request
                                     protocols:
                                         (nullable NSArray<NSString *> *)protocols
                allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates
                                        logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(urlRequest request: URLRequest, protocols: [String]?, allowsUntrustedSSLCertificates: Bool, logger: InternalLog?)

    Parameters

    request

    Request to initialize with.

    protocols

    An array of strings that turn into Sec-WebSocket-Protocol. Default: nil.

    allowsUntrustedSSLCertificates

    Boolean value indicating whether untrusted SSL certificates are allowed. Default: false.

  • Initializes a web socket with a given NSURLRequest, list of sub-protocols and whether untrusted SSL certificates are allowed.

    Declaration

    Objective-C

    - (nonnull instancetype)
        initWithURLRequest:(nonnull NSURLRequest *)request
                 protocols:(nullable NSArray<NSString *> *)protocols
            securityPolicy:(nonnull ARTSRSecurityPolicy *)securityPolicy
                    logger:(nullable ARTInternalLog *)logger;

    Swift

    init(urlRequest request: URLRequest, protocols: [String]?, securityPolicy: ARTSRSecurityPolicy, logger: InternalLog?)

    Parameters

    request

    Request to initialize with.

    protocols

    An array of strings that turn into Sec-WebSocket-Protocol. Default: nil.

    securityPolicy

    Policy object describing transport security behavior.

  • Initializes a web socket with a given NSURL.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURL:(nonnull NSURL *)url
                                 logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(url: URL, logger: InternalLog?)

    Parameters

    url

    URL to initialize with.

  • Initializes a web socket with a given NSURL and list of sub-protocols.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURL:(nonnull NSURL *)url
                              protocols:(nullable NSArray<NSString *> *)protocols
                                 logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(url: URL, protocols: [String]?, logger: InternalLog?)

    Parameters

    url

    URL to initialize with.

    protocols

    An array of strings that turn into Sec-WebSocket-Protocol. Default: nil.

  • Initializes a web socket with a given NSURL, specifying a transport security policy (e.g. SSL configuration).

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURL:(nonnull NSURL *)url
                         securityPolicy:
                             (nonnull ARTSRSecurityPolicy *)securityPolicy
                                 logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(url: URL, securityPolicy: ARTSRSecurityPolicy, logger: InternalLog?)

    Parameters

    url

    URL to initialize with.

    securityPolicy

    Policy object describing transport security behavior.

  • Deprecated

    Disabling certificate chain validation is unsafe. Please use a proper Certificate Authority to issue your TLS certificates.

    Initializes a web socket with a given NSURL, list of sub-protocols and whether untrusted SSL certificates are allowed.

    Declaration

    Objective-C

    - (nonnull instancetype)initWithURL:(nonnull NSURL *)url
                              protocols:(nullable NSArray<NSString *> *)protocols
         allowsUntrustedSSLCertificates:(BOOL)allowsUntrustedSSLCertificates
                                 logger:(nullable ARTInternalLog *)logger;

    Swift

    convenience init(url: URL, protocols: [String]?, allowsUntrustedSSLCertificates: Bool, logger: InternalLog?)

    Parameters

    url

    URL to initialize with.

    protocols

    An array of strings that turn into Sec-WebSocket-Protocol. Default: nil.

    allowsUntrustedSSLCertificates

    Boolean value indicating whether untrusted SSL certificates are allowed. Default: false.

  • Unavailable

    Unavailable initializer. Please use any other initializer.

    Declaration

    Objective-C

    - (nonnull instancetype)init;
  • Unavailable

    Unavailable constructor. Please use any other initializer.

    Declaration

    Objective-C

    + (nonnull instancetype)new;

Schedule

  • Schedules a received on a given run loop in a given mode. By default, a web socket will schedule itself on +[NSRunLoop ARTSR_networkRunLoop] using NSDefaultRunLoopMode.

    Declaration

    Objective-C

    - (void)scheduleInRunLoop:(nonnull NSRunLoop *)runLoop
                      forMode:(nonnull NSString *)mode;

    Swift

    func schedule(in runLoop: RunLoop, forMode mode: String)

    Parameters

    runLoop

    The run loop on which to schedule the receiver.

    mode

    The mode for the run loop.

  • Removes the receiver from a given run loop running in a given mode.

    Declaration

    Objective-C

    - (void)unscheduleFromRunLoop:(nonnull NSRunLoop *)runLoop
                          forMode:(nonnull NSString *)mode;

    Swift

    func unschedule(from runLoop: RunLoop, forMode mode: String)

    Parameters

    runLoop

    The run loop on which the receiver was scheduled.

    mode

    The mode for the run loop.

Open / Close

  • Opens web socket, which will trigger connection, authentication and start receiving/sending events. An instance of ARTSRWebSocket is intended for one-time-use only. This method should be called once and only once.

    Declaration

    Objective-C

    - (void)open;

    Swift

    func open()
  • Closes a web socket using ARTSRStatusCodeNormal code and no reason.

    Declaration

    Objective-C

    - (void)close;

    Swift

    func close()
  • Closes a web socket using a given code and reason.

    Declaration

    Objective-C

    - (void)closeWithCode:(NSInteger)code reason:(nullable NSString *)reason;

    Swift

    func close(withCode code: Int, reason: String?)

    Parameters

    code

    Code to close the socket with.

    reason

    Reason to send to the server or nil.

Send

  • Send a UTF-8 string or binary data to the server.

    Declaration

    Objective-C

    - (void)send:(nullable id)message;

    Swift

    func send(_ message: Any?)

    Parameters

    message

    UTF-8 String or Data to send.

  • Send a UTF-8 String to the server.

    Declaration

    Objective-C

    - (BOOL)sendString:(nonnull NSString *)string
                 error:(NSError *_Nullable *_Nullable)error;

    Swift

    func send(string: String) throws

    Parameters

    string

    String to send.

    error

    On input, a pointer to variable for an NSError object. If an error occurs, this pointer is set to an NSError object containing information about the error. You may specify nil to ignore the error information.

    Return Value

    YES if the string was scheduled to send, otherwise - NO.

  • Send binary data to the server.

    Declaration

    Objective-C

    - (BOOL)sendData:(nullable NSData *)data
               error:(NSError *_Nullable *_Nullable)error;

    Swift

    func send(data: Data?) throws

    Parameters

    data

    Data to send.

    error

    On input, a pointer to variable for an NSError object. If an error occurs, this pointer is set to an NSError object containing information about the error. You may specify nil to ignore the error information.

    Return Value

    YES if the string was scheduled to send, otherwise - NO.

  • Send binary data to the server, without making a defensive copy of it first.

    Declaration

    Objective-C

    - (BOOL)sendDataNoCopy:(nullable NSData *)data
                     error:(NSError *_Nullable *_Nullable)error;

    Swift

    func send(dataNoCopy data: Data?) throws

    Parameters

    data

    Data to send.

    error

    On input, a pointer to variable for an NSError object. If an error occurs, this pointer is set to an NSError object containing information about the error. You may specify nil to ignore the error information.

    Return Value

    YES if the string was scheduled to send, otherwise - NO.

  • Send Ping message to the server with optional data.

    Declaration

    Objective-C

    - (BOOL)sendPing:(nullable NSData *)data
               error:(NSError *_Nullable *_Nullable)error;

    Swift

    func sendPing(_ data: Data?) throws

    Parameters

    data

    Instance of NSData or nil.

    error

    On input, a pointer to variable for an NSError object. If an error occurs, this pointer is set to an NSError object containing information about the error. You may specify nil to ignore the error information.

    Return Value

    YES if the string was scheduled to send, otherwise - NO.