2017-10-18 235 views
0

我試圖爲一個着色書類型應用程序的網絡原型,我正在開發,它允許用戶創建一個圖像,然後將該圖像發送到服務器,然後將圖像並將其包裹在實例化的網格中。我在這個迭代中最初的問題是緩衝區大小。我得到錯誤:NetworkWriter WriteBytes:緩衝區太大

NetworkWriter WriteBytes: buffer is too large (699064) bytes. The maximum buffer size is 64K bytes. 
UnityEngine.Networking.NetworkWriter:WriteBytesFull(Byte[]) 
TextureMessage:Serialize(NetworkWriter) 
UnityEngine.Networking.NetworkServer:SendToAll(Int16, MessageBase) 
Server:SendTexture(Texture2D, String) (at Assets/Scripts/Server.cs:32) 
Server:SendOnButtonPress() (at Assets/Scripts/Server.cs:19) 
UnityEngine.EventSystems.EventSystem:Update() 

如何增加緩衝區大小?我的圖像將在1.5-2mb範圍內。到目前爲止我的代碼如下:

public class MyMsgType 
{ 
    public static short texture = MsgType.Highest + 1; 
} 

public class TextureMessage : MessageBase 
{ 
    public byte[] textureBytes; 
    public string message; //Optional 
} 

服務器端代碼:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.Networking; 

public class Server : MonoBehaviour { 

    public Texture2D textureToSend; 
    string messageToSend = "Test Message"; 

    // Use this for initialization 
    void Start() { 
     NetworkManager.singleton.StartHost(); 
     Debug.Log("Server Started."); 
    } 

    public void SendOnButtonPress() 
    { 
     SendTexture(textureToSend, messageToSend); 
    } 

    //Call to send the Texture and a simple string message 
    public void SendTexture(Texture2D texture, string message) 
    { 
     TextureMessage msg = new TextureMessage(); 

     //Convert Texture2D to byte array 

     msg.textureBytes = texture.GetRawTextureData(); 
     msg.message = message; 

     NetworkServer.SendToAll(MyMsgType.texture, msg); 
    } 
} 

客戶端代碼:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.Networking; 

public class Client : MonoBehaviour 
{ 
    NetworkClient myClient; 

    // Use this for initialization 
    void Start() { 
     NetworkManager.singleton.StartClient(); 
     Debug.Log("Client Started."); 

     setupClient(); 
    } 

    // Create a client and connect to the server port 
    public void setupClient() 
    { 
     //Create new client 
     myClient = new NetworkClient(); 
     //Register to connect event 
     myClient.RegisterHandler(MsgType.Connect, OnConnected); 
     //Register to texture receive event 
     myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive); 

     //Connect to server 
     myClient.Connect("127.0.0.1", 4444); 
    } 

    //Called when texture is received 
    public void OnTextureReceive(NetworkMessage netMsg) 
    { 
     TextureMessage msg = netMsg.ReadMessage<TextureMessage>(); 

     //Your Received message 
     string message = msg.message; 
     Debug.Log("Texture Messsage " + message); 

     //Your Received Texture2D 
     Texture2D receivedtexture = new Texture2D(4, 4); 
     receivedtexture.LoadRawTextureData(msg.textureBytes); 
     receivedtexture.Apply(); 
    } 

    public void OnConnected(NetworkMessage netMsg) 
    { 
     Debug.Log("Connected to server"); 
    } 
} 

回答

1

你需要通過設置NetworkManager.connectionConfig建立一個自定義配置到一個ConnectionConfig對象,您已經設置了較大的數據包大小。示例代碼應該讓你開始:

void Start() 
{ 
    ConnectionConfig myConfig = new ConnectionConfig(); 
    myConfig.AddChannel(QosType.Unreliable); 
    myConfig.AddChannel(QosType.UnreliableFragmented); 
    myConfig.PacketSize = 1440; 
    NetworkManager.connectionConfig = myConfig; 
} 
+0

從哪裏可以知道當我真正發送消息時使用哪種信道類型? – greyBow

+0

connectionConfig是一個只讀屬性。目前這似乎不起作用。 – greyBow

+0

@greyBow文檔不會說它是隻讀的。我從來沒有使用它,所以我擁有的是文檔。 – Draco18s