コード例 #1
0
        /// <summary>Get the raw file data (based on the PowerUI FileProtocol system).</summary>
        public override void OnGetDataNow(ContentPackage package)
        {
            // The endpoint (this is e.g. /v1/hello):
            string endpointPath = package.location.pathname;

            if (endpointPath.StartsWith("/v1"))
            {
                if (!endpointPath.EndsWith("/"))
                {
                    endpointPath += "/";
                }

                // The request payload (can be null):
                Json.JSObject request = Json.JSON.Parse(package.requestText);

                package.responseHeaders["content-type"] = "application/json; charset=utf-8";

                // Get an endpoint:
                Endpoint ep = Endpoint.Get(endpointPath);

                if (ep == null)
                {
                    // Endpoint not found.
                    package.Failed(404);
                }
                else
                {
                    Response response = new Response();

                    // Pass the request to the endpoint:
                    ep.OnRequest(package, response, request);

                    // Output the response:
                    package.ReceivedText(response.ToString());
                }

                return;
            }

            // Serve a file from PowerTools.
            // The browser embedded in the Unity editor (not PowerUI)
            // is webkit which doesn't allow ajax to file:// uri's.
            if (!File.Exists(PowerToolsPath + endpointPath))
            {
                // Doesn't exist:
                package.Failed(404);
            }
            else
            {
                // Currently always HTML files down here:
                package.responseHeaders["content-type"] = "text/html";

                // Read the file:
                byte[] data = File.ReadAllBytes(PowerToolsPath + endpointPath);

                // Serve it up:
                package.ReceivedData(data, 0, data.Length);
            }
        }
コード例 #2
0
        /// <summary>Sets the PostData from the given JSON object. Changes the content type too.</summary>
        public void setRequestBody(Json.JSObject toPost)
        {
            // Make sure it's set to JSON:
            requestHeaders["content-type"] = "application/json";

            // Flatten it:
            string flat = Json.JSON.Stringify(toPost);

            // Set as post data:
            setRequestBody(flat);
        }
コード例 #3
0
        public override void LoadFromJson(Json.JSObject obj)
        {
            // It should be an array:
            Json.JSIndexedArray arr = obj as Json.JSIndexedArray;

            if (arr == null)
            {
                return;
            }

            // For each one..
            for (int i = 0; i < arr.length; i++)
            {
                // Add it as a cookie:
                Add(new Cookie(arr[i].ToString()));
            }
        }