Reputation: 121
I'm using socket.io
framework for swift. I am trying to connect via websockets API, I have a method, which is getting called in the viewDidLoad
of the ViewController
. I have declared my manager and the socketClient globally, i'll put the code for the method below:
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()
}
The issue I face is that the connect
callback is not getting called and hence, I'm not able to emit a ping message. Here are the logs:
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
Is there anything i'm missing out? Something done wrong? Thanks in advance.
Upvotes: 4
Views: 6852
Reputation: 847
Use namespace like below:
let socket = socketManager.socket(forNamespace: "/watch")
Upvotes: 0
Reputation: 100503
Declaration should look like this , you forget to set the port
let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])
let socket = manager.defaultSocket
Upvotes: 1