Socket
public class Socket
extension Socket: WebSocketDelegate
Socket Connection
A single connection is established to the server and
channels are multiplexed over the connection.
Connect to the server using the Socket
class:
let socket = new Socket("/socket", paramsClosure: { ["userToken": "123" ] })
socket.connect()
The Socket
constructor takes the mount point of the socket,
the authentication params, as well as options that can be found in
the Socket docs, such as configuring the heartbeat.
-
The string WebSocket endpoint (ie
"ws://example.com/socket"
,"wss://example.com"
, etc.) That was passed to the Socket during initialization. The URL endpoint will be modified by the Socket to include"/websocket"
if missing.Declaration
Swift
public let endPoint: String
-
The fully qualified socket URL
Declaration
Swift
public private(set) var endPointUrl: URL
-
Resolves to return the
paramsClosure
result at the time of calling. If theSocket
was created with static params, then those will be returned every time.Declaration
Swift
public var params: Payload? { get }
-
The optional params closure used to get params whhen connecting. Must be set when initializaing the Socket.
Declaration
Swift
public let paramsClosure: PayloadClosure?
-
Override to provide custom encoding of data before writing to the socket
Declaration
Swift
public var encode: ([String : Any]) -> Data
-
Override to provide customd decoding of data read from the socket
Declaration
Swift
public var decode: (Data) -> [String : Any]?
-
Timeout to use when opening connections
Declaration
Swift
public var timeout: TimeInterval
-
Interval between sending a heartbeat
Declaration
Swift
public var heartbeatInterval: TimeInterval
-
Interval between socket reconnect attempts, in seconds
Declaration
Swift
public var reconnectAfter: (Int) -> TimeInterval
-
Interval between channel rejoin attempts, in seconds
Declaration
Swift
public var rejoinAfter: (Int) -> TimeInterval
-
The optional function to receive logs
Declaration
Swift
public var logger: ((String) -> Void)?
-
Disables heartbeats from being sent. Default is false.
Declaration
Swift
public var skipHeartbeat: Bool
-
Enable/Disable SSL certificate validation. Default is false. This must be set before calling
socket.connect()
in order to be appliedDeclaration
Swift
public var disableSSLCertValidation: Bool
-
Configure custom SSL validation logic, eg. SSL pinning. This must be set before calling
socket.connect()
in order to apply.Declaration
Swift
public var security: SSLTrustValidator?
-
Configure the encryption used by your client by setting the allowed cipher suites supported by your server. This must be set before calling
socket.connect()
in order to apply.Declaration
Swift
public var enabledSSLCipherSuites: [SSLCipherSuite]?
-
Undocumented
Declaration
Swift
public convenience init(_ endPoint: String, params: Payload? = nil)
-
Undocumented
Declaration
Swift
public convenience init(_ endPoint: String, paramsClosure: PayloadClosure?)
-
- return: The socket protocol, wss or ws
Declaration
Swift
public var websocketProtocol: String { get }
-
- return: True if the socket is connected
Declaration
Swift
public var isConnected: Bool { get }
-
Connects the Socket. The params passed to the Socket on initialization will be sent through the connection. If the Socket is already connected, then this call will be ignored.
Declaration
Swift
public func connect()
-
Disconnects the socket
- paramter callback: Optional. Called when disconnected
Declaration
Swift
public func disconnect(code: CloseCode = CloseCode.normal, callback: (() -> Void)? = nil)
Parameters
code
Optional. Closing status code
-
Registers callbacks for connection open events. Does not handle retain cycles. Use
delegateOnOpen(to:)
for automatic handling of retain cycles.Example:
socket.onOpen() { [weak self] in self?.print("Socket Connection Open") }
Declaration
Swift
public func onOpen(callback: @escaping () -> Void)
Parameters
callback
Called when the Socket is opened
-
Registers callbacks for connection open events. Automatically handles retain cycles. Use
onOpen()
to handle yourself.Example:
socket.delegateOnOpen(to: self) { self in self.print("Socket Connection Open") }
Declaration
Swift
public func delegateOnOpen<T: AnyObject>(to owner: T, callback: @escaping ((T) -> Void))
Parameters
owner
Class registering the callback. Usually
self
callback
Called when the Socket is opened
-
Registers callbacks for connection close events. Does not handle retain cycles. Use
delegateOnClose(_:)
for automatic handling of retain cycles.Example:
socket.onClose() { [weak self] in self?.print("Socket Connection Close") }
Declaration
Swift
public func onClose(callback: @escaping () -> Void)
Parameters
callback
Called when the Socket is closed
-
Registers callbacks for connection close events. Automatically handles retain cycles. Use
onClose()
to handle yourself.Example:
socket.delegateOnClose(self) { self in self.print("Socket Connection Close") }
Declaration
Swift
public func delegateOnClose<T: AnyObject>(to owner: T, callback: @escaping ((T) -> Void))
Parameters
owner
Class registering the callback. Usually
self
callback
Called when the Socket is closed
-
Registers callbacks for connection error events. Does not handle retain cycles. Use
delegateOnError(to:)
for automatic handling of retain cycles.Example:
socket.onError() { [weak self] (error) in self?.print("Socket Connection Error", error) }
Declaration
Swift
public func onError(callback: @escaping (Error) -> Void)
Parameters
callback
Called when the Socket errors
-
Registers callbacks for connection error events. Automatically handles retain cycles. Use
manualOnError()
to handle yourself.Example:
socket.delegateOnError(to: self) { (self, error) in self.print("Socket Connection Error", error) }
Declaration
Swift
public func delegateOnError<T: AnyObject>(to owner: T, callback: @escaping ((T, Error) -> Void))
Parameters
owner
Class registering the callback. Usually
self
callback
Called when the Socket errors
-
Registers callbacks for connection message events. Does not handle retain cycles. Use
delegateOnMessage(_to:)
for automatic handling of retain cycles.Example:
socket.onMessage() { [weak self] (message) in self?.print("Socket Connection Message", message) }
Declaration
Swift
public func onMessage(callback: @escaping (Message) -> Void)
Parameters
callback
Called when the Socket receives a message event
-
Registers callbacks for connection message events. Automatically handles retain cycles. Use
onMessage()
to handle yourself.Example:
socket.delegateOnMessage(self) { (self, message) in self.print("Socket Connection Message", message) }
Declaration
Swift
public func delegateOnMessage<T: AnyObject>(to owner: T, callback: @escaping ((T, Message) -> Void))
Parameters
owner
Class registering the callback. Usually
self
callback
Called when the Socket receives a message event
-
Releases all stored callback hooks (onError, onOpen, onClose, etc.) You should call this method when you are finished when the Socket in order to release any references held by the socket.
Declaration
Swift
public func releaseCallbacks()
-
Initialize a new Channel
Example:
let channel = socket.channel("rooms", params: ["user_id": "abc123"])
- return: A new channel
Declaration
Swift
public func channel(_ topic: String, params: [String: Any] = [:]) -> Channel
Parameters
topic
Topic of the channel
params
Optional. Parameters for the channel
-
Removes the Channel from the socket. This does not cause the channel to inform the server that it is leaving. You should call channel.leave() prior to removing the Channel.
Example:
channel.leave() socket.remove(channel)
Declaration
Swift
public func remove(_ channel: Channel)
Parameters
channel
Channel to remove
-
- return: the next message ref, accounting for overflows
Declaration
Swift
public func makeRef() -> String
-
Undocumented
Declaration
Swift
public func websocketDidConnect(socket: WebSocketClient)
-
Undocumented
Declaration
Swift
public func websocketDidDisconnect(socket: WebSocketClient, error: Error?)
-
Undocumented
Declaration
Swift
public func websocketDidReceiveMessage(socket: WebSocketClient, text: String)
-
Undocumented
Declaration
Swift
public func websocketDidReceiveData(socket: WebSocketClient, data: Data)