SRWebSocket
SRWebSocket is the primary class for interacting with a WebSocket server. It conforms to NSStreamDelegate internally.
Properties
delegate
@property (nonatomic, weak) id <SRWebSocketDelegate> delegate;
The delegate that handles events (open, message, error, close).
readyState
@property (atomic, assign, readonly) SRReadyState readyState;
The current state of the connection:
SR_CONNECTING(0)SR_OPEN(1)SR_CLOSING(2)SR_CLOSED(3)
url
@property (nullable, nonatomic, strong, readonly) NSURL *url;
The URL the socket is connected to.
protocol
@property (nullable, nonatomic, copy, readonly) NSString *protocol;
The negotiated sub-protocol (if any) returned by the server during the handshake.
Initializers
initWithURLRequest:
- (instancetype)initWithURLRequest:(NSURLRequest *)request;
Initializes the socket with a request object. This is useful for passing custom HTTP headers.
initWithURL:
- (instancetype)initWithURL:(NSURL *)url;
Convenience initializer for a simple URL.
initWithURLRequest:protocols:securityPolicy:
- (instancetype)initWithURLRequest:(NSURLRequest *)request
protocols:(nullable NSArray<NSString *> *)protocols
securityPolicy:(SRSecurityPolicy *)securityPolicy;
The designated initializer allowing specification of sub-protocols and custom security policies.
Methods
open
- (void)open;
Starts the connection process. This method must be called once.
close
- (void)close;
Closes the connection with the normal close code (1000).
closeWithCode:reason:
- (void)closeWithCode:(NSInteger)code reason:(nullable NSString *)reason;
Closes the connection with a specific status code and reason string.
sendString:error:
- (BOOL)sendString:(NSString *)string error:(NSError **)error;
Sends a UTF-8 string to the server.
sendData:error:
- (BOOL)sendData:(nullable NSData *)data error:(NSError **)error;
Sends binary data to the server.