socket.io-client-swift
socket.io-client-swift copied to clipboard
Socket disconnects automatically, reconnects, and disconnects again and form a loop.
Hey i am using socket.io version 13.1.0 (latest) When app in launched, after some (random) time socket gets disconnected. It reconnects again and then again it gets disconnected and thus form a loop. My code is given as follows:
let manager = SocketManager(socketURL: URL(string: "https://staging.discretelogix.com:5104/")!, config: [.compress, .log(true), .reconnects(true) ])
let socket = manager.defaultSocket
socket.connect()
socket.on("connect") {data, ack in
print ("Socket connected")
}
Every time socket gets disconnected, one of the following reasons appear in logs:
- Could not connect to server
- Connection closed by server
- Session id unknown.
- Network connection was lost.
I have no idea what's going on behind the scenes. I have also tried config options: forceNew(true), forcePolling(true), reconnects(true) but the problem didn't get resolved.
@nuclearace your help would be appreciated. Thankyou.
EDIT: As i am initializing socket manager with some url. Do i need to initialize engine too in the following way: let engine = SocketEngine(client: manager, url: URL(string: "Some url")!, options: nil) and connect it as: engine.connect()
Or this is something which the library does by itself?
Same for me, my SocketIO init:
self.manager = SocketManager(socketURL: URL(string: str)!, config: [.log(true), .connectParams(["user_id": id, "room_code": roomId])])
self.socket = manager.defaultSocket
self.socket.on("connect") {data, ack in
let dict = [["sid":self.socket.sid]]
loggerHandler(dict, "connected")
print("socket connected")
}
Im facing this issue on 'Socket.IO-Client-Swift', '~> 13.1.0' . did anyone have a solution for this? @bl82 : were you able to find a solutions for this
@ConnectDevVS Yes, we made a code review in our team, traced whole flow with backend. Bug came from our side. During incorrect flow connect/reconnect/disconnect sequence.
@bl82 : Thanks for the rely Also, can you give me some more idea of what went wrong or what you guys did that gave rise to this issue. Im trying to figure what measure to resolve this and this info will be of great help. We are using a simple node server, and our android counter part were able to connect to it seamlessly.
When "forceNew" parameter is given in the configuration, the iOS app was able to reconnect back after disconnection, without this parameter, when connection is lost, it just continue to reconnect without success.
@ConnectDevVS Our flow when was reviewed in details, we found an edge case that initiated recurrent connections to socket manager. We are not using forceNew. I can only advice Android and backend team to start tracing you flow code step-by-step. Also pay attention that you should not call reconnect manually on disconnect handlers.
class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
let manager = SocketManager.init(socketURL: URL(string: APIConstant.basePath)!, config: [.log(true), .compress])
var socket: SocketIOClient!
override init() {
super.init()
// manager = SocketManager.init(socketURL: URL(string: APIConstant.basePath)!, config: ["log" : true, "compress" : true, "connectParams" : ["id": /UDSingleton.shared.user?._id]])
socket = manager.defaultSocket
socket.on("test") { dataArray, ack in
print(dataArray)
}
socket?.on(clientEvent: .connect) { (data, ack) in
debugPrint("socket connected")
}
socket?.on(clientEvent: .error) {data, ack in
debugPrint("socket error")
}
socket?.on(clientEvent: .reconnect) {data, ack in
debugPrint("socket reconnecting")
}
socket?.on(clientEvent: .disconnect) {data, ack in
print("socket disconnected")
self.connect()
}
}
func connect() {
if socket?.status == .connected {
return
}
socket?.connect()
}
func establishConnection() {
self.manager.config = SocketIOClientConfiguration(
arrayLiteral: .connectParams(["id": /UDSingleton.shared.user?._id]), .secure(true)
)
socket.connect()
}
func closeConnection() {
socket?.disconnect()
}
}
The same problem occur in my code
output
"socket connected" "socket error" "socket reconnecting" "socket error" "socket connected"
this is repeated continously
Is this issue resolved for iOS?
really need to know if this issue is fixed.
Facing the same issue here. Socket disconnects and reconnects periodically.
My setup is:
let manager = SocketManager(socketURL: URL(string:Config.baseURL)!,
config: [.log(false),
.forceNew(true),
.connectParams(["token":"myToken"]),
.forceWebsockets(true),
.compress,
.secure(true)])
**Giving me same error:**
self.manager = SocketManager(socketURL: URL(string: Base_Url)!, config: [.log(true), .compress, .connectParams(["EIO": "4"])]) self.socket = manager.defaultSocket
Output: Prints repeatedly manager Status change: [connected, 3] manager error Socket error: ["Error"] manager Status change: [connecting, 2] manager Status change: [connected, 3] manager error Socket error: ["Error"]