Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

com.unity.transport client disconnects when no data is sent during some period

Discussion in 'Multiplayer' started by Zolden, Nov 5, 2018.

  1. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    I'm using that new networking api: https://github.com/Unity-Technologies/multiplayer

    What I created: client connects to server, then player input is being shared between them.

    I only send data when there's input. And do not send anything if there's no input.

    Everything works ok if there's some input happens periodically. But when there's no input (an therefore no data sends) during about 20 sec, the connection disconnects.

    I didn't find any timeout related times neither at NetworkConnection or UdpCNetworkDriver classes.

    So, why does the connection disconnects? Can I somehow ask it not to? Or I have to send dummy data periodically to avoid disconnection?
     
  2. Skjalg

    Skjalg

    Joined:
    May 25, 2009
    Posts:
    211
    I had the same question yesterday, I managed to find the answer while just browsing the code. I love the fact that it is open sourced. https://github.com/Unity-Technologies/multiplayer

    Just as you've guessed, for each connection that the UDP Network Driver has, it stores the last package received and the time it receives it. Then it checks that time every update to see if it should disconnect the connection.

    The answer you are looking for is in https://github.com/Unity-Technologi.../com.unity.transport/Runtime/NetworkParams.cs

    What you need to do is to instead of specifying no NetworkParameters when creating the NetworkDriver like this:
    m_Driver = new UdpCNetworkDriver(new INetworkParameter[0]);
    you instead do something like this:
    m_Driver = new UdpCNetworkDriver(new NetworkConfigParameter{disconnectTimeoutMS = 30000});

    You need to do this with both the server and the client, because either one will disconnect if the timer runs out.

    PS: 30*1000 = 30000 is the default parameter, so you need to increase this number if you want it to delay further before disconnecting.
     
    Zolden likes this.
  3. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Generally you want to send packets on a fixed interval, say 30 times/sec in a game, even if there's nothing to send (which should be pretty rare) just submit an empty packet to the remote.
     
    Zolden likes this.
  4. Skjalg

    Skjalg

    Joined:
    May 25, 2009
    Posts:
    211
    This is what I ended up doing as well. I just send a short message saying ping every 1s and the server replies pong. Thus keeping the connection alive that way. I didn't increase the timeout either.
     
  5. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    Thank you guys!

    Yea, sending stuff periodically seems reasonable, since the absence of incoming data can be interpreted as lost connection and measures can be undertaken.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not just for this API, but since UDP is a connectionless protocol network infrastructure like NAT routers will time out port mappings in their translation tables after a fairly short period of time (often 30 seconds or less). My approach is to monitor when the last message was sent, and if 3 seconds have passed to send a keep alive message.
     
    Zolden likes this.