Streaming Bytes

When a large amount of data and/or type of data that can't be sent with Bolt properties (such as images, dictionaries) needs to be sent over the network, streaming is invaluable.

First you will need to set the max amount of data streamed for second.

public override void Connected(BoltConnection connection) {
  connection.SetStreamBandwidth(1024 * 20);
}

When placed in your server callbacks, this configures the initial stream traffic of new connections to 20 kb/s.

You will then want to create binary streaming channels on the client and the host.

 public static UdpKit.UdpChannelName Voice;
 public static UdpKit.UdpChannelName CustomPlayerIcon;

    public override void BoltStartBegin()
    {
        Voice = BoltNetwork.CreateStreamChannel("Voice", UdpKit.UdpChannelMode.Unreliable, 1});
          CustomPlayerIcon = BoltNetwork.CreateStreamChannel("CustomPlayerIcon", UdpKit.UdpChannelMode.Reliable, 4});
    }

The first parameter names the channel, the second makes the channel reliable or unreliable and the third marks the priority.

Then you can send a binary stream of data to this connection. Here is an example of sending the binary data of a custom icon texture to the server using a static reference to the "PlayerIcon" channel that was created inside a Channels class.

void SendCustomIcon(Texture2D myCustomIcon) {
  byte[] data = myCustomIcon.EncodeToPNG();

  //Streaming bytes in stream channel created in script "Channels"
  BoltNetwork.server.StreamBytes(Channels.PlayerIcon, data);
}

Then you will need a callback for when the binary stream data is received.

public override void StreamDataReceived(BoltConnection connnection, UdpStreamData data) {
  Texture2D icon = new Texture2D(4, 4);
  icon.LoadImage(data.Data);

  PlayerData playerData = (PlayerData)connection.userToken;
  playerData.SetIcon(icon);
}

Then you will be able to do whatever you want with the data after receiving it. Now let's put all these steps together to make a simple example.

using UnityEngine;
using System.Collections;
using System.Text;
using UdpKit;
using Bolt;

[BoltGlobalBehaviour()]
public class StreamCallbacks : Bolt.GlobalEventListener
{
    public static UdpKit.UdpChannelName testChannel;


    public override void BoltStartBegin()
    {
        testChannel = BoltNetwork.CreateStreamChannel("test", UdpKit.UdpChannelMode.Reliable, 1);
    }


    public override void SceneLoadRemoteDone(BoltConnection c)
    {
        if (BoltNetwork.isServer)
        {
            byte[] data = Encoding.ASCII.GetBytes("Testing");
            c.StreamBytes(testChannel, data);
        }
    }

    public override void StreamDataReceived(BoltConnection connection, UdpStreamData data)
    {
        BoltLog.Info(data);
    }

}

Be careful with how much data you stream, as there is a limit to how much you can.

 To Document Top