help with a custom extension

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

alucidworld
Posts: 2
Joined: 26 May 2010, 04:18

help with a custom extension

Postby alucidworld » 26 May 2010, 04:23

I'm trying to setup my own extension in my own room on a smartfox server. I am then trying to make a simple call to the extension, like your basic hello world system.

Unity code :

Code: Select all

using UnityEngine;
using System;
using System.Collections;
using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;
using SmartFoxClientAPI.Util;

public class LoginGUI : MonoBehaviour {
   private SmartFoxClient smartFox;
   //private bool shuttingDown = false;

   private string serverIP = "127.0.0.1";
   private string serverPort = "9339";
   public string zone = "alien8";
   public bool debug = true;

   void Awake() {
      Application.runInBackground = true;

      if ( SmartFox.IsInitialized() ) {
         smartFox = SmartFox.Connection;
      } else {
         try {
            smartFox = new SmartFoxClient(debug);
            smartFox.runInQueueMode = true;
         } catch ( Exception e ) {
            //loginErrorMessage = e.ToString();
         }
      }

      // Register callback delegate
      SFSEvent.onConnection += OnConnection;
      SFSEvent.onConnectionLost += OnConnectionLost;
      SFSEvent.onRoomListUpdate += OnRoomList;
      SFSEvent.onDebugMessage += OnDebugMessage;

   }
   
   void FixedUpdate() {
      smartFox.ProcessEventQueue();
   }

   void OnGUI() {


      smartFox.Connect(serverIP, Convert.ToInt32(serverPort));
      if (smartFox.IsConnected()) {
         smartFox.SendXtMessage("alien8", "loginRequest", null, SmartFoxClient.XTMSG_TYPE_STR);

      } else {
         GUI.Label(new Rect(10, 150, 100, 100), "Waiting for connection");
         //GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
      }
   }
   
   public void OnExtensionResponse(object data, string type) {
      Debug.Log("hi");
   }

   /************
    * Helper methods
    ************/

   private void UnregisterSFSSceneCallbacks() {
      // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
      SFSEvent.onConnection -= OnConnection;
      SFSEvent.onConnectionLost -= OnConnectionLost;
      SFSEvent.onRoomListUpdate -= OnRoomList;
      SFSEvent.onDebugMessage -= OnDebugMessage;
   }

   /************
    * Callbacks from the SFS API
    ************/

   void OnConnection(bool success, string error) {
      if ( success ) {
         SmartFox.Connection = smartFox;
      } else {
         //loginErrorMessage = error;
      }
   }

   void OnConnectionLost() {
      //loginErrorMessage = "Connection lost / no connection to server";
   }

   public void OnDebugMessage(string message) {
      Debug.Log("[SFS DEBUG] " + message);
   }

   void OnRoomList(Hashtable roomList) {
      // When room list is updated we are ready to move on to the island
      UnregisterSFSSceneCallbacks();
      Debug.Log("go to main screen");
   }
}


SFS Extension :

Code: Select all

function init()
{
   
}

function destroy()
{
   trace("Extension destroyed")
}

function handleRequest(cmd, params, user, fromRoom)
{
   
   var response = new Object()
   response._cmd = "logOK"
   _server.sendResponse(response, -1, null, chan)
}

function handleInternalEvent(evt)
{
   


Can anyone help me out with this. I just want to build a simple hello world setup using my custom .as file as an extension.
lastowl
Posts: 36
Joined: 12 Aug 2010, 20:56

Postby lastowl » 28 Aug 2010, 16:18

I've had similar problems, so i'm also quite interested if someone can help
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 29 Aug 2010, 07:28

I'm unsure what exact problem you have without any error messages or "this part doesnt work" indications.

But I did spot at least one issue in your code:

Code: Select all

void OnGUI() {
      smartFox.Connect(serverIP, Convert.ToInt32(serverPort));


OnGUI is called several times per frame - its definitely totally the wrong place to connect to the server. Move it to Awake() or Start() instead.

Genrally most basic extension questions + examples are answered in the documentation already. Take a look here:

http://www.smartfoxserver.com/docs/inde ... ension.htm

/Thomas
spencer12
Posts: 3
Joined: 04 Sep 2010, 10:05

Postby spencer12 » 04 Sep 2010, 10:28

Yes, it is the perfect solution and I tried it. I got the solution because I was also searching that kind of solution. Really, it is right.

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 15 guests