我不知道为什么会这样。我正在创建一个对象,然后尝试将其作为字节数组从我的客户端通过本地网络发送到我的服务器,当多个客户端连接到服务器时,我收到错误Trying to send command for object without authority.
同时。如果我创建的对象是一个序列化的字节数组,那么如何赋予它权限?
我得到的完整错误是
Trying to send command for object without authority.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
Network_Transmitter:CallCmdPrepareToReceiveBytes(Int32, Int32)
<DoSendBytes>c__Iterator0:MoveNext() (at Assets/Scripts/Networking/Network_Transmitter.cs:46)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Player_Data:PrepareServerData() (at Assets/Scripts/Player/Player_Data.cs:77)
Event_Manager:SendTextureData() (at Assets/Scripts/Event_Manager.cs:31)
Game_UI:SendData() (at Assets/Scripts/Game_UI.cs:39)
UnityEngine.EventSystems.EventSystem:Update()
我创建的对象:
[Serializable]
public class PlayerObject
{
public byte[] texBytes;
public int texWidth;
public int texHeight;
public TextureFormat texFormat;
public float tranX;
public float tranY;
public float tranZ;
public string type;
public string id;
public int strength;
public int hitpoints;
}
以及发生错误的函数在这里:
[Client]
public IEnumerator DoSendBytes(int transmissionId, byte[] data)
{
Debug.Assert(!clientTransmissionIds.Contains(transmissionId));
Debug.Log(LOG_PREFIX + "SendBytesToClients processId=" + transmissionId + " | datasize=" + data.Length);
//tell server that he is going to receive some data and tell him how much it will be.
CmdPrepareToReceiveBytes(transmissionId, data.Length);
yield return null;
//begin transmission of data. send chunks of 'bufferSize' until completely transmitted.
clientTransmissionIds.Add(transmissionId);
TransmissionData dataToTransmit = new TransmissionData(data);
int bufferSize = defaultBufferSize;
while (dataToTransmit.curDataIndex < dataToTransmit.data.Length - 1)
{
//determine the remaining amount of bytes, still need to be sent.
int remaining = dataToTransmit.data.Length - dataToTransmit.curDataIndex;
if (remaining < bufferSize)
bufferSize = remaining;
//prepare the chunk of data which will be sent in this iteration
byte[] buffer = new byte[bufferSize];
System.Array.Copy(dataToTransmit.data, dataToTransmit.curDataIndex, buffer, 0, bufferSize);
//send the chunk
CmdReceiveBytes(transmissionId, buffer);
dataToTransmit.curDataIndex += bufferSize;
yield return null;
if (null != OnDataFragmentSent)
OnDataFragmentSent.Invoke(transmissionId, buffer);
}
//transmission complete.
clientTransmissionIds.Remove(transmissionId);
if (null != OnDataComepletelySent)
OnDataComepletelySent.Invoke(transmissionId, dataToTransmit.data);
}
[Command]
private void CmdPrepareToReceiveBytes(int transmissionId, int expectedSize)
{
if (serverTransmissionData.ContainsKey(transmissionId))
return;
//prepare data array which will be filled chunk by chunk by the received data
TransmissionData receivingData = new TransmissionData(new byte[expectedSize]);
serverTransmissionData.Add(transmissionId, receivingData);
}
//use reliable sequenced channel to ensure bytes are sent in correct order
[Command]
private void CmdReceiveBytes(int transmissionId, byte[] recBuffer)
{
//already completely received or not prepared?
if (!serverTransmissionData.ContainsKey(transmissionId))
return;
//copy received data into prepared array and remember current dataposition
TransmissionData dataToReceive = serverTransmissionData[transmissionId];
System.Array.Copy(recBuffer, 0, dataToReceive.data, dataToReceive.curDataIndex, recBuffer.Length);
dataToReceive.curDataIndex += recBuffer.Length;
if (null != OnDataFragmentReceived)
OnDataFragmentReceived(transmissionId, recBuffer);
if (dataToReceive.curDataIndex < dataToReceive.data.Length - 1)
//current data not completely received
return;
//current data completely received
Debug.Log(LOG_PREFIX + "Completely Received Data at transmissionId=" + transmissionId);
serverTransmissionData.Remove(transmissionId);
if (null != OnDataCompletelyReceived)
OnDataCompletelyReceived.Invoke(transmissionId, dataToReceive.data);
}
整个项目可以在这里找到:https://github.com/kenmarold/HLAPI-Prototype