客户端
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class _428Login : MonoBehaviour
{
public UILabel username;
public UILabel password;
// Use this for initialization
void Start()
{
}
//使用HTTP协议实现登录
public void Login()
{
JsonData data = new JsonData();
data["pro"] = 10001;
data["username"] = username.text;
data["password"] = password.text;
string msg = data.ToJson();
StartCoroutine(SendMsg(msg));
username.text = "";
password.text = "";
}
//使用HTTP协议实现注册
public void Register() {
JsonData data = new JsonData();
data["pro"] = 10002;
data["username"] = username.text;
data["password"] = password.text;
string msg = data.ToJson();
StartCoroutine(SendMsg(msg));
username.text = "";
password.text = "";
}
IEnumerator SendMsg(string data)
{
WWWForm form = new WWWForm();
form.AddField("", data);
WWW www = new WWW("http://localhost:10086/api/values", form);
yield return www;
if (www.error == null)
{
if (www.text != null)
{
string st= www.text.Replace("\\","");
st=st.Remove(0,1);
st = st.Remove(st.Length-1,1);
JsonData msg = JsonMapper.ToObject(st);
if (msg["pro"].ToString() == "10001") {
if (msg["res"].ToString() == "0") {
Debug.Log(msg["des"].ToString());
}
if (msg["res"].ToString() == "1") {
Debug.Log(msg["des"].ToString());
}
}
if (msg["pro"].ToString() == "10002") {
if (msg["res"].ToString() == "0")
{
Debug.Log(msg["des"].ToString());
}
if (msg["res"].ToString() == "1")
{
Debug.Log(msg["des"].ToString());
}
}
}
}
else {
Debug.Log(www.error);
}
}
}
服务器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Swashbuckle.Swagger.Annotations;
using LitJson;
namespace _428MonthServer.Controllers
{
public class ValuesController : ApiController
{
protected ValuesController() {
Init();
}
Dictionary hands = new Dictionary();
//初始化
void Init() {
LoginMoudle login = new LoginMoudle();
hands.Add(10001,login.Login);
hands.Add(10002,login.Register);
}
// GET api/values
[SwaggerOperation("GetAll")]
public IEnumerable Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[SwaggerOperation("GetById")]
[SwaggerResponse(HttpStatusCode.OK)]
[SwaggerResponse(HttpStatusCode.NotFound)]
public string Get(int id)
{
return "value";
}
// POST api/values
[SwaggerOperation("Create")]
[SwaggerResponse(HttpStatusCode.Created)]
public string Post([FromBody]string value)
{
JsonData data = JsonMapper.ToObject(value);
int pro = int.Parse(data["pro"].ToString());
MsgHand hand;
hands.TryGetValue(pro, out hand);
if (hand != null) {
return hand(data);
}
return null;
}
// PUT api/values/5
[SwaggerOperation("Update")]
[SwaggerResponse(HttpStatusCode.OK)]
[SwaggerResponse(HttpStatusCode.NotFound)]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[SwaggerOperation("Delete")]
[SwaggerResponse(HttpStatusCode.OK)]
[SwaggerResponse(HttpStatusCode.NotFound)]
public void Delete(int id)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//写数据类
public class User
{
public virtual int id { get; set; }
public virtual string username { get; set; }
public virtual string password { get; set; }
public virtual int isLogin { get; set; }
}
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//写映射类
public class UserMap : ClassMap
{
public UserMap() {
Table("users");
Id(x => x.id).Column("id");
Map(x => x.username).Column("username");
Map(x => x.password).Column("password");
Map(x => x.isLogin).Column("isLogin");
}
}
//委托
using LitJson;
public delegate string MsgHand(JsonData data);
//连接数据库
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class NHibernateHelper
{
private static ISessionFactory sessionFactory = null;
private static void Init() {
sessionFactory = Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(db => db
.Server("127.0.0.1")
.Database("428month")
.Username("root")
.Password("123456")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(e => e.Properties.Add("hbm2ddl.keywords", "none"))
.BuildSessionFactory();
}
public static ISessionFactory SessionFactory {
get {
if (sessionFactory == null)
Init();
return sessionFactory;
}
}
public static ISession OpenSession() {
return SessionFactory.OpenSession();
}
}
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//写数据库的用户表的操作类
public class DBMannger
{
//添加用户
public static void AddUser(string username, string password)
{
using (ISession session = NHibernateHelper.OpenSession())
{
User u = new User();
u.username = username;
u.password = password;
session.Save(u);
}
}
//根据用户名和密码同时匹配查询出对应的用户信息
public static User SelectUser(string username)
{
ISession session = NHibernateHelper.OpenSession();
IList list = session.QueryOver().Where(x => x.username == username).List();
if (list.Count > 0)
{
return list[0];
}
return null;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LitJson;
public class LoginMoudle
{
//服务器端接受客户端的登录数据,进行登录逻辑的编写
public string Login(JsonData data)
{
//newdata 给客户端发送结果
JsonData newdata = new JsonData();
//10001登录
newdata["pro"] = "10001";
//从客户端接受的输入账号和密码
string newusername = data["username"].ToString();
string newpassword = data["password"].ToString();
User oldUser = DBMannger.SelectUser(newusername);
//如果为空 说明数据库里面没有
if (oldUser == null)
{
//没有此用户0
newdata["res"] = "0";
newdata["des"] = "No User";
}
else {
if (oldUser.password != data["password"].ToString())
{
//密码错误0
newdata["res"] = "0";
newdata["des"] = "Password Mistake";
}
else {
//登陆成功1
newdata["res"] = "1";
newdata["des"] = "Login Successful";
}
}
return newdata.ToJson();
}
//服务器端接受客户端的登录数据,进行注册逻辑的编写
public String Register(JsonData data) {
//newdata 给客户端发送结果
JsonData newdata = new JsonData();
//10002注册
newdata["pro"] = "10002";
////从客户端接受的输入账号和密码
string newusername = data["username"].ToString();
string newpassword = data["password"].ToString();
User olduser = DBMannger.SelectUser(newusername);
////如果不为空 说明数据库里面有
if (olduser!=null)
{
//注册成功0
newdata["res"] = "0";
newdata["des"] = "Existing Users";
}
else {
//注册失败1
newdata["res"] = "1";
newdata["des"] = "Register Successful";
DBMannger.AddUser(newusername,newpassword);
}
return newdata.ToJson();
}
}