Socket.io

作者: f8d1cf28626a | 来源:发表于2024-09-07 15:22 被阅读0次

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

相关文章

网友评论

      本文标题:Socket.io

      本文链接:https://www.haomeiwen.com/subject/khxsljtx.html