iOS WebSocket not connecting to Server using SocketRocket - ios

I'm creating a chat application whose WebSocket server is written in java.
For WebSocket client i'm using SocketRocket library.
Everything works fine when i try to connect from localhost or 127.0.0.1.
But when i try to connect it using the public IP, it gives following error in - (void) webSocket:(SRWebSocket *)_webSocket didFailWithError:(NSError *)error method:
Error Domain=NSPOSIXErrorDomain Code=57 "The operation couldn’t be completed. Socket is not connected"
Here is how i initialize the SRWebSocket object:
webSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:#"ws://IP_ADDRESS:8080/ChatWebSocket/chat/12345"]];
// connects successfully when IP_ADDRESS = localhost or 127.0.0.1
// gives 'Socket is not connected' error when IP_ADDRESS = some public IP address
I've also tried initializing SRWebSocket with initWithURLRequest:.
P.S i've also tested and verified that the WebSocket server is accessible by its public IP address from a javascript code.
Therefor i think the issue is at the client end.
Any help will be appreciated.
Thanks in advance

After almost 2 days of research, i finally realized that the problem was arising on simulator only.
When i tested the same code on an iPod touch, it worked like a charm! :)

Related

Error when connecting Rosbridge (Web Sockets)

I am developing an app for iOS with Swift 3, this application search with the Bonjour service some robots in the local network which use a specific service, for example robot.local and show them in a list. The Bonjour service gives me the domain of the device. This is a example of the domains searched.
Ex.:
robot1.local
robot2.local
The next step, is when the user click an element of the list. This action start a connection by web sockets with the device and connect it for control it with the Iphone. I am using a library called RBManager which use RocketSocket library for connect. This library helps me to connect to RosBridge.
I use this code for connect:
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"wss://192.168.0.100:9090"]];
self.socket = [[SRWebSocket alloc] initWithURLRequest:request];
self.socket.delegate = self;
[self.socket open];
The problem is when I am install the app by Xcode I have not any problem but when I am install the app by an ipa file or by TestFlight the connection is rejected and shows this error:
managerDidFailWithError Optional(ErrorDomain=NSOSTatusErrorDomain
Code=-9807 "(null)" UserInfo={_kCFStreamErrorDomainKey=3,
_kCFStreamErrorCodeKey=-9807})
I found this issue in the library but is not the solution that I need.
I am deactive ATS in the info.plist but I not know how to solve this error. Could anyone help me?
I found the problem. The problem was that my RosBridge backend run with TLS and I didn't implement it.
The solution is implement the authentication in the client and all works :D

iOS IPV6 testing: Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected"

We are testing apps for IPV6 compatibility in both IPV6 (nat64 network) only and IPV4 only networks. We had been using the GCDAsyncSocket network library to make socket connections for our app to send and receive data over it.
1. When we test our iOS app (using the old GCDAsyncSocket code that we had downloaded in 2014) connecting to IPV6 only (nat-64) network as per Apple's guides here, the socket connection gets established successfully as soon as we log into the app, but within a few seconds (probably 20-30seconds , sometimes in a couple of mins), the -(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err gets called disconnecting the socket.
This happens only once every time we log out and log in to our app. As soon as we login, socket gets established, then suddenly gets disconnected throwing the below error:
2016-12-17 09:11:10.650437 atCommand Enterprise[288:13780] LOG: Socket disconnect
2016-12-17 09:11:11.979232 atCommand Enterprise[288:13780] LOG: -
[MLAPIControl socketDidDisconnect:withError:] -> 175
Printing description of err:
Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected"
UserInfo={_kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}
Printing description of ((__NSCFDictionary *)0x0000000170470880):
{
"_kCFStreamErrorCodeKey" = 57;
"_kCFStreamErrorDomainKey" = 1;
}
Printing description of sock:
<GCDAsyncSocket: 0x101737d70>
2016-12-17 09:11:55.853540 atCommand Enterprise[288:13780] LOG: Error
Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected" UserInfo=
{_kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}
2016-12-17 09:11:55.858775 atCommand Enterprise[288:13780] LOG: -
[MLAPIControl socketDidDisconnect:withError:] :
2. The exactly same issue of socket disconnection, after initial successful connection, happens even when i downloaded the latest (Latest commit 8ce941d 25 days ago) GCDAsyncSocket library code.
Any help is greatly appreciated.
Thank You.

NSURLErrorDomain Code=-1004 for few seconds after app start up

