Class NetworkServerSimple
The NetworkServerSimple is a basic server class without the "game" related functionality that the NetworkServer class has.
This class has no scene management, spawning, player objects, observers, or static interface like the NetworkServer class. It is simply a server that listens on a port, manages connections, and handles messages. There can be more than one instance of this class in a process.
Like the NetworkServer and NetworkClient classes, it allows the type of NetworkConnection class created for new connections to be specified with SetNetworkConnectionClass(), so custom types of network connections can be used with it.
This class can be used by overriding the virtual functions OnConnected, OnDisconnected and OnData; or by registering message handlers.
Namespace: UnityEngine.Networking
Syntax
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkServerSimple
Constructors
NetworkServerSimple()
Declaration
public NetworkServerSimple()
Properties
connections
A read-only list of the current connections being managed.
Declaration
public ReadOnlyCollection<NetworkConnection> connections { get; }
Property Value
Type | Description |
---|---|
ReadOnlyCollection<NetworkConnection> |
handlers
The message handler functions that are registered.
Declaration
public Dictionary<short, NetworkMessageDelegate> handlers { get; }
Property Value
Type | Description |
---|---|
Dictionary<Int16, NetworkMessageDelegate> |
hostTopology
The transport layer host-topology that the server is configured with.
A host topology object can be passed to the Listen() function, or a default host topology that is compatible with the default topology of NetworkClient will be used.
Declaration
public HostTopology hostTopology { get; }
Property Value
Type | Description |
---|---|
HostTopology |
listenPort
The network port that the server is listening on.
Declaration
public int listenPort { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
messageBuffer
The internal buffer that the server reads data from the network into. This will contain the most recent data read from the network when OnData() is called.
Declaration
public byte[] messageBuffer { get; }
Property Value
Type | Description |
---|---|
Byte[] |
messageReader
A NetworkReader object that is bound to the server's messageBuffer.
Declaration
public NetworkReader messageReader { get; }
Property Value
Type | Description |
---|---|
NetworkReader |
networkConnectionClass
The type of class to be created for new network connections from clients.
By default this is the NetworkConnection class, but it can be changed with SetNetworkConnectionClass() to classes derived from NetworkConnections.
Declaration
public Type networkConnectionClass { get; }
Property Value
Type | Description |
---|---|
Type |
serverHostId
The transport layer hostId of the server.
Declaration
public int serverHostId { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
useWebSockets
This causes the server to listen for WebSocket connections instead of regular transport layer connections.
This allows WebGL clients to talk to the server.
Declaration
public bool useWebSockets { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
ClearHandlers()
Clears the message handlers that are registered.
Declaration
public void ClearHandlers()
Configure(ConnectionConfig, Int32)
This configures the network transport layer of the server.
Declaration
public bool Configure(ConnectionConfig config, int maxConnections)
Parameters
Type | Name | Description |
---|---|---|
ConnectionConfig | config | The transport layer configuration to use. |
Int32 | maxConnections | Maximum number of network connections to allow. |
Returns
Type | Description |
---|---|
Boolean | True if configured. |
Configure(HostTopology)
This configures the network transport layer of the server.
Declaration
public bool Configure(HostTopology topology)
Parameters
Type | Name | Description |
---|---|---|
HostTopology | topology | The transport layer host topology to use. |
Returns
Type | Description |
---|---|
Boolean | True if configured. |
Disconnect(Int32)
This disconnects the connection of the corresponding connection id.
Declaration
public void Disconnect(int connectionId)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | The id of the connection to disconnect. |
DisconnectAllConnections()
This disconnects all of the active connections.
Declaration
public void DisconnectAllConnections()
FindConnection(Int32)
This looks up the network connection object for the specified connection Id.
Declaration
public NetworkConnection FindConnection(int connectionId)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | The connection id to look up. |
Returns
Type | Description |
---|---|
NetworkConnection | A NetworkConnection objects, or null if no connection found. |
Initialize()
Initialization function that is invoked when the server starts listening. This can be overridden to perform custom initialization such as setting the NetworkConnectionClass.
Declaration
public virtual void Initialize()
Listen(Int32)
This starts the server listening for connections on the specified port.
Declaration
public bool Listen(int serverListenPort)
Parameters
Type | Name | Description |
---|---|---|
Int32 | serverListenPort | The port to listen on. |
Returns
Type | Description |
---|---|
Boolean |
Listen(Int32, HostTopology)
This starts the server listening for connections on the specified port.
Declaration
public bool Listen(int serverListenPort, HostTopology topology)
Parameters
Type | Name | Description |
---|---|---|
Int32 | serverListenPort | The port to listen on. |
HostTopology | topology | The transport layer host toplogy to configure with. |
Returns
Type | Description |
---|---|
Boolean |
Listen(String, Int32)
This starts the server listening for connections on the specified port.
Declaration
public bool Listen(string ipAddress, int serverListenPort)
Parameters
Type | Name | Description |
---|---|---|
String | ipAddress | |
Int32 | serverListenPort | The port to listen on. |
Returns
Type | Description |
---|---|
Boolean | True if able to listen. |
ListenRelay(String, Int32, NetworkID, SourceID, NodeID)
Starts a server using a Relay server. This is the manual way of using the Relay server, as the regular NetworkServer.Connect() will automatically use the Relay server if a match exists.
Declaration
public void ListenRelay(string relayIp, int relayPort, NetworkID netGuid, SourceID sourceId, NodeID nodeId)
Parameters
Type | Name | Description |
---|---|---|
String | relayIp | Relay server IP Address. |
Int32 | relayPort | Relay server port. |
NetworkID | netGuid | GUID of the network to create. |
SourceID | sourceId | This server's sourceId. |
NodeID | nodeId | The node to join the network with. |
OnConnected(NetworkConnection)
This virtual function can be overridden to perform custom functionality for new network connections.
By default OnConnected just invokes a connect event on the new connection.
using UnityEngine;
using UnityEngine.Networking;
public abstract class ExampleScript : NetworkManager
{
public virtual void OnConnected(NetworkConnection conn)
{
conn.InvokeHandlerNoData(MsgType.Connect);
}
}
Declaration
public virtual void OnConnected(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The new connection object. |
OnConnectError(Int32, Byte)
A virtual function that is invoked when there is a connection error.
Declaration
public virtual void OnConnectError(int connectionId, byte error)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | The id of the connection with the error. |
Byte | error | The error code. |
OnData(NetworkConnection, Int32, Int32)
This virtual function can be overridden to perform custom functionality when data is received for a connection.
By default this function calls HandleData() which will process the data and invoke message handlers for any messages that it finds.
using UnityEngine;
using UnityEngine.Networking;
public abstract class ExampleScript : NetworkManager
{
byte[] msgBuffer = new byte[1024];
public virtual void OnData(NetworkConnection conn, int channelId, int receivedSize)
{
conn.TransportRecieve(msgBuffer, receivedSize, channelId);
}
}
Declaration
public virtual void OnData(NetworkConnection conn, int receivedSize, int channelId)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | |
Int32 | receivedSize | |
Int32 | channelId |
OnDataError(NetworkConnection, Byte)
A virtual function that is called when a data error occurs on a connection.
Declaration
public virtual void OnDataError(NetworkConnection conn, byte error)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection object that the error occured on. |
Byte | error | The error code. |
OnDisconnected(NetworkConnection)
This virtual function can be overridden to perform custom functionality for disconnected network connections.
By default OnConnected just invokes a disconnect event on the new connection.
using UnityEngine;
using UnityEngine.Networking;
public abstract class ExampleScript : NetworkManager
{
public virtual void OnDisconnected(NetworkConnection conn)
{
conn.InvokeHandlerNoData(MsgType.Disconnect);
}
}
Declaration
public virtual void OnDisconnected(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn |
OnDisconnectError(NetworkConnection, Byte)
A virtual function that is called when a disconnect error happens.
Declaration
public virtual void OnDisconnectError(NetworkConnection conn, byte error)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection object that the error occured on. |
Byte | error | The error code. |
OnError(Int32, Byte)
A virtual function that is called when an error happens outside of connection, data or disconnect.
Declaration
public virtual void OnError(int connectionId, byte error)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | |
Byte | error | The error code. |
RegisterHandler(Int16, NetworkMessageDelegate)
This registers a handler function for a message Id.
Declaration
public void RegisterHandler(short msgType, NetworkMessageDelegate handler)
Parameters
Type | Name | Description |
---|---|---|
Int16 | msgType | Message Id to register handler for. |
NetworkMessageDelegate | handler | Handler function. |
RemoveConnectionAtIndex(Int32)
This removes a connection object from the server's list of connections.
This is a low-level function that should not be used for regular connections. It is only safe to remove connections added with SetConnectionAtIndex() using this function.
Declaration
public bool RemoveConnectionAtIndex(int connectionId)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | The id of the connection to remove. |
Returns
Type | Description |
---|---|
Boolean | True if removed. |
SendBytesTo(Int32, Byte[], Int32, Int32)
This sends the data in an array of bytes to the connected client.
Declaration
public void SendBytesTo(int connectionId, byte[] bytes, int numBytes, int channelId)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | The id of the connection to send on. |
Byte[] | bytes | The data to send. |
Int32 | numBytes | The size of the data to send. |
Int32 | channelId | The channel to send the data on. |
SendWriterTo(Int32, NetworkWriter, Int32)
This sends the contents of a NetworkWriter object to the connected client.
Declaration
public void SendWriterTo(int connectionId, NetworkWriter writer, int channelId)
Parameters
Type | Name | Description |
---|---|---|
Int32 | connectionId | The id of the connection to send on. |
NetworkWriter | writer | The writer object to send. |
Int32 | channelId | The channel to send the data on. |
SetConnectionAtIndex(NetworkConnection)
This adds a connection created by external code to the server's list of connections, at the connection's connectionId index.
Connections are usually added automatically, this is a low-level function for the rare special case of externally created connections.
Declaration
public bool SetConnectionAtIndex(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | A new connection object. |
Returns
Type | Description |
---|---|
Boolean | True if added. |
SetNetworkConnectionClass<T>()
This sets the class that is used when creating new network connections.
The class must be derived from NetworkConnection.
Declaration
public void SetNetworkConnectionClass<T>()
where T : NetworkConnection
Type Parameters
Name | Description |
---|---|
T |
Stop()
This stops a server from listening.
Declaration
public void Stop()
UnregisterHandler(Int16)
This unregisters a registered message handler function.
Declaration
public void UnregisterHandler(short msgType)
Parameters
Type | Name | Description |
---|---|---|
Int16 | msgType | The message id to unregister. |
Update()
This function pumps the server causing incoming network data to be processed, and pending outgoing data to be sent.
This should be called each frame, and is called automatically for the server used by NetworkServer.
Declaration
public void Update()
UpdateConnections()
This function causes pending outgoing data on connections to be sent, but unlike Update() it works when the server is not listening.
When the server is using externally added connections and the dontListen flag is set, the regular connection flush in the Update() function does not happen. In this case, UpdateConnections can be called to pump the external connections. This is an advanced usage that should not be required unless the server uses custom NetworkConnection classes that do not use the built-in transport layer.
Declaration
public void UpdateConnections()