[英]SRWebsocket connection is closing automatically after keeping app idle for sometime in iOS
I am using SRWebSocket to open a websocket connection in iOS. 我正在使用SRWebSocket在iOS中打开websocket连接。 But if I am keeping the application idle for sometimes, the connection is closing automatically.
但是,如果我有时保持应用程序空闲,则连接自动关闭。 After that when I am trying to send any data, the websocket connection is failing.
之后,当我尝试发送任何数据时,websocket连接失败。
Is there anyway to keep the websocket connection alive, until I manually disconnect? 无论如何都要保持websocket连接活着,直到我手动断开连接?
We need to ping the server intermittently (In my case, I do this in every 30 seconds), for avoiding to close the connection from the server side. 我们需要间歇性地ping服务器(在我的情况下,我每隔30秒执行一次),以避免从服务器端关闭连接。
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
{
NSLog(@"Websocket Connected");
// Sending autoping to server
[self startConnectionCheckTimer];
}
// Checking for WSconnection by Sending Scheduled Ping
- (void)startConnectionCheckTimer {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:30.0f
target:self
selector:@selector(sendPing:)
userInfo:nil
repeats:YES];
}
}
- (void)stopConnectionCheckTimer {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
}
- (void)sendPing:(id)sender
{
[_webSocket sendPing:nil];
}
Where, _webSocket is my SRWebSocket object, _timer is an object of NSTimer. 其中, _webSocket是我的SRWebSocket对象, _timer是NSTimer的对象。
Web Socket gets disconnected when the app is kept idle or when the app goes in background. 当应用程序保持空闲或应用程序进入后台时,Web Socket会断开连接。 You can try using this:
你可以试试这个:
[UIApplication sharedApplication].idleTimerDisabled = YES [UIApplication sharedApplication] .idleTimerDisabled = YES
Using this, will disable the provision of iPhone to be idle if your app is running. 使用此功能,如果您的应用正在运行,将禁用iPhone的闲置状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.