Select your preferred scripting language. All code snippets will be displayed in this language.
class in UnityEngine.Networking
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.
CloseFor 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.
CloseA High level network connection. This is used for connections from client-to-server and for connection from server-to-client.
A NetworkConnection corresponds to a specific connection for a host in the transport layer. It has a connectionId that is assigned by the transport layer and passed to the Initialize function.
A NetworkClient has one NetworkConnection. A NetworkServerSimple manages multiple NetworkConnections. The NetworkServer has multiple "remote" connections and a "local" connection for the local client.
The NetworkConnection class provides message sending and handling facilities. For sending data over a network, there are methods to send message objects, byte arrays, and NetworkWriter objects. To handle data arriving from the network, handler functions can be registered for message Ids, byte arrays can be processed by HandleBytes(), and NetworkReader object can be processed by HandleReader().
NetworkConnection objects also act as observers for networked objects. When a connection is an observer of a networked object with a NetworkIdentity, then the object will be visible to corresponding client for the connection, and incremental state changes will be sent to the client.
NetworkConnection objects can "own" networked game objects. Owned objects will be destroyed on the server by default when the connection is destroyed. A connection owns the player objects created by its client, and other objects with client-authority assigned to the corresponding client.
There are many virtual functions on NetworkConnection that allow its behaviour to be customized. NetworkClient and NetworkServer can both be made to instantiate custom classes derived from NetworkConnection by setting their networkConnectionClass member variable.
address | The IP address associated with the connection. |
clientOwnedObjects | A list of the NetworkIdentity objects owned by this connection. |
connectionId | Unique identifier for this connection that is assigned by the transport layer. |
hostId | Transport level host ID for this connection. |
isConnected | True if the connection is connected to a remote end-point. |
isReady | Flag that tells if the connection has been marked as "ready" by a client calling ClientScene.Ready(). |
lastError | The last error associated with this connection. |
lastMessageTime | The last time that a message was received on this connection. |
logNetworkMessages | Setting this to true will log the contents of network message to the console. |
playerControllers | The list of players for this connection. |
CheckHandler | This function checks if there is a message handler registered for the message ID. |
Disconnect | Disconnects this connection. |
Dispose | Disposes of this connection, releasing channel buffers that it holds. |
FlushChannels | This causes the channels of the network connection to flush their data to the transport layer. |
GetStatsIn | Get statistics for incoming traffic. |
GetStatsOut | Get statistics for outgoing traffic. |
Initialize | This inializes the internal data structures of a NetworkConnection object, including channel buffers. |
InvokeHandler | This function invokes the registered handler function for a message. |
InvokeHandlerNoData | This function invokes the registered handler function for a message, without any message data. |
RegisterHandler | This registers a handler function for a message Id. |
ResetStats | Resets the statistics that are returned from NetworkClient.GetConnectionStats(). |
Send | This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel. |
SendByChannel | This sends a network message on the connection using a specific transport layer channel. |
SendBytes | This sends an array of bytes on the connection. |
SendUnreliable | This sends a network message with a message ID on the connection. This message is sent on channel one, which by default is the unreliable channel. |
SendWriter | This sends the contents of a NetworkWriter object on the connection. |
SetChannelOption | This sets an option on the network channel. |
SetMaxDelay | The maximum time in seconds that messages are buffered before being sent. |
ToString | Returns a string representation of the NetworkConnection object state. |
TransportReceive | This virtual function allows custom network connection classes to process data from the network before it is passed to the application. |
TransportSend | This virtual function allows custom network connection classes to process data send by the application before it goes to the network transport layer. |
UnregisterHandler | This removes the handler registered for a message Id. |
HandleBytes | This makes the connection process the data contained in the buffer, and call handler functions. |
HandleReader | This makes the connection process the data contained in the stream, and call handler functions. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!