我正在使用SocketRocket,但我无法让它在后台传递消息。当我再次打开应用程序时,它会恢复连接(无需重新连接),所有消息都会立即进入。
这是我的连接代码:
- (void)_reconnect {
_websocket.delegate = nil;
[_websocket close];
NSString *host = @"ws://localhost:3030/primus";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:host]];
[request setValue:[NSString stringWithFormat:@"<%@>", apiKey] forHTTPHeaderField:@"Authentication"];
_websocket = [[SRWebSocket alloc] initWithURLRequest:request];
[_websocket setDelegateOperationQueue:[NSOperationQueue new]];
_websocket.delegate = self;
[_websocket open];
if (DEBUG) {
NSLog(@"Using: %@", host);
}
}我也尝试过不使用
[_websocket setDelegateOperationQueue:[NSOperationQueue new]];行,但这对任何事情都没有帮助。
你知道发生了什么事吗?
谢谢!
发布于 2014-05-06 21:50:38
我正面临着同样的问题,但仍然没有正确的答案。
如果主线程被冻结(当应用程序在后台时),套接字火箭似乎会停止向分配的SRDelegate发送消息。
我甚至尝试过另一种方法,但没有成功:
[_webSocket setDelegateDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];然而,我确实找到了一个使主线程解冻的变通方法。这不是很好,特别是因为它会导致电池耗尽,但它是有效的。
我注意到,如果您的应用程序在后台有位置更新,WebSocket将继续正常运行。
为了启动定位服务,您可以在applicationDidEnterBackground上执行以下操作:
CLLocationManager* locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];当返回时,您可以通过在applicationWillEnterForeground上调用以下代码来停止它们:
[locationManger stopUpdatingLocation];你需要保持对locationManager的强引用,而不仅仅是在作用域中。
到目前为止,这不是一个推荐的解决方案,但如果没有任何工作...
发布于 2015-11-19 18:33:02
嗨,刚才说的是在处理同样的问题。新版本的socket rocket可以帮助解决这个问题。现在支持一些后台模式。See source code。
我还没有测试,但我相信这应该会有帮助(让事情更接近最终解决方案)。
https://stackoverflow.com/questions/22366038
复制相似问题