Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

NetworkConnection.TransportSend

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function TransportSend(bytes: byte[], numBytes: int, channelId: int, out error: byte): bool;
public bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error);

Parameters

bytes Data to send.
numBytes Size of data to send.
channelId Channel to send data on.
error Error code for send.

Returns

bool True if data was sent.

Description

This virtual function allows custom network connection classes to process data send by the application before it goes to the network transport layer.

The default implementation of this function calls NetworkTransport.Send() with the supplied data, but custom implementations can pass modified versions of the data. This example logs the sent data to the console:

no example available in JavaScript
class DebugConnection : NetworkConnection
{
	public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
	{
    StringBuilder msg = new StringBuilder();
    for (int i = 0; i < numBytes; i++)
    {
      var s = String.Format("{0:X2}", bytes[i]);
      msg.Append(s);
			if (i > 50) break;
		}
		UnityEngine.Debug.LogError("TransportSend    h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);

return NetworkTransport.Send(hostId, connectionId, channelId, bytes, numBytes, out error); } }

Other uses for this function could be data compression or data encryption.

Custom network connection classes are used by setting NetworkServer.NetworkConnectionClass and NetworkClient.NetworkConnectionClass.

no example available in JavaScript
public class SpaceManager : NetworkManager 
{

void Start() { NetworkServer.NetworkConnectionClass = typeof(DebugConnection); NetworkClient.NetworkConnectionClass = typeof(DebugConnection); } }