docs/Classes/SocketManager.html
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 (“/”).
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.
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.
Swift
public var config: SocketIOClientConfiguration { get set }
`
engine
`
The engine for this manager.
Swift
public var engine: SocketEngineSpec?
`
forceNew
`
If true then every time connect is called, a new engine will be created.
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.
Swift
public var handleQueue: DispatchQueue
`
nsps
`
The sockets in this manager indexed by namespace.
Swift
public var nsps: [String : SocketIOClient]
`
reconnects
`
If true, this client will try and reconnect on any disconnects.
Swift
public var reconnects: Bool
`
reconnectWait
`
The minimum number of seconds to wait before attempting to reconnect.
Swift
public var reconnectWait: Int
`
reconnectWaitMax
`
The maximum number of seconds to wait before attempting to reconnect.
Swift
public var reconnectWaitMax: Int
`
randomizationFactor
`
The randomization factor for calculating reconnect jitter.
Swift
public var randomizationFactor: Double
`
status
`
The status of this manager.
Swift
public private(set) var status: SocketIOStatus { get set }
`
version
`
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.
Swift
public var waitingPackets: [SocketPacket]
Initializers
`
init(socketURL:config:)
`
Type safe way to create a new SocketIOClient. opts can be omitted.
Swift
public init(socketURL: URL, config: SocketIOClientConfiguration = [])
| 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>)
Swift
@objc
public convenience init(socketURL: URL, config: [String : Any]?)
| 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.
Swift
open func connect()
`
connectSocket(_:withPayload:)
`
Connects a socket through this manager’s engine.
Swift
open func connectSocket(_ socket: SocketIOClient, withPayload payload: [String : Any]? = nil)
| 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.
Swift
open func didDisconnect(reason: String)
| reason |
The reason for the disconnection.
|
`
disconnect()
`
Disconnects the manager and all associated sockets.
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.
Swift
open func disconnectSocket(_ socket: SocketIOClient)
| 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.
Swift
open func disconnectSocket(forNamespace nsp: String)
| nsp |
The namespace to disconnect from.
|
`
emitAll(clientEvent:data:)
`
Sends a client event to all sockets in nsps
Swift
open func emitAll(clientEvent event: SocketClientEvent, data: [Any])
| clientEvent |
The event to emit.
|
`
emitAll(_:_:)
`
Sends an event to the server on all namespaces in this manager.
Swift
open func emitAll(_ event: String, _ items: SocketData...)
| event |
The event to send.
|
| items |
The data to send with this event.
|
`
engineDidClose(reason:)
`
Called when the engine closes.
Swift
open func engineDidClose(reason: String)
| reason |
The reason that the engine closed.
|
`
engineDidError(reason:)
`
Called when the engine errors.
Swift
open func engineDidError(reason: String)
| reason |
The reason the engine errored.
|
`
engineDidOpen(reason:)
`
Called when the engine opens.
Swift
open func engineDidOpen(reason: String)
| reason |
The reason the engine opened.
|
`
engineDidReceivePing()
`
Called when the engine receives a ping message.
Swift
open func engineDidReceivePing()
`
engineDidSendPing()
`
Called when the sends a ping to the server.
Swift
open func engineDidSendPing()
`
engineDidReceivePong()
`
Called when the engine receives a pong message.
Swift
open func engineDidReceivePong()
`
engineDidSendPong()
`
Called when the sends a pong to the server.
Swift
open func engineDidSendPong()
`
engineDidWebsocketUpgrade(headers:)
`
Called when when upgrading the http connection to a websocket connection.
Swift
open func engineDidWebsocketUpgrade(headers: [String : String])
| headers |
The http headers.
|
`
parseEngineMessage(_:)
`
Called when the engine has a message that must be parsed.
Swift
open func parseEngineMessage(_ msg: String)
| msg |
The message that needs parsing.
|
`
parseEngineBinaryData(_:)
`
Called when the engine receives binary data.
Swift
open func parseEngineBinaryData(_ data: Data)
| 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.
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.
Swift
@discardableResult
open func removeSocket(_ socket: SocketIOClient) -> SocketIOClient?
| socket |
The socket to remove.
|
The socket removed, if it was owned by the manager.
`
setConfigs(_:)
`
Sets manager specific configs.
parameter config: The configs that should be set.
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.
Swift
open func socket(forNamespace nsp: String) -> SocketIOClient
| nsp |
The namespace for the socket.
|
A SocketIOClient for the given namespace.