Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Smart Fox Server With Unity3D

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 2.60k
    Comment on it

    Hello Readers !!


    Today we are going to discuss the Smart Fox Server in Unity 3D.

    We all need to put any server in the game if we are trying to create a Multiplayer game so that we can connect two real players together.

    Smart Fox server is also a good option as a server. It's quite simple and interesting. You just need to take care about some important points then it's ready to go. 

    Today I will discuss the connect, How you can make a connection with SFS through Unity.

    First of all, you need to run a server on any machine. You can also use your own machine too. This is to add the IP address in the connection.

    Once your service is running then you can follow below code to make a connection with server:-

    Declare following variables:-


    string serverName = "";
    int serverPort = 9933;
    string zone = "All";
    SmartFox smartFox;


    We will need these variables. You can set your data in these variables.

    Then in any Method just write down below code.

        void Start() 
                // In a webplayer (or editor in webplayer mode) we need to setup security policy negotiation with the server first
                if (Application.isWebPlayer) 
                {   bool a = Security.PrefetchSocketPolicy(serverName, serverPort, 500);
                    if (!a) 
                        Debug.LogError("Security Exception. Policy file load failed!");
                // Lets connect
                smartFox = new SmartFox(true);
                // Register callback delegate
                smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
                smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
                smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
                smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
                smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnRoomJoin);
                smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout);
                smartFox.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdd);
                smartFox.AddEventListener(SFSEvent.ROOM_CREATION_ERROR, OnRoomError);
                smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionRespose);
                smartFox.Connect(serverName, serverPort);


    After that you need to add following code so that you can get events of SFS.

    public void OnConnection(BaseEvent evt)
            bool success = (bool)evt.Params["success"];
            string error = (string)evt.Params["errorMessage"];
            Debug.Log("On Connection callback got: " + success + " (error : <" + error + ">)");
            if (success) {
                SmartFoxConnection.Connection = smartFox;
                Debug.Log("Connection succesful!");
            else {
                Debug.Log("Can't connect to server!");


    In the above code, you will get the events of the connection. If you are connected or not.

    So it may possible that you will get "Can't connect to server" this is because of the event processing.

    So you need to add the event processing in the code.


    void FixedUpdate() {
            if (smartFox != null) {


    Event processing always works in the update function so that SFS can always communicate.

    Try this code and make a connection with SFS.

    If you have a query you can ask from me also.

    Till then keep coding..

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: