Unity (IOS) Connection Lost

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

ersindemir
Posts: 30
Joined: 30 Apr 2016, 07:23
Location: Turkey

Unity (IOS) Connection Lost

Postby ersindemir » 30 Apr 2016, 07:35

1. Connect & Login Smartfox Server sucessfully
2. Click close button of Iphone and device gone Sleep
3. Wait 5 -7 seconds.
4. Reopen screen
5. Connection lost is handled.

I dont want to connection lost in 60sc after device sleep. How can I fix it ?

Android.Windows 10,Windows Phone devices has not this problem.(No connection lost)

Unity Version : 5.3.4f1
Smartfox Client Version : 1.6.3
Smartfox Server Version : 2.9.2
Tested IOS Devices : IPhone 4S (IOS 9.2.1) + IPhone 6 (IOS 9.3.1)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity (IOS) Connection Lost

Postby Lapo » 30 Apr 2016, 08:53

Hi,
every device and operating system handles sleep mode in a slightly different way.
You could keep alive the connection by sending an empty Extension request every few seconds, such as 4-5 seconds.

Something like this:

Code: Select all

[sfs send:[ExtensionRequest requestWithExtCmd:@"ping" params:[SFSObject newInstance]]];


Optionally the server could reply with a similar empty message, although I think the "ping" from client to server is enough to keep the connection alive.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
ersindemir
Posts: 30
Joined: 30 Apr 2016, 07:23
Location: Turkey

Re: Unity (IOS) Connection Lost

Postby ersindemir » 30 Apr 2016, 09:22

We are using Unity3d with C# .
smartfox.EnableLagMonitor(true, 3, 1); //Is it send ping ? If not Can you send C# code for Ping to Smartfox?

We handle this error device screen is opened. (Not all time )

[SFS - ERROR] [TCPSocketLayer] General error reading data from socket: Read failure at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at Sfs2X.Core.Sockets.TCPSocketLayer.Read () [0x00000] in <filename unknown>:0
-> applicationDidBecomeActive()

Connection was lost, Reason: unknown
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity (IOS) Connection Lost

Postby Lapo » 02 May 2016, 07:27

ersindemir wrote:We are using Unity3d with C# .

Sorry, my bad.

smartfox.EnableLagMonitor(true, 3, 1); //Is it send ping ? If not Can you send C# code for Ping to Smartfox?

Yes you can, but keep in mind that this request will not reset the "idle timer". This means that if the application is kept closed for a time longer than the "user idle timer" you have configured in your Zone, the client will be kicked out for inactivity.

To simulate user activity you will need to send an Extension request.

We handle this error device screen is opened. (Not all time )

[SFS - ERROR] [TCPSocketLayer] General error reading data from socket: Read failure at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at Sfs2X.Core.Sockets.TCPSocketLayer.Read () [0x00000] in <filename unknown>:0
-> applicationDidBecomeActive()

Connection was lost, Reason: unknown

This is the type of error that is caught by the client when a sudden disconnection occurs.
Lapo

--

gotoAndPlay()

...addicted to flash games
ersindemir
Posts: 30
Joined: 30 Apr 2016, 07:23
Location: Turkey

Re: Unity (IOS) Connection Lost

Postby ersindemir » 04 May 2016, 09:10

If I send extension request every 5 second , client never go idle. Is it correct ? ( If yes Any other solution ? )

Only I want to disconnect user when idle time is reached to I have configured in my Zone.Windows 10 , Windows Phone and Android devices does not need to send extension request,But IOS is need !

One more thing IOS devices are disconnect randomly when game is continue(sending extension request every 3-5 seconds). After I got this error

[SFS - ERROR] [TCPSocketLayer] General error reading data from socket: Read failure at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at Sfs2X.Core.Sockets.TCPSocketLayer.Read () [0x00000] in <filename unknown>:0

you can download and test my game here :
ITunnes : https://itunes.apple.com/us/app/spades-club/id1095008002
Google Play : https://play.google.com/store/apps/details?id=com.esertelekom.spade
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity (IOS) Connection Lost

Postby Lapo » 04 May 2016, 09:40

ersindemir wrote:If I send extension request every 5 second , client never go idle. Is it correct ? ( If yes Any other solution ? )

