GlobalConfig.ConnectionReadyForSend

切换到手册
public Action<int,int> ConnectionReadyForSend ;

描述

定义可用于在连接支持发送数据时获取通知的回调委托。

仅在您通过调用 NetworkTransport.NotifyWhenConnectionReadyForSend 明确指出要接收此通知时调用该回调。

此回调将从 IO 线程调用。如果您通过此回调长时间执行某个操作,它会影响您的 IO 操作性能,因此,您应尽快从此类操作中返回。

using System;
using UnityEngine;
using UnityEngine.Networking;

public class Test : MonoBehaviour { static bool isSend = true; //...

public static void SendCallback(int hostId, int connectionId) { isSend = true; //we can send again }

void Start() { GlobalConfig config = new GlobalConfig(); config.ConnectionReadyForSend = SendCallback; //set up callback for send notification

NetworkTransport.Init(config);

//.... }

void Update() { int testHostId = 1; int testConnectionId = 1; int testChannelId = 1; var testArray = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; byte error; while (isSend) { isSend = NetworkTransport.Send(testHostId, testConnectionId, testChannelId, testArray, testArray.Length, out error); if (isSend == false) { NetworkTransport.NotifyWhenConnectionReadyForSend(testHostId, testConnectionId, 2, out error); //ask lib to call SendCallback() when connection will be ready again and we will be able to send as minimum 2 messages break; } } } }