I'm trying to migrate from Socket.IO 0.9 to 1.2.1, and I'm having some difficulty with socket reconnection. In 0.9, I could do something like socket.socket.reconnect();, but unfortunately, socket.socket doesn't exist and socket.io.reconnect doesn't seem to reconnect my socket. However, socket.connect() seems to reconnect using my original connect settings, but this doesn't seem right... why can't I use reconnect? I've looked at this thread, but forceNew didn't seem to help. Just for completion's sake, my connect code looks like this:
var socket = io.connect('/myNamespace', {forceNew: true, path: '/path/to/socket.io'});
I ended up using socket.connect()... if anyone has a solution with .reconnect(), I'm all ears.
A possible workaround to this issue is to execute:
socket.io.skipReconnect = false;
socket.io.reconnect();
Related
I'm using react-native-netinfo to keep track of my device's internet connection. It was working fine for the most part until the point where the internet is disconnected and comes back on. At this point NetInfo does not respond with the correct internet state.
While going over the repo on github it looks like this is a known issue without a fix yet. In one of the threads, there was a mention of using the hook useNetInfo instead cos apparently that still worked fine.
So I tried that out as mentioned here https://github.com/react-native-netinfo/react-native-netinfo/issues/400#issuecomment-953177841 but even this isn't returning the correct state when the connection is established again.
I'm not sure what to do now. I don't know if there is more to this hook or if it needs to be implemented differently.
What I did to make it work was to add an event listener in componentDidMount() and remove the listener in componentWillUnMount().
componentDidMount(){
NetInfo.isConnected.fetch().then(isConnected => {
this.handleConnectivityChange(isConnected === undefined ? true : isConnected);
});
NetInfo.isConnected.addEventListener(
'connectionChange',
this.handleConnectivityChange
);}
My handleConnectivityChange() function stores the isConnected boolean in redux to be used wherever I want.
componentWillUnMount() {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this.handleConnectivityChange
);
}
Here is my network handler:
https://gist.github.com/anonymous/22fc110ad126ef3a2c5f
The problem is that when data is received it blocks my animiation (I have a wheel spinning when data has been requested) so it looks like the app have crashed and then after 1 second or 2, when the data has been received, it works like a charm.
This line seems to make sure that when waiting for an answer the app doesn't freeze:
local input,output = socket.select( { self.sock }, nil, 0 ) -- this is a way not to block runtime while reading socket. zero timeout does the trick
Here is another timeout setting:
self.sock:settimeout(0)
I have tried to change them to 0.01 and 0.001 but with no luck. I'm not really sure how to make it so the animation doesn't freeze.
Maybe I should change to one of Coronas built in async network handler (with callback functions) or is it possible to modify this network handler so that the animation doesn't freeze?
If you can use Corona's network.* api, it's async so you won't have any blocking calls.
Documentation link: http://docs.coronalabs.com/daily/api/library/network/index.html
I'm using the following code:
_broadcastReceiveSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:_broadcastHandler delegateQueue:_broadcastQueue];
BOOL successfulStart = [_broadcastReceiveSocket bindToPort:BROADCAST_PORT error:error]
&& [_broadcastReceiveSocket joinMulticastGroup:BROADCAST_HOST error:error]
&& [_broadcastReceiveSocket beginReceiving:error];
To start a socket listening for UDP broadcasts.
But I can only seem to run this code once in the simulator. After that I get an error back saying "Address already is use" from the bindToPort command.
The only way I have found so far to fix this is to reboot the machine. Which from a developers point of view is totally impractical.
I have had this working just fine. Any ideas what could have changed?
You have to close the socket (or destroy it completely) by sending it the close message.
Does anyone know if there is a (really) fast way to check for an inactive internet connection?
I have implemented the solution suggested here and it work but takes about 30 sec to determine if there was no internet connection.
I have also tried out:
- (BOOL) isConnectionAvailable {
SCNetworkReachabilityFlags flags;
BOOL receivedFlags;
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [#"www.google.com" UTF8String]);
receivedFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
CFRelease(reachability);
return (!receivedFlags || flags == 0) ? FALSE : TRUE;
}
which works too but it still takes about 20-30 seconds to determine if there is no active internet connection.
I feel that it got to be a fast way to determine if there is no internet connection.
Would really appreciate if someone can point me in a good direction.
I guess the "fastest" way, would be to simply utilize the connection with minimal data. Just set an appropriate timeout that works for you.
Apple even recommended at WWDC, that you should just simply try to establish a connection, because the reachability API might provide an inaccurate answer with certain conditions. The only way to measure properly is by sending a real request.
You can make a background thread in you application that will be checking if there is internet connection.
I use this customised reachability class in one of my apps: (code put elsewhere due to length)
Reachability.h - http://pastebin.com/qUVp4tFb
Reachability.m - http://pastebin.com/3C8LUjkS
Make sure to #import it, and call the following which returns a BOOL:
([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN);
It takes around a second to determine if the internet connection is active or not with this class.
Hope that helped :)
I'm working with the NetworkUtils.java class created by Sameer Nafdey in his blog post regarding accuiring a network connection within a Blackberry Application. However I recently noticed that my application was using the cell network even when a WiFi connection was available. I realized this was the case when we tested the application on a Torch with no SIM card and the app failed. After some debugging I found that:
if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED){...}
was returning false despite the fact that the WiFi network was setup correctly (I was able to use web browser to visit Google). We had to return the Torch but while debugging the app in the simulator I noticed that if WiFi was on but the Data Network was turned off then this call would work correctly. However I would then get an exception (java.io.ioexception: Radio is off) when executing this block:
httpConnector = (HttpConnection)Connector.open(URL);
httpConnector.setRequestMethod(HttpConnection.GET);
httpConnector.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
in = httpConnector.openInputStream();
I've seen a lot of issues related to the Torch's WiFi connectivity problems but I'm currently concerned that this behavior may also be affecting other models. Anyone seen anything like this or have an idea of how to fix it?
You could try:
if( RadioInfo.areWAFsSupported( RadioInfo.WAF_WLAN )
&&
( RadioInfo.getActiveWAFs() & RadioInfo.WAF_WLAN ) != 0
&&
CoverageInfo.isCoverageSufficient( 1 , RadioInfo.WAF_WLAN, false) )
{ ... }
It's been working so far, on Blackberry OS 6.0 (Torch 9800). Tested on device and sim.