コード例 #1
0
ファイル: Client.cs プロジェクト: firebitsbr/colyseus-unity3d
        public async Task <Room <T> > ConsumeSeatReservation <T>(MatchMakeResponse response, Dictionary <string, string> headers = null)
        {
            var room = new Room <T>(response.room.name)
            {
                Id        = response.room.roomId,
                SessionId = response.sessionId
            };

            var queryString = new Dictionary <string, object>();

            queryString.Add("sessionId", room.SessionId);

            room.SetConnection(CreateConnection(response.room.processId + "/" + room.Id, queryString, headers));

            var tcs = new TaskCompletionSource <Room <T> >();

            void OnError(string message)
            {
                room.OnError -= OnError;
                tcs.SetException(new Exception(message));
            };

            void OnJoin()
            {
                room.OnError -= OnError;
                tcs.TrySetResult(room);
            }

            room.OnError += OnError;
            room.OnJoin  += OnJoin;

            _ = ColyseusManager.Instance.AddRoom(room);

            return(await tcs.Task);
        }
コード例 #2
0
        protected async Task <Room <T> > CreateMatchMakeRequest <T>(string method, string roomName, Dictionary <string, object> options)
        {
            if (options == null)
            {
                options = new Dictionary <string, object>();
            }

            if (Auth.HasToken)
            {
                options.Add("token", Auth.Token);
            }

            var uriBuilder = new UriBuilder(Endpoint.Uri);

            uriBuilder.Path  += "matchmake/" + method + "/" + roomName;
            uriBuilder.Scheme = uriBuilder.Scheme.Replace("ws", "http");             // FIXME: replacing "ws" with "http" is too hacky!

            var req = new UnityWebRequest();

            req.method = "POST";

            req.url = uriBuilder.Uri.ToString();

            // Send JSON options on request body
            var jsonBodyStream = new MemoryStream();

            Json.Serialize(options, jsonBodyStream);

            req.uploadHandler = new UploadHandlerRaw(jsonBodyStream.ToArray())
            {
                contentType = "application/json"
            };
            req.SetRequestHeader("Content-Type", "application/json");
            req.SetRequestHeader("Accept", "application/json");

            req.downloadHandler = new DownloadHandlerBuffer();
            await req.SendWebRequest();

            if (req.isNetworkError || req.isHttpError)
            {
                throw new Exception(req.error);
            }

            var response = JsonUtility.FromJson <MatchMakeResponse>(req.downloadHandler.text);

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

            var room = new Room <T>(roomName)
            {
                Id        = response.room.roomId,
                SessionId = response.sessionId
            };

            var queryString = new Dictionary <string, object>();

            queryString.Add("sessionId", room.SessionId);

            room.SetConnection(CreateConnection(response.room.processId + "/" + room.Id, queryString));

            var tcs = new TaskCompletionSource <Room <T> >();

            void OnError(string message)
            {
                room.OnError -= OnError;
                tcs.SetException(new Exception(message));
            };

            void OnJoin()
            {
                room.OnError -= OnError;
                tcs.TrySetResult(room);
            }

            room.OnError += OnError;
            room.OnJoin  += OnJoin;

            ColyseusManager.Instance.AddRoom(room);

            return(await tcs.Task);
        }