我正在使用socket.io
框架来编写Swift代码。我试图通过Websockets API进行连接,我有一个方法,在ViewController
的viewDidLoad
中调用。我已经全局声明了我的管理器和socketClient,下面是该方法的代码:
private func setupSockets() {
manager = SocketManager(socketURL: URL(string: "http://kaboom.rksv.net")!, config: [.log(true), .compress])
socket = SocketIOClient(manager: manager, nsp: "/watch")//manager.defaultSocket
socket.onAny {print("Got event: \($0.event), with items: \($0.items)")}
socket.on("data", callback: { (data, ack) in
print(data)
})
socket.on("error", callback: { (data, ack) in
print(data)
print(ack)
})
socket.on("connect", callback: { (data, ack) in
print(data)
print(ack)
self.socket.emit("ping", [])
})
socket.connect()
}
我遇到的问题是connect
回调函数没有被调用,因此我无法发送ping消息。以下是日志:
2018-02-18 19:02:20.589406+0530 stocks-cake[10965:3662189] LOG SocketIOClient{/watch}: Handling event: statusChange with data: [connecting]
2018-02-18 19:02:20.589692+0530 stocks-cake[10965:3662189] LOG SocketIOClient{/watch}: Joining namespace /watch
2018-02-18 19:02:20.589850+0530 stocks-cake[10965:3662189] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2018-02-18 19:02:20.589916+0530 stocks-cake[10965:3662189] LOG SocketManager: Adding engine
2018-02-18 19:02:32.986354+0530 stocks-cake[10965:3662271] LOG SocketEngine: Starting engine. Server: http://kaboom.rksv.net
2018-02-18 19:02:32.988480+0530 stocks-cake[10965:3662271] LOG SocketEngine: Handshaking
2018-02-18 19:02:32.995078+0530 stocks-cake[10965:3662271] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1
2018-02-18 19:02:33.197687+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Got polling response
2018-02-18 19:02:33.200976+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Got poll message: 97:0{"sid":"4Uf21Mr7_9DGjSXcAASj","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
2018-02-18 19:02:33.208867+0530 stocks-cake[10965:3662406] LOG SocketEngine: Got message: 0{"sid":"4Uf21Mr7_9DGjSXcAASj","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
2018-02-18 19:02:33.226824+0530 stocks-cake[10965:3662189] LOG SocketManager: Engine opened Connect
2018-02-18 19:02:33.226894+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.227367+0530 stocks-cake[10965:3662406] LOG SocketEngine: Writing poll: has data: false
2018-02-18 19:02:33.227447+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Sending poll: as type: 2
2018-02-18 19:02:33.230935+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Created POST string: 1:2
2018-02-18 19:02:33.231392+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: POSTing
2018-02-18 19:02:33.231510+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Doing polling POST http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.280857+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Got polling response
2018-02-18 19:02:33.281066+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Got poll message: 2:40
2018-02-18 19:02:33.281933+0530 stocks-cake[10965:3662406] LOG SocketEngine: Got message: 40
2018-02-18 19:02:33.282346+0530 stocks-cake[10965:3662189] LOG SocketParser: Parsing 0
2018-02-18 19:02:33.283167+0530 stocks-cake[10965:3662189] LOG SocketParser: Decoded packet as: SocketPacket {type: 0; data: []; id: -1; placeholders: 0; nsp: /}
2018-02-18 19:02:33.283195+0530 stocks-cake[10965:3662406] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.330123+0530 stocks-cake[10965:3662271] LOG SocketEngineWebSocket: Sending ws: probe as type: 2
2018-02-18 19:02:33.340044+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Got polling response
2018-02-18 19:02:33.340351+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Got poll message: 1:3
2018-02-18 19:02:33.340621+0530 stocks-cake[10965:3662407] LOG SocketEngine: Got message: 3
2018-02-18 19:02:33.341829+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Doing polling GET http://kaboom.rksv.net/socket.io/?transport=polling&b64=1&sid=4Uf21Mr7_9DGjSXcAASj
2018-02-18 19:02:33.378473+0530 stocks-cake[10965:3662407] LOG SocketEngine: Got message: 3probe
2018-02-18 19:02:33.379059+0530 stocks-cake[10965:3662407] LOG SocketEngine: Received probe response, should upgrade to WebSockets
2018-02-18 19:02:33.379252+0530 stocks-cake[10965:3662407] LOG SocketEngine: Upgrading transport to WebSockets
2018-02-18 19:02:33.379368+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Sending poll: as type: 6
2018-02-18 19:02:33.379511+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Created POST string: 1:6
2018-02-18 19:02:33.379968+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: POSTing
2018-02-18 19:02:33.477785+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Got polling response
2018-02-18 19:02:33.478016+0530 stocks-cake[10965:3662407] LOG SocketEnginePolling: Got poll message: 1:6
2018-02-18 19:02:33.478639+0530 stocks-cake[10965:3662407] LOG SocketEngine: Got message: 6
2018-02-18 19:02:33.479275+0530 stocks-cake[10965:3662407] LOG SocketEngine: Switching to WebSockets
2018-02-18 19:02:33.479594+0530 stocks-cake[10965:3662407] LOG SocketEngineWebSocket: Sending ws: as type: 5
2018-02-18 19:02:33.480317+0530 stocks-cake[10965:3662407] LOG SocketEngine: Flushing probe wait
2018-02-18 19:02:59.288327+0530 stocks-cake[10965:3662407] LOG SocketEngine: Writing ws: has data: false
2018-02-18 19:02:59.288576+0530 stocks-cake[10965:3662407] LOG SocketEngineWebSocket: Sending ws: as type: 2
2018-02-18 19:02:59.338827+0530 stocks-cake[10965:3662407] LOG SocketEngine: Got message: 3
2018-02-18 19:03:26.788673+0530 stocks-cake[10965:3662679] LOG SocketEngine: Writing ws: has data: false
我有什么遗漏的吗?做错了什么吗?提前感谢。