コード例 #1
0
        /// <summary>
        ///     Create a match making request
        /// </summary>
        /// <param name="method">The type of request we're making (join, create, etc)</param>
        /// <param name="roomName">The name of the room we're trying to match</param>
        /// <param name="options">Dictionary of options to use in the match making process</param>
        /// <param name="headers">Dictionary of headers to pass to the server</param>
        /// <typeparam name="T">Type of <see cref="ColyseusRoom{T}" /> we want to match with</typeparam>
        /// <returns><see cref="ColyseusRoom{T}" /> we have matched with via async task</returns>
        /// <exception cref="Exception">Thrown if there is a network related error</exception>
        /// <exception cref="CSAMatchMakeException">Thrown if there is an error in the match making process on the server side</exception>
        protected async Task <ColyseusRoom <T> > CreateMatchMakeRequest <T>(string method, string roomName,
                                                                            Dictionary <string, object> options, Dictionary <string, string> headers)
        {
            if (options == null)
            {
                options = new Dictionary <string, object>();
            }

            if (headers == null)
            {
                headers = new Dictionary <string, string>();
            }

            string json = await ColyseusRequest.Request("POST", $"matchmake/{method}/{roomName}", options, headers);

            LSLog.Log($"Server Response: {json}");
            ColyseusMatchMakeResponse response =
                JsonUtility.FromJson <ColyseusMatchMakeResponse>(json /*req.downloadHandler.text*/);

            if (response == null)
            {
                throw new Exception($"Error with request: {json}");
            }

            if (!string.IsNullOrEmpty(response.error))
            {
                throw new CSAMatchMakeException(response.code, response.error);
            }

            return(await ConsumeSeatReservation <T>(response, headers));
        }
コード例 #2
0
        /// <summary>
        /// Initializes the Colyseus manager singleton.
        /// </summary>
        private void InitializeInstance()
        {
            if (Instance != null)
            {
                Destroy(gameObject);
                return;
            }

            Instance = GetComponent <T>();

            // Initialize the requests object with settings
            _requests = new ColyseusRequest(_colyseusSettings);
        }
コード例 #3
0
        /// <summary>
        ///     Get all available rooms of type <typeparamref name="T" />
        /// </summary>
        /// <param name="roomName">Name of the room</param>
        /// <param name="headers">Dictionary of headers to pass to the server</param>
        /// <returns><see cref="CSACSARoomAvailableCollection{T}" /> array via async task</returns>
        public async Task <T[]> GetAvailableRooms <T>(string roomName = "", Dictionary <string, string> headers = null)
        {
            if (headers == null)
            {
                headers = new Dictionary <string, string>();
            }

            string json =
                await ColyseusRequest.Request("GET", $"matchmake/{roomName}", null,
                                              headers); //req.downloadHandler.text;

            if (json.StartsWith("[", StringComparison.CurrentCulture))
            {
                json = "{\"rooms\":" + json + "}";
            }

            CSARoomAvailableCollection <T> response = JsonUtility.FromJson <CSARoomAvailableCollection <T> >(json);

            return(response.rooms);
        }