In one of our iOS app using SignalR-ObjC, the connection is working fine across the app.
As in iOS app in background is not responsive and can't receive signalr messages. So we have following implementation:
SRHubConnection *connection = [[SRHubConnection alloc] initWithURLString:url useDefault:NO]
App goes to Background: [connection stop]; Stop the connection.
App comes to Foreground: [connection start]; Create a new connection object and start it. .
But if we switch between background and foreground 3 to 4 times continuously, makes the connection unstable and it do not connects again and throws following error.
[HTTP_BASED_TRANSPORT]
Cleandisconnectfailed.ErrorDomain=NSURLErrorDomainCode=-1001"The request timed out."UserInfo={
NSUnderlyingError=0x159283ae0{
ErrorDomain=kCFErrorDomainCFNetworkCode=-1001"The request timed out."UserInfo={
NSErrorFailingURLStringKey=http: //websiteurl.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=wxhidka/adfaf/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
NSErrorFailingURLKey=http: //website.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=gcukXaFIN/wm4ilNrj/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
_kCFStreamErrorCodeKey=-2102,
_kCFStreamErrorDomainKey=4,
NSLocalizedDescription=Therequesttimedout.
}
},
NSErrorFailingURLStringKey=http: //website.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=gcukXaFIN/wm4ilNrj/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
NSErrorFailingURLKey=http: //website.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=gcukXaFIN/wm4ilNrj/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
_kCFStreamErrorDomainKey=4,
_kCFStreamErrorCodeKey=-2102,
NSLocalizedDescription=Therequesttimedout.
}
And error:
[CONNECTION]
negotiationfailedErrorDomain=NSURLErrorDomainCode=-1001
"The request timed out."UserInfo={Same as displayed above}
Question:
Is there a way to stop SRHubConnection from going to such unstable state and start the new connection without error?
Make sure that you are not calling start or stop multiple times, on connection object.
Calling them multiple times can lead connection to be in unstable state.
Related
I'm using socket.io (version 4.4.0) with mobile Safari (on iOS 15.1) and receive images as ArrayBuffers periodically (~15-30 times per second) via websocket. I want to convert these buffers to something that can be rendered on a Canvas so the first step (I think) is converting to a Blob, like below:
const socket = io(process.env.SERVER ?? "", { transports: ["websocket"] });
socket.on("connect", () => {
socket.emit("subscribe", { topic: "rgb_image" });
});
socket.on("rgb_image", (msg, ack) => {
const blob = new Blob([msg.data], { type: "image/jpeg" });
});
The problem is that after a number of iterations (~20000 but sometimes less and sometimes more) the Safari websocket crashes (I imagine it is due to memory use slowly building up but I'm not sure):
[Error] WebSocket connection to 'ws://10.0.1.2:1234/' failed: WebSocket network error: Network process crashed.
[Error] undefined
(anonymous function) (index.28bba042.js:324) [<-- this is from Parcel's HMR, which uses web-sockets]
[Error] WebSocket connection to 'ws://10.0.1.17/socket.io/?EIO=4&transport=websocket' failed: WebSocket network error: Network process crashed.
[Log] engine error – Error: websocket error (index.28bba042.js, line 15419)
Error: websocket error [<-- I'm logging engine.io "error" event]
[Log] engine close – "transport error" (index.28bba042.js, line 15416) [<-- I'm logging engine.io "close" event]
[Error] Disconnected! – "transport error" [<-- I'm logging socket.io "disconnect" event]
Now, this did not happen on iOS 14 so I'm wondering if there's anything obvious I might use to work around this problem?
Several people get this error for different reasons. None of the answers I've found have solved my problem.
I use Timberjack to log my Alamofire requests.
All my GET requests work fine and I receive data in JSON.
My POSTs on the other hand works around 1 out 10 times every time if the POST includes a JSON body.
The server does not specify any Keep-Alive header.
Deployment target is iOS 9.0
This is my shared Manager with Timberjack:
class HTTPManager: Alamofire.Manager{
static let sharedManager: HTTPManager = {
let configuration = Timberjack.defaultSessionConfiguration()
let manager = HTTPManager(configuration: configuration)
return manager
}()
}
Defining the request:
let parameters: [String: AnyObject] = ["status":status]
request = HTTPManager.sharedManager.request(.POST, "\(baseURL)\(uri)", parameters: parameters, encoding: .JSON).validate()
Sending the request:
request!.responseJSON(queue: queue, options: .AllowFragments, completionHandler: { (response) in
//Handling the response
})
Most of the time the server receives an empty JSON body. But sometimes it does work and the body is received and the server responds with an OK. When it doesn't work I receive the error:
Error: The network connection was lost.
FAILURE: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
UserInfo={NSUnderlyingError=0x12fa47cb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost."
If someone can explain what's happening here I would be forever grateful :)
EDIT 1
We let the server respond every call with "Connection": "close" which did nothing to help the problem. The app always sends "Connection": "keep-alive" by default and it cannot be changed. Could this be the problem? That the app thinks the connection is open even though it's closed by the server? But even though you wait a few minutes it seems as though the POST call only works at random.
EDIT 2
Even when I wait 30 seconds between GET(s) and/or POST(s). GET always works. POST works at random (rarely). I get the -1005 error on most POSTs. Even though I get the network connection was lost error the server still receive my request but the JSON body is missing.
EDIT 3 - Ugly solution
In my response I check for the error code -1005, when I receive this error I just recreate the request and try again. This results in sending around 2-4 POST requests to the server where one POST works and the rest have empty JSON bodies.
Restart simulator or kill your app by throwing out from tasks.
Or check more solutions to this error code:
NSURLErrorDomain Code -1005 The network connection was lost
I would like to send periodic pings to server to keep the connection alive.
This is the function to send ping
socket.writePing(NSData())
But I am not getting the pong back
func websocketDidReceivePong(socket: WebSocket){
wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket received pong")
}
the above delegate will get fired when server sends back the pong.
FYI:
I am able to successfully establish web socket connect send message and close connection and I have implemented and I have given socket.delegate=self
WebSocketPongDelegate
Here is the web socket url that I am using for testing purposes
ws://echo.websocket.org/
I am wondering why I am not getting the pong back from web socket server.
Is your socket a property of your class to make sure it sticks around? If you allocate it just on a function stack it will fall out of scope and the delegates will never get called. Also the pongDelegate is separate from the regular delegate, so you need to set that to self as well:
socket.pongDelegate = self
I am developing an iPhone App (using iOS 9 beta). I am using Socket connections for which I am using SocketRocket client library. But when I try to establish a wss connection with some invalid host name, I don't get any error on opening socket, connection or even on sending data, so whenever I try to run program it seems like information about host is correct and data is being sent.
I am using current version of SocketRocket library, I have added SRWebSocket.h, SRWebSocket.m and SocketRocket-Prefix.pch files in my project. Following is the part of code I have:
NSString* url = [NSString stringWithFormat:#"wss://%#/myproject/stream?data=%d", webSocketHost, dummyData];
SRWebSocket *webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
webSocket.delegate = self;
// open websocket
[webSocket open];
// send message to websocket
[webSocket send:[self getJSONString:parameters]];
// close websocket
[webSocket close];
webSocket = nil;
If I pass some random host name like "abc.def" for the variable webSocketHost, it will still run smoothly (I have try-catch blocks surrounding above code, and I also tried to put break points in between and debugged it line by line).
And even when I don't have any internet connection to my phone, there aren't any errors thrown.
Does anyone know what could be the problem?
Thanks!
Are you implementing this delegate method? The library won't throw an error when you call [webSocket open], it will call this method if it can't connect to the endpoint sometime in the future since establishing a connection is an asynchronous operation.
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
Using TUSKit for iOS and tus.io for our API. After setting up tus.io and configuring the example project for TUSKit
<TUSResumableUpload.m:(115)> Finished upload to `PATH/videos/uploads/15/` for fingerprint assets-library://asset/asset.mp4?id=30C691FA-5866-4498-82CB-E95FB600BD74&ext=mp4
2015-04-07 01:46:36.397 TUSKit[207:8879] url: PATH/videos/uploads/15/
2015-04-07 01:46:36.410 TUSKit[207:8879] <TUSResumableUpload.m:(157)> ERROR: connection requested new body stream, which is currently not supported
the snippet
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request {
TUSLog(#"ERROR: connection requested new body stream, which is currently not supported");
return nil;
}
in TUSResumableUpload.m
is where we are getting the error, I'm just not sure why
the uploads on the server are started, but empty.
Any suggestions on how to fix this?
Xcode 6.1.1
Solved!
There was a bug in the API with the redirects which caused the TUSKit to fail to finish the upload.
And then we had to change the way binary data was received in order to start getting data in our files.