Initializes the connection cryptography to protect all client-server communications with standard TLS protocol.

Namespace: Sfs2X
Assembly: SmartFox2X (in SmartFox2X.dll) Version: (1.7.0)


public IEnumerator InitCrypto()

Return Value

Type: IEnumerator


This method must be called right after a successful connection, before the login is performed.
Once the encryption initialization process is successfully completed, all of the server's data will be encrypted using standard AES 128-bit algorithm, with a secure key served over HTTPS.


In Unity this method must be executed as a coroutine, hence using MonoBehaviour.StartCoroutine method. For this reason, in case the Unity project is built for Windows Store (SDK 8.1, Phone 8.1, Universal 8.1), a conditional statement is required as in the example below.

This method is not available when building for WebGL: use WSS connection instead.

When building for the Web Player, do not use Security.PrefetchSocketPolicy in your code. In fact this method accepts an IP address only, while you should connect to the domain name instead, since the SSL certificate is (typically) bound to that. Let the Web Player auto-fetch the cross-domain policy from the default TCP port 843. In order to do this, add a listener for such port in the SFS2X AdminTool's Server Configurator module.


The following example initializes the encrypted communication:
 Copy imageCopy
void SomeMethod() {
    SmartFox sfs = new SmartFox();
    sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
    sfs.AddEventListener(SFSEvent.CRYPTO_INIT, OnEncryptionInitialized);
    sfs.Connect("", 9933);
void OnConnection(BaseEvent evt) {
    if ((bool)evt.Params["success"])
        Console.WriteLine("Connection was established");
        // Initialize encrypted connection
        Console.WriteLine("Connection failed");
void OnEncryptionInitialized(BaseEvent evt) {
    if ((bool)evt.Params["success"])
        // Do login
        sfs.Send( new LoginRequest("FozzieTheBear", "", "SimpleChat") );
        Console.WriteLine("Encryption initialization failed. Caused by: " + (string)evt.Params["errorMsg"]);

See Also