230 lines
7.3 KiB
C#
230 lines
7.3 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Fantasy;
|
||
using Fantasy.Async;
|
||
using Fantasy.Network;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class Entry : MonoBehaviour
|
||
{
|
||
private Scene _scene;
|
||
private Session _session;
|
||
|
||
public Button LoginButton;
|
||
public Button ExitButton;
|
||
public Button SendTestButton;
|
||
public Button GetHaveMailButton;
|
||
public Button OpenMailButton;
|
||
public Button ReceiveMailButton;
|
||
public Button RemoveMailButton;
|
||
public Button SendMailButton;
|
||
public InputField LoginUserName;
|
||
public InputField MailId;
|
||
public InputField MailTitle;
|
||
public InputField MailUserName;
|
||
public InputField MailContent;
|
||
void Start()
|
||
{
|
||
LoginButton.onClick.RemoveAllListeners();
|
||
LoginButton.onClick.AddListener(OnLoginButtonClick);
|
||
ExitButton.onClick.RemoveAllListeners();
|
||
ExitButton.onClick.AddListener(OnExitButtonClick);
|
||
SendTestButton.onClick.RemoveAllListeners();
|
||
SendTestButton.onClick.AddListener(() =>
|
||
{
|
||
OnSendTestButtonClick().Coroutine();
|
||
});
|
||
GetHaveMailButton.onClick.RemoveAllListeners();
|
||
GetHaveMailButton.onClick.AddListener(() =>
|
||
{
|
||
OnGetHaveMailButtonClick().Coroutine();
|
||
});
|
||
OpenMailButton.onClick.RemoveAllListeners();
|
||
OpenMailButton.onClick.AddListener(() =>
|
||
{
|
||
OnOpenMailButtonClick().Coroutine();
|
||
});
|
||
ReceiveMailButton.onClick.RemoveAllListeners();
|
||
ReceiveMailButton.onClick.AddListener(() =>
|
||
{
|
||
OnReceiveMailButtonClick().Coroutine();
|
||
});
|
||
RemoveMailButton.onClick.RemoveAllListeners();
|
||
RemoveMailButton.onClick.AddListener(() =>
|
||
{
|
||
OnRemoveMailButtonClick().Coroutine();
|
||
});
|
||
SendMailButton.onClick.RemoveAllListeners();
|
||
SendMailButton.onClick.AddListener(() =>
|
||
{
|
||
OnSendMailButtonClick().Coroutine();
|
||
});
|
||
StartAsync().Coroutine();
|
||
}
|
||
|
||
private async FTask StartAsync()
|
||
{
|
||
Fantasy.Platform.Unity.Entry.Initialize(GetType().Assembly);
|
||
_scene = await Scene.Create(SceneRuntimeType.MainThread);
|
||
}
|
||
|
||
private void OnLoginButtonClick()
|
||
{
|
||
// 1、判定用户输入的用户名是否为空,如果是空,则提示用户输入用户名。
|
||
|
||
if (string.IsNullOrEmpty(LoginUserName.text))
|
||
{
|
||
Log.Error("用户名不可以为空!");
|
||
return;
|
||
}
|
||
|
||
// 2、首先创建一个连接,并且设置连接成功和失败的回调。
|
||
_session = _scene.Connect("127.0.0.1:20000", NetworkProtocolType.KCP, () =>
|
||
{
|
||
Log.Debug("连接成功");
|
||
// 首先要判定一下。当前的Session是否被断开或销毁了
|
||
if (_session.IsDisposed)
|
||
{
|
||
return;
|
||
}
|
||
// 添加一个心跳组件,用来跟服务器保持心跳
|
||
_session.AddComponent<SessionHeartbeatComponent>().Start(2000);
|
||
// 执行登陆请求
|
||
LoginRequest().Coroutine();
|
||
}, () =>
|
||
{
|
||
Log.Debug("连接失败");
|
||
}, () =>
|
||
{
|
||
// 能触发到连接断开的委托的时候只有如下两种情况:
|
||
// 1、服务器主动跟客户端断开连接。
|
||
// 2、客户端主动跟服务器断开连接。
|
||
Log.Debug("断开连接");
|
||
}, false, 3000);
|
||
}
|
||
|
||
private void OnApplicationQuit()
|
||
{
|
||
_scene.Dispose();
|
||
}
|
||
|
||
private async FTask LoginRequest()
|
||
{
|
||
var userName = LoginUserName.text;
|
||
var response = (G2C_LoginResponse)await _session.Call(new C2G_LoginRequest()
|
||
{
|
||
Name = userName
|
||
});
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"登陆时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
Log.Debug($"登陆成功,UserName:{userName}");
|
||
}
|
||
|
||
private void OnExitButtonClick()
|
||
{
|
||
if (_session == null || _session.IsDisposed)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_session.Send(new C2G_Exit());
|
||
Log.Debug("已经跟服务器发送了断开的协议!");
|
||
}
|
||
|
||
private async FTask OnSendTestButtonClick()
|
||
{
|
||
var userName = LoginUserName.text;
|
||
var response = (Mail2C_TestResponse)await _session.Call(new C2Mail_TestRequest()
|
||
{
|
||
Tag = userName
|
||
});
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"测试时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
Log.Debug($"接收到服务器发送来的自定义Route协议,Tag:{response.Tag}");
|
||
}
|
||
|
||
private async FTask OnGetHaveMailButtonClick()
|
||
{
|
||
var response = (Mail2C_GetHaveMailResposne)await _session.Call(new C2Mail_GetHaveMailRequest());
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"获取邮件时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
foreach (var mailSimplifyInfo in response.Mails)
|
||
{
|
||
MailId.text = mailSimplifyInfo.MailId.ToString();
|
||
Log.Debug($"获取到邮件,MailId:{mailSimplifyInfo.MailId},MailType:{mailSimplifyInfo.MailType} 标题:{mailSimplifyInfo.Title}");
|
||
}
|
||
|
||
Log.Debug($"获取到邮件数量:{response.Mails.Count}");
|
||
}
|
||
|
||
private async FTask OnOpenMailButtonClick()
|
||
{
|
||
var response = (Mail2C_OpenMailResposne)await _session.Call(new C2Mail_OpenMailRequest()
|
||
{
|
||
MailId = long.Parse(MailId.text), ReturnMailInfo = true
|
||
});
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"打开邮件时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
Log.Debug($"打开邮件成功,邮件Id:{response.MailInfo.MailId} 标题:{response.MailInfo.Title} 内容:{response.MailInfo.Content}");
|
||
}
|
||
|
||
private async FTask OnReceiveMailButtonClick()
|
||
{
|
||
var response = (Mail2C_ReceiveMailResponse)await _session.Call(new C2Mail_ReceiveMailRequest()
|
||
{
|
||
MailId = long.Parse(MailId.text)
|
||
});
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"领取邮件时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
Log.Debug($"领取邮件附件成功");
|
||
}
|
||
|
||
private async FTask OnRemoveMailButtonClick()
|
||
{
|
||
var response = (Mail2C_RemoveMailResponse)await _session.Call(new C2Mail_RemoveMailRequest()
|
||
{
|
||
MailId = long.Parse(MailId.text)
|
||
});
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"删除邮件时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
Log.Debug($"删除邮件成功");
|
||
}
|
||
|
||
private async FTask OnSendMailButtonClick()
|
||
{
|
||
var response = (Mail2C_SendMailResponse)await _session.Call(new C2Mail_SendMailRequest()
|
||
{
|
||
UserName = MailUserName.text,
|
||
Title = MailTitle.text,
|
||
Content = MailContent.text,
|
||
Money = 100
|
||
});
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"发送邮件时发生错误。ErrorCode:{response.ErrorCode}");
|
||
return;
|
||
}
|
||
Log.Debug($"发送邮件成功");
|
||
}
|
||
}
|