Beispiel #1
0
        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);
        }
 private static void ReadTextFrame(NetworkingPlayer player, BeardedManStudios.Forge.Networking.Frame.Text frame, NetWorker sender)
 {
     Console.WriteLine("Read: " + frame.ToString());
 }