Connecting to a server

To connect to a server, a client application is required to request an identity from the Client Lib. This string should be requested only once and then locally stored in the applications configuration. The next time the application connects to a server, the identity should be read from the configuration and reused again.

unsigned int ts3client_createIdentity(result); 
char** result;
 

Returns ERROR_ok on success, otherwise an error code as defined in public_errors.h. If an error occured, the result string is uninitialized and must not be accessed.

[Caution]Caution

The result string must be released using ts3client_freeMemory. If an error has occured, the result string is uninitialized and must not be released.


Once a server connection handler has been spawned and an identity is available, connect to a TeamSpeak 3 server with

unsigned int ts3client_startConnection(serverConnectionHandlerID,  
 identity,  
 ip,  
 port,  
 nickname,  
 defaultChannelArray,  
 defaultChannelPassword,  
 serverPassword); 
uint64 serverConnectionHandlerID;
const char* identity;
const char* ip;
unsigned int port;
const char* nickname;
const char** defaultChannelArray;
const char* defaultChannelPassword;
const char* serverPassword;
 

All strings need to be encoded in UTF-8 format.

Returns ERROR_ok on success, otherwise an error code as defined in public_errors.h. When trying to connect with an invalid identity, the Client Lib will set the error ERROR_client_could_not_validate_identity.


There is an alternative convinience function to start the connection which takes a channelID as parameter for the default channel instead of a channel name string array.

unsigned int ts3client_startConnectionWithChannelID(serverConnectionHandlerID,  
 identity,  
 ip,  
 port,  
 nickname,  
 defaultChannelId,  
 defaultChannelPassword,  
 serverPassword); 
uint64 serverConnectionHandlerID;
const char* identity;
const char* ip;
unsigned int port;
const char* nickname;
uint64 defaultChannelId;
const char* defaultChannelPassword;
const char* serverPassword;
 


  

Example code to request a connection to a TeamSpeak 3 server:

unsigned int error;
uint64 scHandlerID;
char* identity;

error = ts3client_spawnNewServerConnectionHandler(&scHandlerID);
if(error != ERROR_ok) {
    printf("Error spawning server conection handler: %d\n", error);
    return;
}

error = ts3client_createIdentity(&identity);  /* Application should store and reuse the identity */
if(error != ERROR_ok) {
    printf("Error creating identity: %d\n", error);
    return;
}

error = ts3client_startConnection(scHandlerID,
                                  identity
                                  "my-teamspeak-server.com",
				  9987,
				  "Gandalf",
				  NULL,      // Join servers default channel
				  "",        // Empty default channel password
				  "secret"); // Server password
if(error != ERROR_ok) {
    (...)
}			  
ts3client_freeMemory(identity);  /* Don't need this anymore */


After calling ts3client_startConnection, the client will be informed of the connection status changes by the callback

void onConnectStatusChangeEvent(serverConnectionHandlerID,  
 newStatus,  
 errorNumber); 
uint64 serverConnectionHandlerID;
int newStatus;
int errorNumber;
 

While connecting, the states will switch through the values STATUS_CONNECTING, STATUS_CONNECTED and STATUS_CONNECTION_ESTABLISHED. Once the state STATUS_CONNECTED has been reached, there the server welcome message is available, which can be queried by the client:


To check if a connection to a given server connection handler is established, call:

unsigned int ts3client_getConnectionStatus(serverConnectionHandlerID,  
 result); 
uint64 serverConnectionHandlerID;
int* result;
 

Returns ERROR_ok on success, otherwise an error code as defined in public_errors.h.


After the state STATUS_CONNECTED has been reached, the client is assigned an ID which identifies the client on this server. This ID can be queried with

unsigned int ts3client_getClientID(serverConnectionHandlerID,  
 result); 
uint64 serverConnectionHandlerID;
anyID* result;
 

Returns ERROR_ok on success, otherwise an error code as defined in public_errors.h.


After connection has been established, all current channels on the server are announced to the client. This happens with delays to avoid a flood of information after connecting. The client is informed about the existance of each channel with the following event:

void onNewChannelEvent(serverConnectionHandlerID,  
 channelID,  
 channelParentID); 
uint64 serverConnectionHandlerID;
uint64 channelID;
uint64 channelParentID;
 

Channel IDs start with the value 1.

The order in which channels are announced by onNewChannelEvent is defined by the channel order as explained in the chapter Channel sorting.

All clients currently logged to the server are announced after connecting with the callback onClientMoveEvent.