Code Monkey home page Code Monkey logo

unityhttp's Introduction

UnityHttp

This is a small wrapper of UnityWebRequest to facilitate the work with the HTTP protocol. As well as the central control unit HttpCommunication.This module processes Cookies on both Android and iOS. The file iOSCoockieStorageHelper.mm, is plugin for iOS, thanks to the correct work on the cookies.

How to start

  1. Import into your Unity project UnityHTTP.dll and iOSCoockieStorageHelper.mm in Assets/Plugins folder OR import source codes
  2. Create your request script which will be extended by RequestBase. For example:
// http://localhost/version
public class GetAPIVersionRequest : RequestBase<GetAPIVersionRequest>
{
    public override string RequestUrl
    {
        get { return "version"; }
    }

    protected override void InitRequest()
    {
        _unityWebRequest = UnityWebRequest.Get(FullUrl);
    }
    
    public override void OnSendCompleted() { }

    protected override void SetRequestData(RequestDataBase requestData) { }
}
  1. If you need to send some data, create script which will be extended by RequestDataBase. For example ResetPasswordRequest.Data:
// http://localhost/forgotPass
 public class ResetPasswordRequest : RequestBase<ResetPasswordRequest>
    {
        private const string EMAIL_FIELD = "email";

        public override string RequestUrl
        {
            get { return "forgotPass"; }
        }

        private readonly WWWForm _requestData;

        public ResetPasswordRequest()
        {
            _requestData = new WWWForm();
        }
        
        protected override void SetRequestData(RequestDataBase requestData)
        {
            var data = requestData as Data;
            Debug.Assert(data != null, "data != null");
            _requestData.AddField(EMAIL_FIELD, data.email);
        }

        protected override void InitRequest()
        {
            _unityWebRequest = UnityWebRequest.Post(FullUrl, _requestData);
        }

        public override void OnSendCompleted() { }
        
        public class Data : RequestDataBase
        {
            public string email;
        }
    }
  1. In your project you need to initialize Unity HTTP module (only once)
public class Test : MonoBehaviour 
{
    // Use this for initialization
    void Start ()
    {
        HttpCommunication.Instance.Init("http://localhost/");
        //Your code
    }
}
  1. To send a request you should call Send method and pass parameters to it
public class Test : MonoBehaviour 
{
    // Use this for initialization
    void Start ()
    {
        HttpCommunication.Instance.Init("http://localhost/");
        HttpCommunication.Instance.Send<ResetPasswordRequest, SimpleResponse>(new ResetPasswordRequest.Data{email = "[email protected]"}, response =>
        {
            Debug.Log(response.ToString());
        });
        ///Your code
    }
}
  1. Script SimpleResponse contains the default fields of response, but if you want to get some data, you should create a script that will be extended by SimpleResponse
    [Serializable]
    public class GetAPIVersionResponse : SimpleResponse
    {
        public int version;
    }
...
HttpCommunication.Instance.Send<GetAPIVersionRequest, GetAPIVersionResponse>(null, response =>
        {
            if (!response.IsError)
            {
                Debug.Log(response.version);
            }
        });
...

unityhttp's People

Contributors

feynmen avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.