Unity与注册登录服务器交互原理及code

主要用到了unity内置的WWW类和WWWForm类,运用WWWForm.AddField(String fieldName, String value)方法通过post的表单提交方式把表单参数传递给服务器端的逻辑业务层。

客户端的demo效果图:

 

imei是手机的唯一识别id,用imei表示可能不恰当.

客户端代码:

using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public class Client : MonoBehaviour
{
    WWW www;
    WWWForm form;
    string url;
    string username_label = “username:”; string username_input = “”;
    string password_label = “password:”; string password_input = “”;
    string password_label = “password:”; string password_input = “”;
    string email_label = “email:”; string email_input = “”;
    string callback_label = “result:”; string callback_label = “”;

    void OnStart()
    {
    }

    void OnGUI()
    {
        GUI.Label(new Rect(,,,), username_label);
        username_input = GUI.TextField(new Rect(,,,), username_input);
        GUI.Label(new Rect(,,,), password_label);
        password_input = GUI.TextField(new Rect(,,,), password_input);
        GUI.Label(new Rect(,,,), password_label);
        password_input = GUI.TextField(new Rect(,,,), password_input);
        GUI.Label(new Rect(,,,), email_label);
        email_input = GUI.TextField(new Rect(,,,), email_input);
        GUI.Label(new Rect(,,,), callback_label);
        callback_label = GUI.TextField(new Rect(,,,), callback_label);
        if (GUI.Button(new Rect(,,,),  “Login”)) {
            form = new WWWForm();
            form.AddField(“name”, username_input);
            form.AddField(“password”, password_input);
            string url = “http: //...:/ddt/UserLogin.jsp”;
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestUserNameLogin(www));
        }
        if (GUI.Button(new Rect(,,,),  “Register”)) {
            form = new WWWForm();
            form.AddField(“id”, “phone_id_str”);
            form.AddField(“id”, SystemInfo.deviceUniqueIdentifier);
            form.AddField(“name”, username_input);
            form.AddField(“password”, password_input);
            form.AddField(“retry_password”, password_input);
            form.AddField(“email”, email_input);
            url = “http: //...:/ddt/registerUser.jsp”;
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestRegister(www));
        }
        if (GUI.Button(new Rect(,,,),  “non - reg to play”)) {
            form = new WWWForm();
            form.AddField(“id”, SystemInfo
                .deviceUniqueIdentifier);
            form.AddField(“name”, username_input);
            form.AddField(“password”, password_input);
            orm.AddField(“retry_password”, password_input);
            form.AddField(“email”, email_input);
            url = “http: //...:/ddt/NonRegPlay.jsp”;
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestPhoneIdLogin(www));
        }
        if (GUI.Button(new Rect(,,,),  “Check UserName”)
            ) {
            form = new WWWForm();
            form.AddField(“name”, username_input);
            Debug.Log(“username_input....” +username_input);
            url = “http: //...:/ddt/CheckUserIsExist.jsp”;
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestCheck(www));
        }
        if (GUI.Button(new Rect(,,,),  “IMEI”)) {
            callback_label = SystemInfo.deviceUniqueIdentifier;
        }
    }

    IEnumerator WaitForRequestUserNameLogin(WWW www)
    {
        yield return www;
        if (www.error != null) Debug.Log(“
        fail to request...” +www.error); else {
            if (www.isDone)
            {
                string ex = @“([\
                S\s\t]*?)”;
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
                    if (result == “success”) {
                        callback_label = “登录成功”;
                    } else if (
                        result == “empty”) {
                        callback_label = “用户名或密码为空”;
                    } else if (result == “fail”) {
                        callback_label = “找不到指定用户”;
                    } else {
                        callback_label = “未知错误”;
                    }
                }
            }
        }
    }

    IEnumerator WaitForRequestRegister(WWW www)
    {
        yield return www;
        if (www.error != null) Debug.Log(“
        fail to request...” +www.error); else {
            if (www.isDone)
            {
                string ex = @“([\
                S\s\t]*?)”;
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
                    if (result == “success”) {
                        callback_label = “注册成功”;
                    } else if (result == “empty”) {
                        callback_label = “用户名或密码为空”;
                    } else if (result == “equals”) {
                        callback_label = “两次输入密码不一致”;
                    } else if (result == “fail”) {
                        callback_label = “更新数据库失败”;
                    } else {
                        callback_label = “未知错误”;
                    }
                }
            }
        }
    }

    IEnumerator WaitForRequestCheck(WWW www)
    {
        yield return www;
        if (www.error != null) Debug.Log(“
        fail to request...” +www.error); else {
            if (www.isDone)
            {
                Debug.Log(“data-- >” +www.data);
                string ex = @“([\
                S\s\t]*?)”;
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
                    if (result == “empty”) {
                        callback_label = “用户名为空”;
                    } else if (result == “nothing”) {
                        callback_label = “用户名不存在,可以注册”;
                    } else if (result == “exist”) {
                        callback_label = “用户名已存在”;
                    } else {
                        callback_label = “未知错误”;
                    }
                }
            }
        }
    }

    IEnumerator WaitForRequestPhoneIdLogin(WWW www)
    {
        yield return www;
        if (www.error != null) Debug.Log(“
        fail to request...” +www.error); else {
            if (www.isDone)
            {
                string ex = @“([\
                S\s\t]*?)”;
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(“ >”) + , result.LastIndexOf(“”) - ).Trim();
                    if (result == “ok”) {
                        callback_label = “手机ID登录成功”;
                    } else if (result == “error”) {
                        callback_label = “手机ID登录成功”;
                    } else {
                        callback_label = “未知错误”;
                    }
                }
            }
        }
    }
}

服务器端注册逻辑:

<% String id = request.getParameter("id"); String username = request.getParameter("name"); String password = request.getParameter("password"); String retry_password = request.getParameter("retry_password"); String email = request.getParameter("email"); user.processRegisterUserRequest(id, username, password, retry_password, email, request, response);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姑娘很大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值