Socket.io

Socket.io

  • Example
import SocketIO

let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])

let socket = manager.defaultSocket

// let socket = manager.socket(forNamespace: "/swift")

socket.on(clientEvent: .connect) {data, ack in
    print("socket connected")
}

socket.on("currentAmount") {data, ack in
    guard let cur = data[0] as? Double else { return }
    
    socket.emitWithAck("canUpdate", cur).timingOut(after: 0) {data in
        if data.first as? String ?? "passed" == SocketAckValue.noAck {
            // Handle ack timeout 
        }

        socket.emit("update", ["amount": cur + 2.50])
    }

    ack.with("Got your currentAmount", "dude")
}

socket.connect()

SocketIOClient

open class SocketIOClient : NSObject, SocketIOClientSpec

Properties

nsp
anyHandler
handlers
manager
rawEmitView
status
sid

Initializers

public init(manager: SocketManagerSpec, nsp: String)

// Create a socket for the /swift namespace

let socket = manager.socket(forNamespace: "/swift")

// Add some handlers and connect
  • NOTE: The client is not thread/queue safe, all interaction with the socket should be done on the manager.handleQueue

Methods

connect(withPayload:)
connect(withPayload:timeoutAfter:withHandler:)
didConnect(toNamespace:payload:)
didDisconnect(reason:)
disconnect()
emit(_:_:completion:)
emitWithAck(_:_:)
emitAck(_:with:)
handleAck(_:data:)
handleClientEvent(_:data:)
handleEvent(_:data:isInternalMessage:withAck:)
handlePacket(_:)
leaveNamespace()
joinNamespace(withPayload:)
off(clientEvent:)
off(_:)
off(id:)
on(_:callback:)
on(clientEvent:callback:)
once(clientEvent:callback:)
once(_:callback:)
onAny(_:)
removeAllHandlers()
setReconnecting(reason:)

SocketManager

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

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

Properties

defaultSocket
socketURL
config
engine
forceNew
handleQueue
nsps
reconnects
reconnectWait
reconnectWaitMax
randomizationFactor
status
version
waitingPackets
Initializers

Initializers

init(socketURL:config:)
init(socketURL:config:)

Methods

connect()
connectSocket(_:withPayload:)
didDisconnect(reason:)
disconnect()
disconnectSocket(_:)
disconnectSocket(forNamespace:)
emitAll(clientEvent:data:)
emitAll(_:_:)
engineDidClose(reason:)
engineDidError(reason:)
engineDidOpen(reason:)
engineDidReceivePing()
engineDidSendPing()
engineDidReceivePong()
engineDidSendPong()
engineDidWebsocketUpgrade(headers:)
parseEngineMessage(_:)
parseEngineBinaryData(_:)
reconnect()
removeSocket(_:)
setConfigs(_:)
socket(forNamespace:)

goto >>> docs

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容