I'm getting "NSURLErrorDomain Code=-1004" error with Alamofire API calls,
but only for few seconds after app started (or took a rest for few minutes while the app's opened and make a call after that)
If I try to make the same call after few seconds, everything works fine.
I searched all the Stack Overflow questions and checked all the possible causes below:
No problem with Internet connection
"App Transport Security Settings" are correct and server's using https (I also tried "NSAllowsArbitraryLoads=true" but that didn't help)
APIs working fine
My gut feeling is that getting the network settings takes few seconds and when I make an API call before that's done, it just fails immediately. OR.. I'm using a Websocket in background that could be related?
FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x137d39380 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb341a9f540000000000000000}, _kCFStreamErrorCodeKey=-2200, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=[FILTERED], NSErrorFailingURLKey=[FILTERED], _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2200, NSLocalizedDescription=Could not connect to the server.}
Any suggestions?
UPDATED
Found that app makes 4 requests on launch, and 1 or 2 of them randomly fails, and I checked Nginx access and error log and there's no log for the failed calls at all.
We have the same issue here with Nginx 1.10.0 (and 1.9.15), iOS 9.3.1 using HTTP/2 with TLS 1.2.
The issue goes away with HTTP/1.1 and it's also working with HTTP/2 in Nginx version up to 1.9.14.
Nginx 1.11.0 Mainline is now available with the fix included mentioned earlier in this topic;
Change: HTTP/2 clients can now start sending request body
immediately; the "http2_body_preread_size" directive controls size of
the buffer used before nginx will start reading client request body.
I tested it and for me this release now works correctly again.
This seems like a confirmed bug in nginx 1.10. An issue about it can be found on the bug tracker of nginx at https://trac.nginx.org/nginx/ticket/979. The actual issue can be found at https://trac.nginx.org/nginx/ticket/959
You might want to consider switching to the 1.9 branch which has releases which do work. Hopefully nginx will release a 1.10.1 version soon which does not have this bug.
The issue actually only occurs on iOS; Android, Windows and OSX itself seem to have no problems negotiating a valid http2 connection.
I can also confirm that the nginx 1.9.15 not works correctly. Some calls always got "Could not connect to the server", and after revert to nginx 1.9.12 everything works fine.
These are the steps that I would try to follow:
1) test my app in the simulator and device
2) look if https is really needed instead of http
3) configure an alamofire manager and change timeout (for this step i
write some code):
var alamofireManager = Alamofire.Manager.sharedInstance
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPMaximumConnectionsPerHost = 10
configuration.timeoutIntervalForRequest = 30
configuration.timeoutIntervalForResource = 30
alamofireManager.delegate.taskWillPerformHTTPRedirection = nil
(so whit this last step the next alamofire calls can be for example: alamofireManager.request(etc....)
4) test with hard link like http://www.google.com, if the same thing
dont happened nothing around your swift code are incorrect, try to
set your web server parameters..
Problem solved!!!
versions:
1. Nginx version: 1.10.2
2. IOS version: 9.3.2
When the config like this:
listen 443 ssl;
Have the same problem like you.
But !!!
When the config like this:
listen 443 ssl http2;
Problem solved!!

issue with Afnetworking 1.0 error handling

I am using AfNetworking 1.0 into my app.I have created a singleton class of AFHTTP Client.
So all the API's are place in the same class.
I am getting a very wierd issue.
For eg: Let say, i have requested for some data from server.But the internet connection has lost.
So the failure block get's called.
m getting the following error:
Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x797dcc70 {NSErrorFailingURLStringKey=https://testing.com?searchText=s&pageSize=10&pageNumber=5, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=https://testing.com?searchText=s&pageSize=10&pageNumber=5, NSLocalizedDescription=The Internet connection appears to be offline., _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7aa367e0 "The Internet connection appears to be offline."}
But if i try to get the status code using following code:
NSLog(#"Status code %d",[operation.response statusCode]);
m getting Status Code as "0".
Can you please explain,why i am getting Status code as "0"?
I have to handle the error,depends on HTTP Status Code.So what is the best way to get this done?
(the same way which i am doing or is there any other way.)
In this case you can't use the status code because there isn't one. The status code comes from the server and in this case the client has not been able to contact the server.
You should use the error domain and code to handle this case.

NSURLConnection Lost Connection

I have an app with 100k+ users that connects using a SOAP web service. Everything works fine for most users except sometimes using NSURLConnection I keep getting the error:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0xac16c70 {NSErrorFailingURLStringKey=https://mail.scripps.org:443/ews/Exchange.asmx, NSErrorFailingURLKey=https://mail.scripps.org:443/ews/Exchange.asmx, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x133b0eb0 "The network connection was lost."}
This only happens for certain servers. The servers have NTLM authentication which works 95% of the time but for some reason I'm getting the error and I have no idea why.
Any help appreciated.
This Question also identified the problem I was having.
Instead of soap, I'm using jsonrpc.
The solution was simple. I needed:
[myMutableURLRequestObject setHTTPMethod:#"POST"];
Network connection failures happen, you just need to handle them gracefully. One solution may be to retry the connection 1 or more times until it succeeds after you are sure there is a network connection. Rough example below:
if([error domain] == NSURLErrorDomain &&
[error code] == NSURLErrorNetworkConnectionLost &&
retryCount < kMaxRetryCount)
{
[self retry];
++retryCount;
}

Resources