Back to Socket Io Client Swift

SocketManager

docs/Classes/SocketManager.html

16.1.112.7 KB
Original Source

SocketManager

open class SocketManager : NSObject, SocketManagerSpec, SocketParsable, SocketDataBufferable, ConfigSettable

A manager for a socket.io connection.

A SocketManager is responsible for multiplexing multiple namespaces through a single SocketEngineSpec.

Example:

let manager = SocketManager(socketURL: URL(string:"http://localhost:8080/")!)
let defaultNamespaceSocket = manager.defaultSocket
let swiftSocket = manager.socket(forNamespace: "/swift")

// defaultNamespaceSocket and swiftSocket both share a single connection to the server

Sockets created through the manager are retained by the manager. So at the very least, a single strong reference to the manager must be maintained to keep sockets alive.

To disconnect a socket and remove it from the manager, either call SocketIOClient.disconnect() on the socket, or call one of the disconnectSocket methods on this class.

NOTE : The manager is not thread/queue safe, all interaction with the manager should be done on the handleQueue

Properties

`

                defaultSocket
                `

The socket associated with the default namespace (“/”).

Declaration

Swift

public var defaultSocket: SocketIOClient { get }

`

                socketURL
                `

The URL of the socket.io server.

If changed after calling init, forceNew must be set to true, or it will only connect to the url set in the init.

Declaration

Swift

public let socketURL: URL

`

                config
                `

The configuration for this client.

Some configs will not take affect until after a reconnect if set after calling a connect method.

Declaration

Swift

public var config: SocketIOClientConfiguration { get set }

`

                engine
                `

The engine for this manager.

Declaration

Swift

public var engine: SocketEngineSpec?

`

                forceNew
                `

If true then every time connect is called, a new engine will be created.

Declaration

Swift

public var forceNew: Bool

`

                handleQueue
                `

The queue that all interaction with the client should occur on. This is the queue that event handlers are called on.

This should be a serial queue! Concurrent queues are not supported and might cause crashes and races.

Declaration

Swift

public var handleQueue: DispatchQueue

`

                nsps
                `

The sockets in this manager indexed by namespace.

Declaration

Swift

public var nsps: [String : SocketIOClient]

`

                reconnects
                `

If true, this client will try and reconnect on any disconnects.

Declaration

Swift

public var reconnects: Bool

`

                reconnectWait
                `

The minimum number of seconds to wait before attempting to reconnect.

Declaration

Swift

public var reconnectWait: Int

`

                reconnectWaitMax
                `

The maximum number of seconds to wait before attempting to reconnect.

Declaration

Swift

public var reconnectWaitMax: Int

`

                randomizationFactor
                `

The randomization factor for calculating reconnect jitter.

Declaration

Swift

public var randomizationFactor: Double

`

                status
                `

The status of this manager.

Declaration

Swift

public private(set) var status: SocketIOStatus { get set }

`

                version
                `

Declaration

Swift

public private(set) var version: SocketIOVersion { get }

`

                waitingPackets
                `

A list of packets that are waiting for binary data.

The way that socket.io works all data should be sent directly after each packet. So this should ideally be an array of one packet waiting for data.

This should not be modified directly.

Declaration

Swift

public var waitingPackets: [SocketPacket]

Initializers

`

                init(socketURL:config:)
                `

Type safe way to create a new SocketIOClient. opts can be omitted.

Declaration

Swift

public init(socketURL: URL, config: SocketIOClientConfiguration = [])

Parameters

| socketURL |

The url of the socket.io server.

| | config |

The config for this socket.

|

`

                init(socketURL:config:)
                `

Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity. If using Swift it’s recommended to use init(socketURL: NSURL, options: Set<SocketIOClientOption>)

Declaration

Swift

@objc
public convenience init(socketURL: URL, config: [String : Any]?)

Parameters

| socketURL |

The url of the socket.io server.

| | config |

The config for this socket.

|

Methods

`

                connect()
                `

Connects the underlying transport and the default namespace socket.

Override if you wish to attach a custom SocketEngineSpec.

Declaration

Swift

open func connect()

`

                connectSocket(_:withPayload:)
                `

Connects a socket through this manager’s engine.

Declaration

Swift

open func connectSocket(_ socket: SocketIOClient, withPayload payload: [String : Any]? = nil)

Parameters

| socket |

The socket who we should connect through this manager.

| | withPayload |

Optional payload to send on connect

|

`

                didDisconnect(reason:)
                `

