gpt4 book ai didi

ios - SocketRocket 连接在后台暂停

转载 作者:可可西里 更新时间:2023-11-01 06:08:11 26 4
gpt4 key购买 nike

我正在使用 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]];

行,但这没有任何帮助。

你知道发生了什么事吗?

谢谢!

最佳答案

我遇到了同样的问题,但仍然没有正确的答案。

如果主线程被卡住(当应用程序处于后台时),Socket Rocket 似乎会停止向分配的 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 的强引用,而不仅仅是在范围内。

这不是目前推荐的解决方案,但如果没有任何效果......

关于ios - SocketRocket 连接在后台暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22366038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com