Je crée une application iOS dans laquelle j'utilise socket.io. Le scénario est très simple, une personne livre une aventure en envoyant des données en utilisant emit. Après cela, j'ai utilisé socket.on afin d'obtenir la réponse du serveur. Cela fonctionnait parfaitement depuis un certain temps mais maintenant je suis confronté à des problèmes. Ni les données atteignent le serveur, ni la réponse revient. J'ai cherché à propos de ce problème et la compréhension que j'ai eu est:Socket.io - Maintenir la connexion socket (Swift)
Il y a un certain point où le socket est déconnecté et par conséquent, il cesse de fonctionner. J'ai même utilisé socket.reconnect
avant d'émettre des données mais cela n'a pas fonctionné.
J'ai trouvé un paramètre de forceNew
pour node.js mais je n'ai trouvé aucune de ses alternatives dans swift.
Voici mon code (lié à Socket.IO):
En AppDelegate:
AppDelegate.socket = SocketIOClient(socketURL: URL(string: "######")!, config: [.log(true), .compress])
AppDelegate.socket.connect()
En ViewController:
override func viewDidLoad() {
super.viewDidLoad()
socket = AppDelegate.socket
self.socket.on("book-adventure-seeker") {[weak self] data, ack in
print (data)
}
}
@IBAction func bookAdventureTapped(_ sender: Any) {
let jwtToken = UserDefaults.standard.string(forKey: "jwtToken")
let data = ["adventure_id": adventureId, "jwt": jwtToken] as [String : Any]
self.socket.emit("book-adventure", data)
}
Et le journal est:
2017-10-20 09:45:04.665 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws: has data: false
2017-10-20 09:45:08.541 CuufyPrototype[2591:93618] LOG SocketIOClient: Emitting: 2["book-adventure",{"jwt":"######","adventure_id":"######"}]
2017-10-20 09:45:08.541 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: as type: 2
SocketIOClientStatus
2017-10-20 09:45:22.804 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws: 2["book-adventure",{"jwt":"######","adventure_id":"#####"}] has data: false
2017-10-20 09:45:22.805 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: 2["book-adventure",{"jwt":"#######","adventure_id":"######"}] as type: 4
2017-10-20 09:45:22.807 CuufyPrototype[2591:94370] LOG SocketEngine: Writing ws: has data: false
2017-10-20 09:45:22.807 CuufyPrototype[2591:94370] LOG SocketEngine: Sending ws: as type: 2
2017-10-20 09:45:22.810 CuufyPrototype[2591:93661] ERROR SocketEngine:
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] ERROR SocketIOClient:
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: error with data: [""]
2017-10-20 09:45:22.811 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Starting reconnect
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: reconnect with data: [""]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Trying to reconnect
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: reconnectAttempt with data: [-1]
2017-10-20 09:45:22.812 CuufyPrototype[2591:93618] LOG SocketIOClient: Handling event: statusChange with data: [SocketIO.SocketIOClientStatus]
Même j'ai essayé d'utiliser socket.on
dans AppDelegate comme ceci: (comme dans l'exemple de documents officiels)
AppDelegate.socket = SocketIOClient(socketURL: URL(string: "#######")!, config: [.log(true), .compress])
AppDelegate.socket.on("book-adventure-seeker") {[weak self] data, ack in
print (data)
}
AppDelegate.socket.connect()
Mais encore ne fonctionnait pas pour moi.
Mise à jour: Parfois, lors d'une commande d'émission, Engine is being closed
apparaît dans le journal, mais je n'ai aucune idée de comment résoudre ce problème.