SocketIOClientSpec
public protocol SocketIOClientSpec : class
Defines the interface for a SocketIOClient.
-
A handler that will be called on any event.
Declaration
Swift
var anyHandler: ((SocketAnyEvent) -> ())? -
The configuration for this client.
Declaration
Swift
var config: SocketIOClientConfiguration -
The queue that all interaction with the client must be on.
Declaration
Swift
var handleQueue: DispatchQueue -
The array of handlers for this socket.
Declaration
Swift
var handlers: [SocketEventHandler] -
The namespace that this socket is currently connected to.
Must start with a
/.Declaration
Swift
var nsp: String -
The status of this client.
Declaration
Swift
var status: SocketIOClientStatus
-
Connect to the server. The same as calling
connect(timeoutAfter:withHandler:)with a timeout of 0.Only call after adding your event listeners, unless you know what you’re doing.
Declaration
Swift
func connect() -
Connect to the server. If we aren’t connected after
timeoutAfterseconds, thenwithHandleris called.Only call after adding your event listeners, unless you know what you’re doing.
Declaration
Swift
func connect(timeoutAfter: Double, withHandler handler: (() -> ())?)Parameters
timeoutAfterThe number of seconds after which if we are not connected we assume the connection has failed. Pass 0 to never timeout.
withHandlerThe handler to call when the client fails to connect.
-
Called when the client connects to a namespace. If the client was created with a namespace upfront, then this is only called when the client connects to that namespace.
Declaration
Swift
func didConnect(toNamespace namespace: String)Parameters
toNamespaceThe namespace that was connected to.
-
Called when the client has disconnected from socket.io.
Declaration
Swift
func didDisconnect(reason: String)Parameters
reasonThe reason for the disconnection.
-
didError(reason:)Default implementationCalled when the client encounters an error.
Default Implementation
Default implementation.
Declaration
Swift
func didError(reason: String)Parameters
reasonThe reason for the disconnection.
-
Disconnects the socket.
Declaration
Swift
func disconnect() -
Send an event to the server, with optional data items.
If an error occurs trying to transform
itemsinto their socket representation, aSocketClientEvent.errorwill be emitted. The structure of the error data is[eventName, items, theError]Declaration
Swift
func emit(_ event: String, _ items: SocketData...)Parameters
eventThe event to send.
itemsThe items to send with this event. May be left out.
-
Call when you wish to tell the server that you’ve received the event for
ack.Declaration
Swift
func emitAck(_ ack: Int, with items: [Any])Parameters
ackThe ack number.
withThe data for this ack.
-
Sends a message to the server, requesting an ack.
NOTE: It is up to the server send an ack back, just calling this method does not mean the server will ack. Check that your server’s api will ack the event being sent.
If an error occurs trying to transform
itemsinto their socket representation, aSocketClientEvent.errorwill be emitted. The structure of the error data is[eventName, items, theError]Example:
socket.emitWithAck("myEvent", 1).timingOut(after: 1) {data in ... }Declaration
Swift
func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallbackParameters
eventThe event to send.
itemsThe items to send with this event. May be left out.
Return Value
An
OnAckCallback. You must call thetimingOut(after:)method before the event will be sent. -
Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called.
Declaration
Swift
func handleAck(_ ack: Int, data: [Any])Parameters
ackThe number for this ack.
dataThe data sent back with this ack.
-
Called when we get an event from socket.io.
Declaration
Swift
func handleEvent(_ event: String, data: [Any], isInternalMessage: Bool, withAck ack: Int)Parameters
eventThe name of the event.
dataThe data that was sent with this event.
isInternalMessageWhether this event was sent internally. If
trueit is always sent to handlers.withAckIf > 0 then this event expects to get an ack back from the client.
-
Called on socket.io specific events.
Declaration
Swift
func handleClientEvent(_ event: SocketClientEvent, data: [Any])Parameters
eventThe
SocketClientEvent.dataThe data for this event.
-
Call when you wish to leave a namespace and return to the default namespace.
Declaration
Swift
func leaveNamespace() -
Joins
namespace.Do not use this to join the default namespace. Instead call
leaveNamespace.Declaration
Swift
func joinNamespace(_ namespace: String)Parameters
namespaceThe namespace to join.
-
Removes handler(s) for a client event.
If you wish to remove a client event handler, call the
off(id:)with the UUID received from itsoncall.Declaration
Swift
func off(clientEvent event: SocketClientEvent)Parameters
clientEventThe event to remove handlers for.
-
Removes handler(s) based on an event name.
If you wish to remove a specific event, call the
off(id:)with the UUID received from itsoncall.Declaration
Swift
func off(_ event: String)Parameters
eventThe event to remove handlers for.
-
Removes a handler with the specified UUID gotten from an
onoronceIf you want to remove all events for an event, call the off
off(_:)method with the event name.Declaration
Swift
func off(id: UUID)Parameters
idThe UUID of the handler you wish to remove.
-
Adds a handler for an event.
Declaration
Swift
func on(_ event: String, callback: @escaping NormalCallback) -> UUIDParameters
eventThe event name for this handler.
callbackThe callback that will execute when this event is received.
Return Value
A unique id for the handler that can be used to remove it.
-
Adds a handler for a client event.
Example:
socket.on(clientEvent: .connect) {data, ack in ... }Declaration
Swift
func on(clientEvent event: SocketClientEvent, callback: @escaping NormalCallback) -> UUIDParameters
eventThe event for this handler.
callbackThe callback that will execute when this event is received.
Return Value
A unique id for the handler that can be used to remove it.
-
Adds a single-use handler for a client event.
Declaration
Swift
func once(clientEvent event: SocketClientEvent, callback: @escaping NormalCallback) -> UUIDParameters
clientEventThe event for this handler.
callbackThe callback that will execute when this event is received.
Return Value
A unique id for the handler that can be used to remove it.
-
Adds a single-use handler for an event.
Declaration
Swift
func once(_ event: String, callback: @escaping NormalCallback) -> UUIDParameters
eventThe event name for this handler.
callbackThe callback that will execute when this event is received.
Return Value
A unique id for the handler that can be used to remove it.
-
Adds a handler that will be called on every event.
Declaration
Swift
func onAny(_ handler: @escaping (SocketAnyEvent) -> ())Parameters
handlerThe callback that will execute whenever an event is received.
-
Tries to reconnect to the server.
Declaration
Swift
func reconnect() -
Removes all handlers.
Can be used after disconnecting to break any potential remaining retain cycles.
Declaration
Swift
func removeAllHandlers()
SocketIOClientSpec Protocol Reference