Ios Socket.io不会按需关闭套接字(并且不允许我的swift应用程序重新连接到node.js服务器)
我正在使用Ios Socket.io不会按需关闭套接字(并且不允许我的swift应用程序重新连接到node.js服务器),ios,node.js,swift,sockets,socket.io,Ios,Node.js,Swift,Sockets,Socket.io,我正在使用socket.io库,我已经实现了服务器端(在node.js中)。我尝试遵循一个非常简单的场景: 1) 用户建立连接 2) 用户连接到房间 3) 用户断开连接 4) 用户重新连接 我正在客户端使用ios应用程序(用swift编写) 我的服务器代码如下: io.on('connection', function(clientSocket){ console.log('a user connected here'); clientSocket.on('disconnect
socket.io
库,我已经实现了服务器端(在node.js
中)。我尝试遵循一个非常简单的场景:
1) 用户建立连接
2) 用户连接到房间
3) 用户断开连接
4) 用户重新连接
我正在客户端使用ios应用程序(用swift
编写)
我的服务器代码如下:
io.on('connection', function(clientSocket){
console.log('a user connected here');
clientSocket.on('disconnect', function(){
console.log('user disconnected');
});
clientSocket.on('connectUser', function(username){
clientSocket.join(username);
console.log('user ' + username + ' connected to the room.');
});
现在,我在客户端创建了一个类,负责处理所有套接字内容:
class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: serverURL)!, options: [.ForceNew(true)])
override init() {
super.init()
}
func establishConnection() {
socket.connect()
}
func closeConnection() {
socket.disconnect()
}
func connectToServerWithNickname(nickname: String) {
print("CONNECTING TO SOCKET SERVER \(nickname)")
socket.emit("connectUser", nickname)
}
}
现在-进一步-在我的swift应用程序中(在applicationIDBecomeActive
中),我正在调用establishConnection()
,然后调用SocketIOManager.sharedInstance.ConnectToServer with昵称(用户名)
在我的服务器控制台中,我看到:
a user connected here
user 571fc6818451630f5becda9c connected to the room.
在myappDelegate
中,我实现了以下功能:
func applicationWillResignActive(application: UIApplication) {
SocketIOManager.sharedInstance.closeConnection()
print("closing sockets")
}
因此,现在当我将我的应用程序放在后台时,我看到正在关闭套接字
,在我的服务器控制台中,我看到用户已断开连接
现在主要的问题是,当用户返回应用程序时,我想重新连接他。因此我实施了这个方法:
func applicationDidBecomeActive(application: UIApplication) {
print("application is back to life")
SocketIOManager.sharedInstance.establishConnection()
SocketIOManager.sharedInstance.connectToServerWithNickname(username)
}
但是现在,当我从后台唤醒应用程序时,在我的服务器控制台中,我只看到一个用户连接到这里
。因此,这条信息:
user 571fc6818451630f5becda9c connected to the room.
缺少,我想这是因为我的应用程序没有重新连接到套接字
现在从我所读到的内容来看,socket.io
确实有问题,人们建议切换到其他库,但我还是想使用它。我在github上发现了这个问题,他们建议在客户端使用一个特定的参数。在swift的这个插件页面之后,我找到了特定的标志:
case ForceNew(Bool) // Will a create a new engine for each connect. Useful if you find a bug in the engine related to reconnects
这就是我在客户端构建套接字的原因:
var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: serverURL)!, options: [.ForceNew(true)])
但是是的,即使在Socket.IO客户端Swift
库的官方代码中,在断开连接方法上方也有一条注释:
/// Disconnects the socket. Only reconnect the same socket if you know what you're doing.
/// Will turn off automatic reconnects.
public func disconnect() {
DefaultSocketLogger.Logger.log("Closing socket", type: logType)
reconnects = false
didDisconnect("Disconnect")
}
有人知道我怎么处理吗?我想在每次我的应用程序进入后台时关闭套接字,并在我的应用程序进入前台时再次连接。谢谢 在应用程序中添加代码 SocketIOManager.sharedInstance.establishConnection()
SocketIOManager.sharedInstance.ConnectToServer with昵称(用户名)在应用程序中添加代码 SocketIOManager.sharedInstance.establishConnection() SocketIOManager.sharedInstance.ConnectToServerWith昵称(用户名)