Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

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

NetworkServerSimple

class in UnityEngine.Networking

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

Description

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.

Properties

connectionsA read-only list of the current connections being managed.
handlersThe message handler functions that are registered.
hostTopologyThe transport layer host-topology that the server is configured with.
listenPortThe network port that the server is listening on.
messageBufferThe 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.
messageReaderA NetworkReader object that is bound to the server's messageBuffer.
networkConnectionClassThe type of class to be created for new network connections from clients.
serverHostIdThe transport layer hostId of the server.
useWebSocketsThis causes the server to listen for WebSocket connections instead of regular transport layer connections.

Public Methods

ClearHandlersClears the message handlers that are registered.
ConfigureThis configures the network transport layer of the server.
DisconnectThis disconnects the connection of the corresponding connection id.
DisconnectAllConnectionsThis disconnects all of the active connections.
FindConnectionThis looks up the network connection object for the specified connection Id.
InitializeInitialization function that is invoked when the server starts listening. This can be overridden to perform custom initialization such as setting the NetworkConnectionClass.
ListenThis starts the server listening for connections on the specified port.
ListenRelayStarts 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.
OnConnectedThis virtual function can be overridden to perform custom functionality for new network connections.
OnConnectErrorA virtual function that is invoked when there is a connection error.
OnDataThis virtual function can be overridden to perform custom functionality when data is received for a connection.
OnDataErrorA virtual function that is called when a data error occurs on a connection.
OnDisconnectedThis virtual function can be overridden to perform custom functionality for disconnected network connections.
OnDisconnectErrorA virtual function that is called when a disconnect error happens.
RegisterHandlerThis registers a handler function for a message Id.
RemoveConnectionAtIndexThis removes a connection object from the server's list of connections.
SendBytesToThis sends the data in an array of bytes to the connected client.
SendWriterToThis sends the contents of a NetworkWriter object to the connected client.
SetConnectionAtIndexThis adds a connection created by external code to the server's list of connections, at the connection's connectionId index.
SetNetworkConnectionClassThis sets the class that is used when creating new network connections.
StopThis stops a server from listening.
UnregisterHandlerThis unregisters a registered message handler function.
UpdateThis function pumps the server causing incoming network data to be processed, and pending outgoing data to be sent.
UpdateConnectionsThis function causes pending outgoing data on connections to be sent, but unlike Update() it works when the server is not listening.