Пример #1
0
        public static async Task <UnityWebRequest> PostAsync(string url, string details, RequestAuthMiddleware authMiddleware = null)
        {
            string jsonData = JsonConvert.SerializeObject(new{ query = details });

            byte[]          postData = Encoding.ASCII.GetBytes(jsonData);
            UnityWebRequest request  = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST);

            request.uploadHandler = new UploadHandlerRaw(postData);
            request.SetRequestHeader("Content-Type", "application/json");
            if (authMiddleware != null)
            {
                authMiddleware.Prepare(request);
                Debug.LogFormat("Auth Header: {0}", request.GetRequestHeader("Authorization"));
            }
            OnRequestBegin requestBegin = new OnRequestBegin();

            requestBegin.FireEvent();

            try{
                await request.SendWebRequest();
            }
            catch (Exception e) {
                Debug.Log("Testing exceptions");
                OnRequestEnded requestFailed = new OnRequestEnded(e);
                requestFailed.FireEvent();
            }
            Debug.Log(request.downloadHandler.text);

            OnRequestEnded requestSucceeded = new OnRequestEnded(request.downloadHandler.text);

            requestSucceeded.FireEvent();
            return(request);
        }
Пример #2
0
        //Use this to subscribe to a graphql endpoint
        public static async Task <ClientWebSocket> WebsocketConnect(string subscriptionUrl, string details, RequestAuthMiddleware authMiddleware = null, string socketId = "1", string protocol = "graphql-ws")
        {
            string          subUrl = subscriptionUrl.Replace("http", "ws");
            string          id     = socketId;
            ClientWebSocket cws    = new ClientWebSocket();

            cws.Options.AddSubProtocol(protocol);
            if (authMiddleware != null)
            {
                authMiddleware.Prepare(cws);
            }
            Uri u = new Uri(subUrl);

            try{
                await cws.ConnectAsync(u, CancellationToken.None);

                if (cws.State == WebSocketState.Open)
                {
                    Debug.Log("connected");
                }
                await WebsocketInit(cws);
                await WebsocketSend(cws, id, details);
            }
            catch (Exception e) {
                Debug.Log("woe " + e.Message);
            }

            return(cws);
        }