/// <summary>Requests a file over the internet and posts form data to it.</summary>
		/// <param name="url">The url to request.</param>
		/// <param name="onDone">A method to call with the result.</param>
		/// <param name="form">A http form to send with the request.</param>
		public static void Request(string url,OnHttpEvent onDone,WWWForm form){
			HttpRequest request=new HttpRequest(url,onDone);
			
			if(form!=null){
				request.AttachForm(form);
			}
			
			request.Send();
		}
		/// <summary>The callback used to process a completed request.</summary>
		/// <param name="request">The HttpRequest that has now completed.</param>
		private static void OnAjaxDone(HttpRequest request){
			object[] extraData=(object[])request.ExtraData;
			DynamicMethod<Nitro.Void> callback=(DynamicMethod<Nitro.Void>)extraData[0];
			
			request.ExtraData=extraData[1];
			
			if(callback!=null){
				callback.Run(request);
			}
		}
		public HttpErrorInfo(HttpRequest request){
			
			// Store the request:
			Request=request;
			
			// Grab the URL:
			Url=new FilePath(request.Url,"");
			
			// Grab the message:
			Message=request.Error;
			
		}
		public override void OnGetGraphic(ImagePackage package,FilePath path){
			// Work like a proper browser - let's go get the image from the web.
			// Second is for video - it's called when the video is ready for playback but
			// Not necessarily fully downloaded.
			
			// First, was this graphic already requested?
			// If so, we'll provide that immediately.
			ImagePackage previousRequest=GetFromCache(path.Url);
			
			if(previousRequest!=null){
				// Great - provide this packages result first.
				// This prevents any flashing if innerHTML is loaded.
				package.GotCached(previousRequest);
				return;
			}
			
			HttpRequest request=new HttpRequest(path.Url,GotGraphicResult);
			request.OnRequestReady+=GotGraphicResult;
			request.ExtraData=package;
			request.Send();
		}
//--------------------------------------
		/// <summary>Performs a post request, sending the given post data.</summary>
		/// <param name="url">The URL to request.</param>
		/// <param name="postData">The data to send (as a UTF8 string).</param>
		/// <param name="callback">The callback to run when the request completes.</param>
		/// <param name="extraData">A custom object to pass to the callback when the request completes.</param>
		public static void Post(string url,string postData,DynamicMethod<Nitro.Void> callback,object extraData){
			HttpRequest request=new HttpRequest(url,OnAjaxDone);
			request.ExtraData=new object[]{callback,extraData};
			request.PostData=System.Text.Encoding.UTF8.GetBytes(postData);
			request.Send();
		}
		/// <summary>Performs a simple get request, calling the callback with the result.</summary>
		/// <param name="url">The URL to request.</param>
		/// <param name="callback">The callback to run when the request completes.</param>
		/// <param name="extraData">A custom object to pass to the callback when the request completes.</param>
		public static void Get(string url,DynamicMethod<Nitro.Void> callback,object extraData){
			HttpRequest request=new HttpRequest(url,OnAjaxDone);
			request.ExtraData=new object[]{callback,extraData};
			request.Send();
		}
//--------------------------------------
		private void OnFormSent(HttpRequest request){
			Element element=(Element)request.ExtraData;
			
			// Attempt to run ondone:
			object result=element.Run("ondone",request);
			
			if(result!=null && result.GetType()==typeof(bool) && (((bool)result)==false) ){
				// The ondone function returned false. Don't load into a target at all.
				return;
			}
			
			// Load the result into target now.
			Document document=ResolveTarget(element);
			
			if(document==null){
				// Posting a form to an external target.
				
				Log.Add("Warning: Unity cannot post to external targets. The page will be loaded a second time.");
				
				// Open the URL outside of Unity:
				Application.OpenURL(request.Url);
				
			}else{
				
				if(request.Errored){
					if(ErrorHandlers.PageNotFound!=null){
						ErrorHandlers.PageNotFound(new HttpErrorInfo(request),document);
					}else{
						document.innerHTML="Error: "+request.Error;
					}
				}else{
					document.innerHTML=request.Text;
				}
				
			}
			
		}
Beispiel #10
0
		/// <summary>Requests a file over the internet.</summary>
		/// <param name="url">The url to request.</param>
		/// <param name="onDone">A method to call with the result.</param>
		/// <param name="extraData">An object which will be available in the onDone method.</param>
		public static void Request(string url,OnHttpEvent onDone,object extraData){
			HttpRequest request=new HttpRequest(url,onDone);
			request.ExtraData=extraData;
			request.Send();
		}
Beispiel #11
0
		/// <summary>Clears all active requests.</summary>
		public static void Clear(){
			FirstRequest=LastRequest=null;
		}
Beispiel #12
0
		public static void Queue(HttpRequest request){
			
			if(MaxSimultaneousRequests!=-1 && CurrentActiveRequests==MaxSimultaneousRequests){
				// Add to the waiting list:
				if(FirstWaitingRequest==null){
					LastWaitingRequest=FirstWaitingRequest=request;
				}else{
					request.RequestBefore=LastWaitingRequest;
					LastWaitingRequest=LastWaitingRequest.RequestAfter=request;
				}
				// The waiting list will be updated when an active request completes.
			}else{
				// Bump up the active count:
				CurrentActiveRequests++;
				
				// Add to main queue:
				if(FirstRequest==null){
					LastRequest=FirstRequest=request;
				}else{
					request.RequestBefore=LastRequest;
					LastRequest=LastRequest.RequestAfter=request;
				}
				
			}
			
		}
		public override void OnPostForm(FormData form,Element formElement,FilePath path){
			// Post to HTTP; Action is our URL.
			HttpRequest request=new HttpRequest(path.Url,OnFormSent);
			request.ExtraData=formElement;
			request.AttachForm(form.ToUnityForm());
			request.Send();
		}
		public override void OnGetText(TextPackage package,FilePath path){
			// Work like a proper browser - Let's go grab text from the given url.
			// Note that this will only work with simple sites (no JS - nitro only) or ones built specifically for PowerUI.
			HttpRequest request=new HttpRequest(path.Url,GotTextResult);
			request.ExtraData=package;
			request.Send();
		}
		private void GotGraphicResult(HttpRequest request){
			ImagePackage package=(ImagePackage)request.ExtraData;
			
			if(request.Errored){
				package.GotGraphic(request.Error);
			}
			
			// Cache it:
			AddToCache(request.Url,package);
			
			string url=request.Url.ToLower();
			
			// Split by dot for the type:
			string[] pieces=url.Split('.');
			
			// Grab the type:
			string type=pieces[pieces.Length-1];
			
			
			if(type=="spa"){
				// Animation
				package.GotGraphic(new SPA(request.Url,request.Bytes));
			#if !MOBILE
			}else if(ContentType.IsVideo(type)){
				// Video
				package.GotGraphic(request.Video);
			#endif
			}else if(request.Image!=null){
				// Image
				package.GotGraphic(request.Image);
			}else{
				package.GotGraphic(request.Text);
			}
		}
		private void GotTextResult(HttpRequest request){
			TextPackage package=(TextPackage)request.ExtraData;
			
			package.GotText(request.Text,request.Error);
		}
		private void GotDataResult(HttpRequest request){
			DataPackage package=(DataPackage)request.ExtraData;
			
			package.GotData(request.Bytes,request.Error);
		}
		public override void OnGetData(DataPackage package,FilePath path){
			HttpRequest request=new HttpRequest(path.Url,GotDataResult);
			request.ExtraData=package;
			request.Send();
		}