scut和unity之间收发请求返回
Posted Unity3d师兄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scut和unity之间收发请求返回相关的知识,希望对你有一定的参考价值。
前期首先配置好scut服务器,我在上篇博客中已经写到
1、在scut中设置协议,协议号100,版本号,设置请求参数String类型的userid和userpassword和返回参数的string字符串Content
2、将网页中显示的服务器代码copy到vs中命名为Action100的类中
using System; using System.Collections.Generic; using ZyGames.Framework.Game.Contract.Action; using ZyGames.Framework.Game.Service; namespace GameServer.CsScript.Action { /// <summary> /// helloworld /// </summary> /// <remarks>继续BaseStruct类:允许无身份认证的请求;AuthorizeAction:需要身份认证的请求</remarks> public class Action100 : AuthorizeAction { #region class object #endregion /// <summary> /// 用户id /// </summary> private string _userID; /// <summary> /// 用户密码 /// </summary> private string _userPassword; /// <summary> /// /// </summary> private string _content; public Action100(ActionGetter actionGetter) : base((short)100, actionGetter) { } /// <summary> /// 检查的Action是否需要授权访问 /// </summary> protected override bool IgnoreActionId { get { return true; } } /// <summary> /// 客户端请求的参数较验 /// </summary> /// <returns>false:中断后面的方式执行并返回Error</returns> public override bool GetUrlElement() { if (httpGet.GetString("UserID", ref _userID) && httpGet.GetString("UserPassword", ref _userPassword)) {
_congtent = "server data"; return true; } return false; } /// <summary> /// 业务逻辑处理 /// </summary> /// <returns>false:中断后面的方式执行并返回Error</returns> public override bool TakeAction() { return true; } /// <summary> /// 下发给客户的包结构数据 /// </summary> public override void BuildPacket() { this.PushIntoStack(_content); } } }
3、运行vs中scut服务端
4、编写UNITY中一个登陆demo,登陆成功发送请求,返回一个_content字符串
using UnityEngine; using System.Collections; using System.Collections.Generic; using GameRanking.Pack; public class GameLogin : MonoBehaviour { [SerializeField] private UIInput aPhoneLoginInput = null; [SerializeField] private UIInput aPhonePasswordInput = null; [SerializeField] private UILabel aTipsLabel = null; private string userId; private string userPassword; private ActionParam actionParam; //todo 启用自定的结构 bool useCustomAction = false; public void OnUserLogin() { if (aPhoneLoginInput.value == "" || aPhonePasswordInput.value == "") { Debug.Log("未输入用户名密码"); OnFadeIn(); aTipsLabel.text = "请输入手机号或是密码"; } else { userId = aPhoneLoginInput.value; userPassword = aPhonePasswordInput.value; if (useCustomAction) { Net.Instance.HeadFormater = new CustomHeadFormater(); Request1001Pack requestPack = new Request1001Pack() { PageIndex = 1, PageSize = 20 }; actionParam = new ActionParam(requestPack); } else { actionParam = new ActionParam(); actionParam["userid"] = userId; actionParam["userpassword"] = userPassword; } Debug.Log("点击成功userid" + actionParam["userid"] + "userpassword" + actionParam["userpassword"]); NetWriter.SetUrl("127.0.0.1:9001"); Net.Instance.Send(100, HelloWorldReturn,actionParam); } } void HelloWorldReturn(ActionResult action) { Debug.Log(action["Content"]); } void GetUser() { userId = aPhoneLoginInput.value; userPassword = aPhonePasswordInput.value; } void OnFadeIn() { aTipsLabel.gameObject.SetActive(true); TweenAlpha ta = aTipsLabel.gameObject.GetComponent<TweenAlpha>(); ta.ResetToBeginning(); ta.enabled = true; ta.PlayForward(); EventDelegate.Set(ta.onFinished, OnFadeOut); } void OnFadeOut() { aTipsLabel.gameObject.SetActive(false); } }
以上是关于scut和unity之间收发请求返回的主要内容,如果未能解决你的问题,请参考以下文章