【问题标题】:How to call a method on server side from c#如何从c#调用服务器端的方法
【发布时间】:2016-06-06 06:34:09
【问题描述】:

我在堆栈溢出时得到了这个 QA --How to call code behind server method from a client side javascript function?

然而,它是从javascript调用函数,我该怎么做从c#。我用unity3d开发一个ios应用,之前没做过。有人知道怎么做吗?

提前致谢。

附注关于后端

后端如下所示。 网址:https://{服务器地址}/register 参数:

  • 用户名,字符串
  • 名称,字符串
  • 密码,字符串

并返回: - user_id, int

我用正确的参数运行服务器方法,如果参数正确,它会返回一个int/user_id

【问题讨论】:

  • 实际场景是什么,你的服务器端代码请提供详细信息。
  • 由于网络应用程序和非网络应用程序之间的巨大技术差异,这个链接的问题可以说是无用的。
  • @MickyD 感谢您的建议。
  • @MohammadFaizanKhan 我将更改问题以发布服务器代码。
  • @tim 是服务器端的这个 php

标签: c# .net unity3d server backend


【解决方案1】:

WWW unity3d 类,这可能是您正在寻找的。万维网是

一个用于检索 URL 内容的小型实用模块。

有关使用 C# 与服务器 (PHP) 交互的详细信息,我建议您使用 unitywiki link。您将需要按照本教程的建议执行类似的操作。

 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.
        }
    }

您可以根据您的规范自定义主要使用的重置内容是带有Co-routine 的WWW。

【讨论】:

  • 谢谢,通过你的回答,我知道了如何获取数据,如何将数据发布到服务器,将数据作为参数发送到服务器?你能帮帮我吗?
  • 显然它正在使用参数向服务器发送数据,您检查链接了吗
  • 对不起,我忽略了它。谢谢。
【解决方案2】:

我从this blog -- devindia 找到了一篇关于此的帖子

在face中,将WWWForm作为辅助参数添加到WWW,然后是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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多