mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
完善网络框架,增加服务器断开连接回调
This commit is contained in:
@@ -79,7 +79,7 @@ namespace GameLogic
|
||||
|
||||
if (Scene.Session == null || Scene.Session.IsDisposed)
|
||||
{
|
||||
Scene.CreateSession(address, ProtocolType, OnConnectComplete, OnConnectFail);
|
||||
Scene.CreateSession(address, ProtocolType, OnConnectComplete, OnConnectFail, OnConnectDisconnect);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +95,12 @@ namespace GameLogic
|
||||
Log.Warning("Could not connect to server");
|
||||
}
|
||||
|
||||
private void OnConnectDisconnect()
|
||||
{
|
||||
Status = GameClientStatus.StatusClose;
|
||||
Log.Warning("OnConnectDisconnect server");
|
||||
}
|
||||
|
||||
public virtual void Send(object message, uint rpcId = 0, long routeId = 0)
|
||||
{
|
||||
if (Scene.Session == null)
|
||||
|
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections;
|
||||
using TEngine.Core.Network;
|
||||
using TEngine.Core;
|
||||
using TEngine.Logic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
@@ -62,7 +63,7 @@ namespace TEngine.Demo
|
||||
// onConnectComplete:当跟服务器建立连接后的回调
|
||||
// onConnectFail:当网络无法连接或出错时的回调
|
||||
// connectTimeout:连接超时时间、默认是5000毫秒
|
||||
Scene.CreateSession("127.0.0.1:20000", NetworkProtocolType.KCP, OnConnectComplete, OnConnectFail);
|
||||
Scene.CreateSession("127.0.0.1:20000", NetworkProtocolType.KCP, OnConnectComplete, OnConnectFail, OnConnectDisconnect);
|
||||
// 注意:框架使用的ProtoBuf协议、文件定义的位置在Demo下Config/ProtoBuf/里
|
||||
// 生成协议是在服务器工程生成
|
||||
// ProtoBuf有三个文件:
|
||||
@@ -74,6 +75,7 @@ namespace TEngine.Demo
|
||||
private void OnConnectComplete()
|
||||
{
|
||||
IsConnect = true;
|
||||
Scene.Session.AddComponent<SessionHeartbeatComponent>().Start(15);
|
||||
LogDebug("已连接到服务器");
|
||||
}
|
||||
|
||||
@@ -82,6 +84,12 @@ namespace TEngine.Demo
|
||||
IsConnect = false;
|
||||
LogError("无法连接到服务器");
|
||||
}
|
||||
|
||||
private void OnConnectDisconnect()
|
||||
{
|
||||
IsConnect = false;
|
||||
LogError("服务器主动断开了连接");
|
||||
}
|
||||
|
||||
private void OnSendButtonClick()
|
||||
{
|
||||
|
Reference in New Issue
Block a user