|
Understanding Connections |
|
Before you read
Overview
In order for a client to communicate with the server a connection must be established. There are multiple ways to tell the API to do this, and there are multiple types of connections. Below we explain it all.
Host, port, and transport types
Before a client can connect to the server the server must be listening for connections on a specific host and port. A host is a DNS or ip - such as "electrotank.com" or "127.0.0.1". The port is a number that is chosen that no other application is using to keep data routed to the right place - such as 9899. The transport type is the type of connection and how it is used. Here are the supported transport types by ElectroServer and which client APIs support them.
AS2 |
AS3 |
Java |
Objective-C |
C# |
Javascript |
|
BinaryTCP |
X |
X |
X |
X |
||
HTTP |
X |
X |
X |
X |
X |
|
UDP |
X (AIR 2.0) |
X |
X |
X |
||
RTMP |
X |
|||||
TextTCP |
X |
Here they are described:
There are times when the client may want to establish more than one connection with the server. For example, for video chat. First the client would connect using BinaryTCP, find its way to a chat room, and then establish an RTMP connection for audio/video streaming.
Similarly, a client may want to have a BinaryTCP connection established in a first person shooter game, but also establish a UDP connection for rapid position updates.
It should be noted that some connection types can be standalone, which we call "primary", or are used only with a primary connection, which we call "secondary".
Primary capable |
Secondary capable |
|
BinaryTCP |
X |
X |
HTTP |
X |
X |
UDP |
X |
|
RTMP |
X |
|
TextTCP |
X |
X |
Connecting to ElectroServer
To connect to ElectroServer the client API must first be configured with one or more available connections to attempt. This can be done in two ways:
Both of those techniques will be shown below.
When the API is told to connect it starts with the first available connection in the list and tries to connect to it. If it fails, then it moves on to the next, and so on. When a connection it stops trying new ones. For each individual connection success or failure a ConnectionAttemptResponse event is fired. When a connection succeeds or when the all fail, a ConnectionResponse event is fired.
Connecting automatically
Here are the contents of a properly formatted settings file. This is file describes the host, port, and transport type for two available connections. The order matters. Firs the server will try to connect using BinaryTCP, if that fails it will move on to HTTP.
The value of the serverId parameter doesn't matter so long as you use the same value everywhere. There are some future features of ElectroServer that will use this, and so it was added now.
Using the settings.xml file is easy. Assuming that you already have an instance of the API called '_es', here is the line of code that will both load XML file and then immediately start trying to connect.
To capture the ConnectionResponse event add this line.
The onConnectionResponse takes a ConnectionResponse object.
Connecting manually
Connecting manually is possible as well. To do this you first create an instance of the Server class. Then you add instances of AvailableConnection to it. Then the server class is added to the API. And finally the API is simple told to connect.