Click or drag to resize

SmartFoxConnect Method (String, Int32)

Establishes a connection between the client and a SmartFoxServer 2X instance.

Namespace:  Sfs2X
Assembly:  SmartFox2X (in SmartFox2X.dll) Version: 1.8.0.0 (1.8.0)
Syntax
C#
public void Connect(
	string host,
	int port
)

Parameters

host
Type: SystemString
The address of the server to connect to.
port
Type: SystemInt32
The TCP port to connect to.
Exceptions
ExceptionCondition
ArgumentExceptionIf an invalid host/address or port is passed, and it can't be found in the loaded settings.
Remarks
If no argument is passed, the client will use the settings loaded via LoadConfig(String, Boolean) method.

The client usually connects to a SmartFoxServer instance through a socket connection. In case a socket connection can't be established, and the UseBlueBox property is set to true, a tunnelled http connection through the BlueBox module is attempted as a fail-safe system. When a successful connection is established, the ConnectionMode property can be used to check the current connection mode.

Tunnelled http connection is not available in case of websocket connection.

When using a websocket connection to an IPv6 address, always wrap the host value in square brackets.

Examples
The following example connects to a local SmartFoxServer 2X instance:
void SomeMethod() {
    SmartFox sfs = new SmartFox();

    sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
    sfs.Connect("127.0.0.1", 9933);
}

void OnConnection(BaseEvent evt) {
    if ((bool)evt.Params["success"])
    {
        Console.WriteLine("Connection was established");                         // .Net / Unity
        System.Diagnostics.Debug.WriteLine("Connection was established");        // UWP
    }
    else
    {
        Console.WriteLine("Connection failed");                         // .Net / Unity
        System.Diagnostics.Debug.WriteLine("Connection failed");        // UWP
    }
}
See Also