gpt4 book ai didi

ios - 如何使用 SRWebsocket 检查网络套接字是否处于事件状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:30 26 4
gpt4 key购买 nike

我正在使用 SRWebsocket 进行套接字连接。现在,只要应用程序进入前台,我就会调用套接字打开功能,但我不想那样做。我需要先检查连接是否处于事件状态,然后才打开 websocket。有什么办法吗?

- (void)connectWebSocket {


if( self.readyState == SR_OPEN){

NSLog(@"CONNECT%u ",self.readyState);
} else {

NSLog(@"CLOSED %u",self.readyState);

}





[[IXDataBaseManager sharedNetworkDataManager] deleteOldPolls];


NSString *urlString =@"wss://m.hol.com/testing/api/ws/";



if( self.webSocket ) {
[self.webSocket close];
self.webSocket.delegate = nil;
self.webSocket=nil;
}
self.webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] protocols:@[@"hol-json"]];
self.webSocket.delegate = self;
[self.webSocket open];
NSLog(@"CONNECTOPN%u ",self.readyState);

}

self.readyState 总是返回“0”

最佳答案

这个示例代码可能对您有帮助:

- (void)openSocket {
if (_webSocket.readyState == SR_CLOSED)
{
// _websocket is closed. Open your web socket again
[_webSocket open];
} else {
// _websocket is not closed. check the readyState and perform your task accordingly
}
}

- (void)forceOpenSocket {
_webSocket.delegate = nil;
[_webSocket close];

_webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"Input Url"]]];
_webSocket.delegate = self;

// Opening Connection ...
[_webSocket open];
}

关于ios - 如何使用 SRWebsocket 检查网络套接字是否处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30206345/

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