private void RefreshMasterServerListings() { masterClient = new TCPMasterClient(); masterClient.serverAccepted += (sender) => { try { // Create the get request with the desired filters JSONNode sendData = JSONNode.Parse("{}"); JSONClass getData = new JSONClass(); getData.Add("id", Settings.serverId); getData.Add("type", Settings.type); getData.Add("mode", Settings.mode); sendData.Add("get", getData); // Send the request to the server masterClient.Send(TextFrame.CreateFromString(masterClient.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true)); } catch { // If anything fails, then this client needs to be disconnected masterClient.Disconnect(true); masterClient = null; } }; masterClient.textMessageReceived += (player, frame, sender) => { try { JSONNode data = JSONNode.Parse(frame.ToString()); if (data["hosts"] != null) { var response = new MasterServerResponse(data["hosts"].AsArray); if (response != null && response.serverResponse.Count > 0) { // Go through all of the available hosts and add them to the server browser foreach (var server in response.serverResponse) { // Run on main thread as we are creating UnityEngine.GameObjects MainThreadManager.Run(() => AddServer(server.Address, server.Port)); } } } } finally { if (masterClient != null) { // If we succeed or fail the client needs to disconnect from the Master Server masterClient.Disconnect(true); masterClient = null; } } }; masterClient.Connect(Settings.masterServerHost, Settings.masterServerPort); }
public void Refresh() { // Clear out all the currently listed servers for (int i = content.childCount - 1; i >= 0; --i) { Destroy(content.GetChild(i).gameObject); } // The Master Server communicates over TCP client = new TCPMasterClient(); // Once this client has been accepted by the master server it should sent it's get request client.serverAccepted += (sender) => { try { // Create the get request with the desired filters JSONNode sendData = JSONNode.Parse("{}"); JSONClass getData = new JSONClass(); getData.Add("id", gameId); getData.Add("type", gameType); getData.Add("mode", gameMode); sendData.Add("get", getData); // Send the request to the server client.Send(Frame.Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true)); } catch { // If anything fails, then this client needs to be disconnected client.Disconnect(true); client = null; } }; // An event that is raised when the server responds with hosts client.textMessageReceived += (player, frame, sender) => { try { // Get the list of hosts to iterate through from the frame payload JSONNode data = JSONNode.Parse(frame.ToString()); if (data["hosts"] != null) { MasterServerResponse response = new MasterServerResponse(data["hosts"].AsArray); if (response != null && response.serverResponse.Count > 0) { // Go through all of the available hosts and add them to the server browser foreach (MasterServerResponse.Server server in response.serverResponse) { string protocol = server.Protocol; string address = server.Address; ushort port = server.Port; string name = server.Name; // name, address, port, comment, type, mode, players, maxPlayers, protocol CreateServerOption(name, () => { // Determine which protocol should be used when this client connects NetWorker socket = null; if (protocol == "udp") { socket = new UDPClient(); ((UDPClient)socket).Connect(address, port, natServerHost, natServerPort); } else if (protocol == "tcp") { socket = new TCPClient(); ((TCPClient)socket).Connect(address, port); } #if !UNITY_IOS && !UNITY_ANDROID else if (protocol == "web") { socket = new TCPClientWebsockets(); ((TCPClientWebsockets)socket).Connect(address, port); } #endif if (socket == null) { throw new Exception("No socket of type " + protocol + " could be established"); } Connected(socket); }); } } } } finally { if (client != null) { // If we succeed or fail the client needs to disconnect from the Master Server client.Disconnect(true); client = null; } } }; client.Connect(masterServerHost, (ushort)masterServerPort); }
public virtual void MatchmakingServersFromMasterServer(string masterServerHost, ushort masterServerPort, int elo, System.Action <MasterServerResponse> callback = null, string gameId = "myGame", string gameType = "any", string gameMode = "all") { // The Master Server communicates over TCP TCPMasterClient client = new TCPMasterClient(); // Once this client has been accepted by the master server it should send it's get request client.serverAccepted += (sender) => { try { // Create the get request with the desired filters JSONNode sendData = JSONNode.Parse("{}"); JSONClass getData = new JSONClass(); getData.Add("id", gameId); getData.Add("type", gameType); getData.Add("mode", gameMode); getData.Add("elo", new JSONData(elo)); sendData.Add("get", getData); // Send the request to the server client.Send(Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true)); } catch { // If anything fails, then this client needs to be disconnected client.Disconnect(true); client = null; MainThreadManager.Run(() => { if (callback != null) { callback(null); } }); } }; // An event that is raised when the server responds with hosts client.textMessageReceived += (player, frame, sender) => { try { // Get the list of hosts to iterate through from the frame payload JSONNode data = JSONNode.Parse(frame.ToString()); MainThreadManager.Run(() => { if (data["hosts"] != null) { MasterServerResponse response = new MasterServerResponse(data["hosts"].AsArray); if (callback != null) { callback(response); } } else { if (callback != null) { callback(null); } } }); } finally { if (client != null) { // If we succeed or fail the client needs to disconnect from the Master Server client.Disconnect(true); client = null; } } }; try { client.Connect(masterServerHost, masterServerPort); } catch (System.Exception ex) { Debug.LogError(ex.Message); MainThreadManager.Run(() => { if (callback != null) { callback(null); } }); } }
public void Refresh() { ClearServers(); if (lan) { NetWorker.RefreshLocalUdpListings(ushort.Parse(portNumber.text)); return; } // The Master Server communicates over TCP TCPMasterClient client = new TCPMasterClient(); // Once this client has been accepted by the master server it should sent it's get request client.serverAccepted += x => { try { // The overall game id to select from string gameId = "myGame"; // The game type to choose from, if "any" then all types will be returned string gameType = "any"; // The game mode to choose from, if "all" then all game modes will be returned string gameMode = "all"; // Create the get request with the desired filters JSONNode sendData = JSONNode.Parse("{}"); JSONClass getData = new JSONClass(); // The id of the game to get getData.Add("id", gameId); getData.Add("type", gameType); getData.Add("mode", gameMode); sendData.Add("get", getData); // Send the request to the server client.Send(BeardedManStudios.Forge.Networking.Frame.Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true)); } catch { // If anything fails, then this client needs to be disconnected client.Disconnect(true); client = null; } }; // An event that is raised when the server responds with hosts client.textMessageReceived += (player, frame, sender) => { try { // Get the list of hosts to iterate through from the frame payload JSONNode data = JSONNode.Parse(frame.ToString()); if (data["hosts"] != null) { // Create a C# object for the response from the master server MasterServerResponse response = new MasterServerResponse(data["hosts"].AsArray); if (response != null && response.serverResponse.Count > 0) { // Go through all of the available hosts and add them to the server browser foreach (MasterServerResponse.Server server in response.serverResponse) { Debug.Log("Found server " + server.Name); // Update UI MainThreadManager.Run(() => { AddServer(server.Name, server.Address, server.PlayerCount, server.Mode); }); } } } } finally { if (client != null) { // If we succeed or fail the client needs to disconnect from the Master Server client.Disconnect(true); client = null; } } }; client.Connect(masterServerHost, (ushort)masterServerPort); }