天天看點

【unity】與注冊登入伺服器互動原理及code

http://www.cnblogs.com/leng-yuye/archive/2012/08/01/2617971.html

主要用到了unity内置的WWW類和WWWForm類,運用WWWForm.AddField(String fieldName, String value)方法通過post的表單送出方式把表單參數傳遞給伺服器端的邏輯業務層。(我用的是JSP,在邏輯層上request.getParameter(fiedlName);就能得到AddField中傳遞的參數,接下來就是伺服器的邏輯處理了。PHP貌似是$_POST[fieldName]吧,好久沒寫過php了,ASP.NET不着咋寫...)

用戶端的demo效果圖:

【unity】與注冊登入伺服器互動原理及code

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_label2 = "password2:";
    string password_input2 = "";
    string email_label = "email:";
    string email_input = "";
    string callback_label = "result:";
    string callback_label2 = "";
    
    void OnStart()
    {
            
    }
    
    void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 80, 20), username_label);
        username_input = GUI.TextField(new Rect(80, 0, 100, 20), username_input);
        GUI.Label(new Rect(0, 30, 80, 20), password_label);
        password_input = GUI.TextField(new Rect(80, 30, 100, 20), password_input);
        GUI.Label(new Rect(0, 60, 80, 20), password_label2);
        password_input2 = GUI.TextField(new Rect(80, 60, 100, 20), password_input2);
        GUI.Label(new Rect(0, 90, 80, 20), email_label);
        email_input = GUI.TextField(new Rect(80, 90, 100, 20), email_input);
        GUI.Label(new Rect(0, 160, 80, 20), callback_label);
        callback_label2 = GUI.TextField(new Rect(50, 160, 160, 20), callback_label2);
        if (GUI.Button(new Rect(0, 120, 100, 30), "Login"))
        {
            form = new WWWForm();
            form.AddField("name", username_input);
            form.AddField("password", password_input);
            string url = "http://192.168.100.98:8084/ddt/UserLogin.jsp";
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestUserNameLogin(www));
        }
        if (GUI.Button(new Rect(120, 120, 100, 30), "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_input2);
            form.AddField("email", email_input);
            url = "http://192.168.100.98:8084/ddt/registerUser.jsp";
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestRegister(www));
        }
        if (GUI.Button(new Rect(240, 120, 100, 30), "non-reg to play"))
        {
            form = new WWWForm();
            form.AddField("id", SystemInfo.deviceUniqueIdentifier);
            //form.AddField("name", username_input);
            //form.AddField("password", password_input);
            //form.AddField("retry_password", password_input2);
            //form.AddField("email", email_input);
            url = "http://192.168.100.98:8084/ddt/NonRegPlay.jsp";
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestPhoneIdLogin(www));
        }
        if (GUI.Button(new Rect(200, 0, 130, 20), "Check UserName"))
        {
            form = new WWWForm();
            form.AddField("name", username_input);
            Debug.Log("username_input...." + username_input);
            url = "http://192.168.100.98:8084/ddt/CheckUserIsExist.jsp";
            www = new WWW(url, form);
            StartCoroutine(WaitForRequestCheck(www));
        }
        if (GUI.Button(new Rect(0, 200, 50, 30), "IMEI"))
        {
            callback_label2 = 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 = @"<span>([\S\s\t]*?)</span>";
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim();
                    if (result == "success")
                    {
                        callback_label2 = "登入成功";
                    }
                    else if (result == "empty")
                    {
                        callback_label2 = "使用者名或密碼為空";
                    }
                    else if (result == "fail")
                    {
                        callback_label2 = "找不到指定使用者";
                    }
                    else
                    {
                        callback_label2 = "未知錯誤";
                    }
                }
            }
        }
    }
    IEnumerator WaitForRequestRegister(WWW www)
    {
        yield return www;
        if (www.error != null)
            Debug.Log("fail to request..." + www.error);
        else
        {
            if (www.isDone)
            {
                string ex = @"<span>([\S\s\t]*?)</span>";
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim();
                    if (result == "success")
                    {
                        callback_label2 = "注冊成功";
                    }
                    else if (result == "empty")
                    {
                        callback_label2 = "使用者名或密碼為空";
                    }
                    else if (result == "equals")
                    {
                        callback_label2 = "兩次輸入密碼不一緻";
                    }
                    else if (result == "fail")
                    {
                        callback_label2 = "更新資料庫失敗";
                    }
                    else
                    {
                        callback_label2 = "未知錯誤";
                    }
                }
            }
        }
    }
    
    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 = @"<span>([\S\s\t]*?)</span>";
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
               {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim();
                    if (result == "empty")
                    {
                        callback_label2 = "使用者名為空";
                    }
                    else if (result == "nothing")
                    {
                        callback_label2 = "使用者名不存在,可以注冊";
                    }
                    else if (result == "exist")
                    {
                        callback_label2 = "使用者名已存在";
                    }
                    else
                    {
                        callback_label2 = "未知錯誤";
                    }
                }
            }
        }
    }
    IEnumerator WaitForRequestPhoneIdLogin(WWW www)
    {
        yield return www;
        if (www.error != null)
            Debug.Log("fail to request..." + www.error);
        else
        {
            if (www.isDone)
            {
                string ex = @"<span>([\S\s\t]*?)</span>";
                Match m = Regex.Match(www.data, ex);
                if (m.Success)
                {
                    string result = m.Value;
                    result = result.Substring(result.IndexOf(">") + 1, result.LastIndexOf("<") - result.IndexOf(">") - 1).Trim();
                    if (result == "ok")
                    {
                        callback_label2 = "手機ID登入成功";
                    }
                    else if (result == "error")
                    {
                        callback_label2 = "手機ID登入成功";
                    }
                    else
                    {
                        callback_label2 = "未知錯誤";
                    }
                }
            }
        }
    }

}      
<%@ page language="java" import="ddt.*" %>

<jsp:useBean id="user" scope="session" class="ddt.UserBean" />

<
%@page
 contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

   "
http://www.w3.org/TR/html4/loose.dtd
">

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Page</title>

    </head>

    <body>

        <%

                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);

        %>

    </body>

</html>      
www

繼續閱讀