示例#1
0
        /// <summary>Initialize the client using NobleConnectSettings. The region used is determined by the Relay Server Address in the NobleConnectSettings.</summary>
        /// <remarks>
        /// The default address is connect.noblewhale.com, which will automatically select the closest 
        /// server based on geographic region.
        /// 
        /// If you would like to connect to a specific region you can use one of the following urls:
        /// <pre>
        ///     us-east.connect.noblewhale.com - Eastern United States
        ///     us-west.connect.noblewhale.com - Western United States
        ///     eu.connect.noblewhale.com - Europe
        ///     ap.connect.noblewhale.com - Asia-Pacific
        ///     sa.connect.noblewhale.com - South Africa
        ///     hk.connect.noblewhale.com - Hong Kong
        /// </pre>
        /// 
        /// Note that region selection will ensure each player connects to the closest relay server, but it does not 
        /// prevent players from connecting across regions. If you want to prevent joining across regions you will 
        /// need to implement that separately (by filtering out unwanted regions during matchmaking for example).
        /// </remarks>
        /// <param name="topo">The HostTopology to use for the NetworkClient. Must be the same on host and client.</param>
        /// <param name="onFatalError">A method to call if something goes horribly wrong.</param>
        /// <param name="allocationResendTimeout">Initial timeout before resending refresh messages. This is doubled for each failed resend.</param>
        /// <param name="maxAllocationResends">Max number of times to try and resend refresh messages before giving up and shutting down the relay connection. If refresh messages fail for 30 seconds the relay connection will be closed remotely regardless of these settings.</param>
        public NobleClient(GeographicRegion region = GeographicRegion.AUTO, Action<string> onFatalError = null, int relayLifetime = 60, int relayRefreshTime = 30, float allocationResendTimeout = .1f, int maxAllocationResends = 8)
        {
            var settings = (NobleConnectSettings)Resources.Load("NobleConnectSettings", typeof(NobleConnectSettings));

            this.onFatalError = onFatalError;
            nobleConfig = new IceConfig
            {
                iceServerAddress = RegionURL.FromRegion(region),
                icePort = settings.relayServerPort,
                maxAllocationRetransmissionCount = maxAllocationResends,
                allocationRetransmissionTimeout = (int)(allocationResendTimeout * 1000),
                allocationLifetime = relayLifetime,
                refreshTime = relayRefreshTime
            };

            if (!string.IsNullOrEmpty(settings.gameID))
            {
                string decodedGameID = Encoding.UTF8.GetString(Convert.FromBase64String(settings.gameID));
                string[] parts = decodedGameID.Split('\n');

                if (parts.Length == 3)
                {
                    nobleConfig.username = parts[1];
                    nobleConfig.password = parts[2];
                    nobleConfig.origin = parts[0];
                }
            }

            Init();
        }
示例#2
0
        /// <summary>Initialize the NetworkServer and Ice.Controller</summary>
        static void _Init(int listenPort, string relayServerAddress, Action <string, ushort> onPrepared = null, Action <string> onFatalError = null, bool forceRelayOnly = false)
        {
            var settings = (NobleConnectSettings)Resources.Load("NobleConnectSettings", typeof(NobleConnectSettings));
            var platform = Application.platform;

            NobleServer.onFatalError = onFatalError;

            nobleConfig = new IceConfig
            {
                iceServerAddress                 = relayServerAddress,
                icePort                          = settings.relayServerPort,
                useSimpleAddressGathering        = (platform == RuntimePlatform.IPhonePlayer || platform == RuntimePlatform.Android) && !Application.isEditor,
                onFatalError                     = OnFatalError,
                forceRelayOnly                   = forceRelayOnly,
                allocationLifetime               = relayLifetime,
                refreshTime                      = relayRefreshTime,
                maxAllocationRetransmissionCount = maxAllocationResends
            };

            if (!string.IsNullOrEmpty(settings.gameID))
            {
                if (settings.gameID.Length % 4 != 0)
                {
                    throw new System.ArgumentException("Game ID is wrong. Re-copy it from the Dashboard on the website.");
                }
                string   decodedGameID = Encoding.UTF8.GetString(Convert.FromBase64String(settings.gameID));
                string[] parts         = decodedGameID.Split('\n');

                if (parts.Length == 3)
                {
                    nobleConfig.origin   = parts[0];
                    nobleConfig.username = parts[1];
                    nobleConfig.password = parts[2];
                }
            }

            baseServer = new Peer(nobleConfig);

            NetworkServer.ReplaceHandler <ConnectMessage>(OnServerConnect, false);
            NetworkServer.ReplaceHandler <DisconnectMessage>(OnServerDisconnect, false);

            if (baseServer == null)
            {
                Logger.Log("NobleServer.Init() must be called before InitializeHosting() to specify region and error handler.", Logger.Level.Fatal);
            }
            baseServer.InitializeHosting(listenPort, onPrepared);
        }