/// <summary>Host a match</summary> /// <remarks> /// If relays are enabled this will cause a UNET match to be created as well. /// The Match Up match will not be created until after the UNET match is up because /// the match ID needs to be added to the match data for relay connections to work. /// If relays are not enabled the MAtch Up match is created immediately and no UNET /// match is created. /// </remarks> public void HostAMatch() { if (useRelays) { // If using the relays we must create a unet match and wait to get the match id before we can create the match up match networkManager.StartMatchMaker(); #if UNITY_5_3 networkManager.matchMaker.CreateMatch("", networkManager.matchSize, true, "", OnUNETMatchCreated); #else networkManager.matchMaker.CreateMatch( "", networkManager.matchSize, true, "", NetworkManagerExtension.externalIP, NetworkManagerExtension.GetLocalAddress(AddressFamily.InterNetwork), 0, 0, OnUNETMatchCreated); #endif } else { // Host and create the match networkManager.StartHost(); CreateMatch(); } }
/// <summary>Set up the networking stuff</summary> IEnumerator Start() { var asyncResult = Dns.BeginGetHostAddresses(matchmakerURL, null, null); networkManager = FindObjectOfType <NetworkManager>(); // We need to get the external IP if (networkManager) { StartCoroutine(NetworkManagerExtension.FetchExternalIP(externalIPSource)); } while (!asyncResult.IsCompleted) { yield return(0); } var addresses = Dns.EndGetHostAddresses(asyncResult); if (addresses == null || addresses.Length == 0) { Debug.LogError(TAG + "Failed to resolve matchmakerUrl: " + matchmakerURL); yield break; } var matchmakerIP = addresses[0]; matchmakingClient = new TcpClient(); asyncResult = matchmakingClient.BeginConnect(matchmakerIP, matchmakerPort, null, null); while (!asyncResult.IsCompleted) { yield return(0); } StartCoroutine(KeepAlive()); try { matchmakingClient.EndConnect(asyncResult); networkStream = matchmakingClient.GetStream(); streamReader = new StreamReader(networkStream); } catch (SocketException e) { Debug.LogError(TAG + "Failed to connect to the matchmaking server. Is it running? " + e); } catch (Exception e) { Debug.LogError(TAG + "Failed to connect to the matchmaking server. " + e); } }
/// <summary>Send the command to the matchmaking server to create a new match.</summary> /// <param name="maxClients">The maximum number of clients to allow. Once a match is full it is no longer returned in match listings (until a client leaves).</param> /// <param name="matchData">Optional match data to include with the match. This is a good place to store your connection data.</param> /// <param name="matchName">The name of the match.</param> /// <param name="onCreateMatch">Optional callback method to call when a response is received from the matchmaking server.</param> public void CreateMatch(int maxClients, Dictionary <string, MatchData> matchData = null, Action <bool, Match> onCreateMatch = null) { if (matchData == null) { matchData = new Dictionary <string, MatchData>(); } if (networkManager) { if (!matchData.ContainsKey("internalIP")) { matchData["internalIP"] = NetworkManagerExtension.GetLocalAddress(AddressFamily.InterNetwork); } if (!matchData.ContainsKey("externalIP")) { matchData["externalIP"] = NetworkManagerExtension.externalIP; } if (!matchData.ContainsKey("port")) { matchData["port"] = networkManager.networkPort; } if (networkManager.matchMaker != null && networkManager.matchInfo != null) { matchData["unetMatchID"] = (ulong)networkManager.matchInfo.networkId; } } if (matchmakerURL == "grabblesgame.com" || matchmakerURL == "noblewhale.com") { // If you're using my matchmaking server then we need to include some sort of ID to keep your game's matches separate from everyone else's if (!matchData.ContainsKey("applicationID")) { matchData["applicationID"] = Application.productName; } } currentMatch = new Match(-1, matchData); SendCommand( Command.CREATE_MATCH, currentMatch.Serialize(maxClients), (success, transaction) => OnCreateMatchInternal(success, transaction.response, onCreateMatch) ); }