2017-10-18 237 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