Yes, because you're continuously resetting the idle timer. It works as a "keep-alive" signal.
Probably you don't need to ping every 5 seconds, 30-50 seconds should be fine and you can save some traffic.

Only I want to disconnect user when idle time is reached to I have configured in my Zone.Windows 10 , Windows Phone and Android devices does not need to send extension request,But IOS is need !

Yes, unfortunately every device manages the resources of apps in the background in a different way.

One more thing IOS devices are disconnect randomly when game is continue(sending extension request every 3-5 seconds). After I got this error

[SFS - ERROR] [TCPSocketLayer] General error reading data from socket: Read failure at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at Sfs2X.Core.Sockets.TCPSocketLayer.Read () [0x00000] in <filename unknown>:0

Yes, this is a generic disconnection. Meaning that a network error has occurred.
Does it happen when testing locally?

thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
ersindemir
Posts: 30
Joined: 30 Apr 2016, 07:23
Location: Turkey

Re: Unity (IOS) Connection Lost

Postby ersindemir » 04 May 2016, 19:43

It occur on Real IOS Device ( Device info is writed on First Post).
Problem is that : Idle time is configured 60 second in Zone , but when Device go to sleep , smartfox disconnect in 5 second. Does any configuration need for IOS ? ( Dont forget that Android,Windows devices screen are slept after open screen smartfox is continue without disconnect.)
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity (IOS) Connection Lost

Postby Lapo » 05 May 2016, 08:21

The thing is... it is not SmartFox that disconnects the client. SmartFox never disconnects anyone unless there's a specific timeout setting telling it to do so. What happens is that (apparently) either Unity or iOS are not handling the connection properly.

I also have to say that I've tested a similar scenario in the simulator and was not able to reproduce the problem, at all. I just took the most simple "Lobby" example we provide, exported for iOS and run it in the iPhone 6 Simulator.

Even when the application is in the background and I am doing something else, there is no problem. The connection is always up and I can return to the application and interact with it without issues.

I've tested with a long 20 minutes timeout and I was never kicked out, until I was idle for 20 minutes as expected.

You didn't explain if you're testing locally or remotely and you didn't tell us if it happens all the times or only every now and then.
Let us know.
Lapo

--

gotoAndPlay()

...addicted to flash games
ersindemir
Posts: 30
Joined: 30 Apr 2016, 07:23
Location: Turkey

Re: Unity (IOS) Connection Lost

Postby ersindemir » 07 Jun 2016, 20:02

This problem is about Client Side. Its occur on Real IOS Devices.
please download and test my app on Real IOS Device.

This is scenario :
1- Open App
2- Login with Facebook or Guest (Does not matter)
3- Click to Close Screen Button( Your Device hardware button)
4- Wait 10 second
5- Open Screen
6- You will see Connection LOST

but it should not be. Idle time is 120 second.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity (IOS) Connection Lost

Postby Lapo » 08 Jun 2016, 08:37

It depends on how the connection is handled by the Runtime (unity) and the underlying OS (iOS).
If the connection gets closed by the system there's nothing we can do about it. It's beyond our control, and yours too.

SmartFoxServer and its API never disconnect anyone unless there's a timeout. All other disconnections occurring are happening for causes that are outside of our control. Example: if you turn off your router while playing a multiplayer game it's not the server's fault or the API's fault or the game's fault. The network has been interrupted from outside. Period.

This is the same scenario. Something outside the SFS API is managing the network connection of your App when it is put in background mode. The SFS API are simply notified by the OS that the connection has been interrupted and react to it by triggering a CONNECTION_LOST event.

Apple's documentation says it pretty clearly:
Be prepared to handle connection failures in your network-based sockets. The system may tear down socket connections while your app is suspended for any number of reasons. As long as your socket-based code is prepared for other types of network failures, such as a lost signal or network transition, this should not lead to any unusual problems. When your app resumes, if it encounters a failure upon using a socket, simply reestablish the connection.

Source: https://developer.apple.com/library/ios ... 72-CH4-SW4

What you may want to try is enabling the reconnection system and give it at least 60-90 seconds of reconnection time so that short background pauses that cause a disconnection may have a chance to be reconnected transparently.
See here: http://docs2x.smartfoxserver.com/Gettin ... n-hrc-plus

hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 17 guests