114 lines
3.1 KiB
C#
114 lines
3.1 KiB
C#
|
|
using System.Linq;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Net.Sockets;
|
|||
|
|
using System.Text;
|
|||
|
|
using Stary.Evo;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace Main
|
|||
|
|
{
|
|||
|
|
public interface IClientSocketSystem : ISystem
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 连接
|
|||
|
|
/// </summary>
|
|||
|
|
void OnConnect(string ip);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 向服务端发送消息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="type">消息类型</param>
|
|||
|
|
/// <param name="message">消息内容</param>
|
|||
|
|
void Send(SocketEvent type, string message);
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 断开连接
|
|||
|
|
/// </summary>
|
|||
|
|
void DisConnect();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public class ClientSocketSystem : AbstractSystem, IClientSocketSystem
|
|||
|
|
{
|
|||
|
|
private SocketClient _socketClient;
|
|||
|
|
|
|||
|
|
public override void Dispose()
|
|||
|
|
{
|
|||
|
|
// 注意由于Unity编译器环境下,游戏开启/关闭只影响主线程的开关,游戏关闭回调时需要通过Close函数来关闭服务端/客户端的线程。
|
|||
|
|
if (_socketClient != null) _socketClient.Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 向服务端发送消息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="type">消息类型</param>
|
|||
|
|
/// <param name="message">消息内容</param>
|
|||
|
|
public void Send(SocketEvent type, string message)
|
|||
|
|
{
|
|||
|
|
var bytes = Encoding.UTF8.GetBytes(message);
|
|||
|
|
_socketClient.Send((ushort)type, bytes);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 退出服务端
|
|||
|
|
/// </summary>
|
|||
|
|
public void DisConnect()
|
|||
|
|
{
|
|||
|
|
// 踢出连接
|
|||
|
|
_socketClient.DisConnect();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void OnConnect(string ip)
|
|||
|
|
{
|
|||
|
|
_socketClient = new SocketClient(ip, 10000);
|
|||
|
|
|
|||
|
|
|
|||
|
|
_socketClient.Connect(() =>
|
|||
|
|
{
|
|||
|
|
Debug.Log("连接成功");
|
|||
|
|
_socketClient.OnReceive += OnReceive;
|
|||
|
|
// _client.DisConnect();
|
|||
|
|
}, () => { Debug.Log("连接失败"); });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Connect()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected override void OnInit()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 接收客户端消息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="client">指定客户端</param>
|
|||
|
|
/// <param name="data">消息数据</param>
|
|||
|
|
private void OnReceive(SocketDataPack data)
|
|||
|
|
{
|
|||
|
|
Debug.LogFormat("接收到数据>>>{0} {1}", (SocketEvent)data.Type, data.Buff.Length);
|
|||
|
|
|
|||
|
|
switch ((SocketEvent)data.Type)
|
|||
|
|
{
|
|||
|
|
case SocketEvent.ServerMessage:
|
|||
|
|
Debug.LogFormat("接收到服务器数据 >>> {0}", Encoding.UTF8.GetString(data.Data));
|
|||
|
|
var message = Encoding.UTF8.GetString(data.Data);
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
case SocketEvent.ClientDisconn:
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public string GetLocalIPv4()
|
|||
|
|
{
|
|||
|
|
return Dns.GetHostEntry(Dns.GetHostName())
|
|||
|
|
.AddressList.First(f => f.AddressFamily == AddressFamily.InterNetwork)
|
|||
|
|
.ToString();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|