简体   繁体   中英

How to call a method on server side from c#

I got this QA on stack overflow -- How to call code behind server method from a client side javascript function?

however, it is call the function from javascript, how can I do it from c# . I use unity3d to develop an ios app, I did not do it before. Could some one know how to do it?

Thanks in advance.

PS about the backend

the backend give these like the below. url: https://{server address}/register paramters:

  • username, string
  • name, string
  • pwd, string

and return: - user_id, int

I run the server method with the right parameters, if the parameters are right, it give back a int/user_id return.

WWW unity3d Class, this is what probably you are looking for. WWW is

a small utility module for retrieving the contents of URLs.

For details to interact with Server (PHP) using C# i'll suggest you this unitywiki link . You will require to do something like this as this tutorial suggested.

 IEnumerator GetData()
    {
        gameObject.guiText.text = "Loading Scores";
        WWW hs_get = new WWW(highscoreURL);//highscoreURL this is ur url as u said
        yield return hs_get;

        if (hs_get.error != null)//checking empty or error response etc
        {
            print("There was an error getting the high score: " + hs_get.error);
        }
        else
        {
            gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
        }
    }

The reset thing you can customize according to your specification main things to use is WWW with Co-routine .

I found a post about this from this blog -- devindia

In face, add a WWWForm as a secondary parameter to WWW , then it is a post to server.

using UnityEngine;
using System.Collections;

public class PostJsonDataScript : MonoBehaviour
{
    // Use this for initialization
    string Url;
    void Start()
    {
        Url = "Url to the service";
        PostData(100,"Unity");
    }
    // Update is called once per frame
    void Update()
    {

    }
    void PostData(int Id,string Name)
    {
        WWWForm dataParameters = new WWWForm();
        dataParameters.AddField("Id", Id);
        dataParameters.AddField("Name", Name);
        WWW www = new WWW(Url,dataParameters);
        StartCoroutine("PostdataEnumerator", Url);
    }
    IEnumerator PostdataEnumerator(WWW www)
    {
        yield return www;
        if (www.error != null)
        {
            Debug.Log("Data Submitted");
        }
        else
        {
            Debug.Log(www.error);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM