zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    别做操之过急的”无效将军”,做实实在在的”日拱一卒”
    大话三种个性化推荐,你喜欢哪一种?
    程序员的厚德载物(上)
    腾讯或联姻优酷,微信嫁女模式引发互联网通婚潮流
    [PHP知识点乱炖]四、全局变量——小偷从良记
    小谈程序员创业者的”劣根性”
    腾讯程序员一年3亿代码意味着什么?
    腾讯和京东做了连襟:一个枪和弹合作的故事
    程序员初学者如何自学编程另类版
    从软件公司的企业文化浅谈什么是管理能力
  • 原文地址:https://www.cnblogs.com/unity3d-Yang/p/6404528.html
Copyright © 2011-2022 走看看