Called when the manager has disconnected from socket.io.

Declaration

Swift

open func didDisconnect(reason: String)

Parameters

| reason |

The reason for the disconnection.

|

`

                disconnect()
                `

Disconnects the manager and all associated sockets.

Declaration

Swift

open func disconnect()

`

                disconnectSocket(_:)
                `

Disconnects the given socket.

This will remove the socket for the manager’s control, and make the socket instance useless and ready for releasing.

Declaration

Swift

open func disconnectSocket(_ socket: SocketIOClient)

Parameters

| socket |

The socket to disconnect.

|

`

                disconnectSocket(forNamespace:)
                `

Disconnects the socket associated with forNamespace.

This will remove the socket for the manager’s control, and make the socket instance useless and ready for releasing.

Declaration

Swift

open func disconnectSocket(forNamespace nsp: String)

Parameters

| nsp |

The namespace to disconnect from.

|

`

                emitAll(clientEvent:data:)
                `

Sends a client event to all sockets in nsps

Declaration

Swift

open func emitAll(clientEvent event: SocketClientEvent, data: [Any])

Parameters

| clientEvent |

The event to emit.

|

`

                emitAll(_:_:)
                `

Sends an event to the server on all namespaces in this manager.

Declaration

Swift

open func emitAll(_ event: String, _ items: SocketData...)

Parameters

| event |

The event to send.

| | items |

The data to send with this event.

|

`

                engineDidClose(reason:)
                `

Called when the engine closes.

Declaration

Swift

open func engineDidClose(reason: String)

Parameters

| reason |

The reason that the engine closed.

|

`

                engineDidError(reason:)
                `

Called when the engine errors.

Declaration

Swift

open func engineDidError(reason: String)

Parameters

| reason |

The reason the engine errored.

|

`

                engineDidOpen(reason:)
                `

Called when the engine opens.

Declaration

Swift

open func engineDidOpen(reason: String)

Parameters

| reason |

The reason the engine opened.

|

`

                engineDidReceivePing()
                `

Called when the engine receives a ping message.

Declaration

Swift

open func engineDidReceivePing()

`

                engineDidSendPing()
                `

Called when the sends a ping to the server.

Declaration

Swift

open func engineDidSendPing()

`

                engineDidReceivePong()
                `

Called when the engine receives a pong message.

Declaration

Swift

open func engineDidReceivePong()

`

                engineDidSendPong()
                `

Called when the sends a pong to the server.

Declaration

Swift

open func engineDidSendPong()

`

                engineDidWebsocketUpgrade(headers:)
                `

Called when when upgrading the http connection to a websocket connection.

Declaration

Swift

open func engineDidWebsocketUpgrade(headers: [String : String])

Parameters

| headers |

The http headers.

|

`

                parseEngineMessage(_:)
                `

Called when the engine has a message that must be parsed.

Declaration

Swift

open func parseEngineMessage(_ msg: String)

Parameters

| msg |

The message that needs parsing.

|

`

                parseEngineBinaryData(_:)
                `

Called when the engine receives binary data.

Declaration

Swift

open func parseEngineBinaryData(_ data: Data)

Parameters

| data |

The data the engine received.

|

`

                reconnect()
                `

Tries to reconnect to the server.

This will cause a SocketClientEvent.reconnect event to be emitted, as well as SocketClientEvent.reconnectAttempt events.

Declaration

Swift

open func reconnect()

`

                removeSocket(_:)
                `

Removes the socket from the manager’s control. One of the disconnect methods should be called before calling this method.

After calling this method the socket should no longer be considered usable.

Declaration

Swift

@discardableResult
open func removeSocket(_ socket: SocketIOClient) -> SocketIOClient?

Parameters

| socket |

The socket to remove.

|

Return Value

The socket removed, if it was owned by the manager.

`

                setConfigs(_:)
                `

Sets manager specific configs.

parameter config: The configs that should be set.

Declaration

Swift

open func setConfigs(_ config: SocketIOClientConfiguration)

`

                socket(forNamespace:)
                `

Returns a SocketIOClient for the given namespace. This socket shares a transport with the manager.

Calling multiple times returns the same socket.

Sockets created from this method are retained by the manager. Call one of the disconnectSocket methods on this class to remove the socket from manager control. Or call SocketIOClient.disconnect() on the client.

Declaration

Swift

open func socket(forNamespace nsp: String) -> SocketIOClient

Parameters

| nsp |

The namespace for the socket.

|

Return Value

A SocketIOClient for the given namespace.