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

  • The socket associated with the default namespace (/).

    Declaration

    Swift

    public var defaultSocket: SocketIOClient
  • 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
  • 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
  • The engine for this manager.

    Declaration

    Swift

    public var engine: SocketEngineSpec?
  • If true then every time connect is called, a new engine will be created.

    Declaration

    Swift

    public var forceNew = false
  • 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.main
  • The sockets in this manager indexed by namespace.

    Declaration

    Swift

    public var nsps = [String: SocketIOClient]()
  • If true, this client will try and reconnect on any disconnects.

    Declaration

    Swift

    public var reconnects = true
  • The number of seconds to wait before attempting to reconnect.

    Declaration

    Swift

    public var reconnectWait = 10
  • The status of this manager.

    Declaration

    Swift

    public private(set) var status: SocketIOStatus = .notConnected
  • 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]()
  • 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.

  • 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

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

    Parameters

    socketURL

    The url of the socket.io server.

    config

    The config for this socket.

  • Connects the underlying transport and the default namespace socket.

    Override if you wish to attach a custom SocketEngineSpec.

    Declaration

    Swift

    open func connect()
  • Connects a socket through this manager’s engine.

    Declaration

    Swift

    open func connectSocket(_ socket: SocketIOClient)

    Parameters

    socket

    The socket who we should connect through this manager.

  • Called when the manager has disconnected from socket.io.

    Declaration

    Swift

    open func didDisconnect(reason: String)

    Parameters

    reason

    The reason for the disconnection.

  • Disconnects the manager and all associated sockets.

    Declaration

    Swift

    open func disconnect()
  • 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.

  • 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.

  • 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.

  • 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.

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

    Same as emitAll(_:_:), but meant for Objective-C.

    Declaration

    Swift

    open func emitAll(_ event: String, withItems items: [Any])

    Parameters

    event

    The event to send.

    items

    The data to send with this event.

  • Called when the engine closes.

    Declaration

    Swift

    open func engineDidClose(reason: String)

    Parameters

    reason

    The reason that the engine closed.

  • Called when the engine errors.

    Declaration

    Swift

    open func engineDidError(reason: String)

    Parameters

    reason

    The reason the engine errored.

  • Called when the engine opens.

    Declaration

    Swift

    open func engineDidOpen(reason: String)

    Parameters

    reason

    The reason the engine opened.

  • Called when the engine receives a pong message.

    Declaration

    Swift

    open func engineDidReceivePong()
  • Called when the sends a ping to the server.

    Declaration

    Swift

    open func engineDidSendPing()
  • Called when when upgrading the http connection to a websocket connection.

    Declaration

    Swift

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

    Parameters

    headers

    The http headers.

  • 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.

  • Called when the engine receives binary data.

    Declaration

    Swift

    open func parseEngineBinaryData(_ data: Data)

    Parameters

    data

    The data the engine received.

  • 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()
  • 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

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

    Parameters

    socket

    The socket to remove.

    Return Value

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

  • Sets manager specific configs.

    parameter config: The configs that should be set.

    Declaration

    Swift

    open func setConfigs(_ config: SocketIOClientConfiguration)
  • 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.