如何使用 SRWebsocket 检查网络套接字是否处于活动状态
how to check whether web socket is active or not using SRWebsocket
我正在使用 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总是return“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];
}
我正在使用 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总是return“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];
}