Extension not working

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Birch
Posts: 15
Joined: 06 Jun 2012, 07:42
Location: Sweden

Extension not working

Postby Birch » 16 Jun 2012, 11:59

Hello!

I have just started to learn SmartFoxServer 2x and i'm currently trying to create a working server side extension. It is a very simple one, all it does is to create a request handler to deal with messages sent from unity and then send it back. Obviously, i can't make it work. I manage to connect to the server, login and join a zone and a room. I join a zone called test and the room "The Lobby". In "The Lobby" room i am using testExtension as my main class. When i try to send a message to the server the runtime statistics confirm that it is indeed receiving some incoming traffic. But after that, nothing happens. I've tried remote debugging my extension but i've ran into a lot of problems there as well and posted a topic here at the forums about it, but so far no success. I really hope you can help me as i am very eager to start working with SFS2X!


I know it's a pain looking through all this code, but i'll try to make it as easy as possible and not only include all the code, but also highlights of the most important parts.

The server side extensions

testExtension.java

Code: Select all

package my.test;

import com.smartfoxserver.v2.extensions.SFSExtension;

public class testExtension extends SFSExtension {
   
   public void init(){
      
      addRequestHandler("test", testHandler.class);
      
   }

}


testHandler.java

Code: Select all

package my.test;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;


public class testHandler extends BaseClientRequestHandler{

   @Override
   public void handleClientRequest(User user, ISFSObject sfsObject) {
      
      int number = sfsObject.getInt("test");
      
      ISFSObject data = new SFSObject();
      
      data.putInt("number", number * 2);
      
      this.send("test", data, this.getParentExtension().getParentRoom().getPlayersList(),false);
      
   }

}


Client (Unity)

Code: Select all

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Requests;
using Sfs2X.Logging;

using Sfs2X.Entities.Data;

public class SmartFoxTesting : MonoBehaviour {
   
   private SmartFox smartFox;
   
   private string ipAdress;
   
   private float btnX, btnY, btnW, btnH, txtFldH;
   
   bool loggedIn;
   
   void FixedUpdate() {
      smartFox.ProcessEvents();
   }

   // Use this for initialization
   void Awake () {
      
      loggedIn = false;
      
      Application.runInBackground = true;

      smartFox = new SmartFox(false);
      
      smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
      smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
      smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
      smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
      smartFox.AddEventListener(SFSEvent.ROOM_CREATION_ERROR, OnCreateRoomError);
      smartFox.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdd);
      smartFox.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError);
      smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);

      btnX = Screen.width * 0.05f;
      btnY = Screen.width * 0.05f;
      btnW = Screen.width * 0.075f;
      btnH = Screen.width * 0.025f;
      
      txtFldH = 20;
      
      ipAdress = "192.168.1.3";
   
   }
   
   // Update is called once per frame
   void Update () {
      

   }
   public void OnExtensionResponse(BaseEvent evt){
         
      if(evt.Params["cmd"] == "test"){
         
         Debug.Log("Succes!" + evt.Params["number"]);
         
      }
      
      Debug.Log("Nice");
         
   }
   public void OnRoomJoinError(BaseEvent evt){
      
      Debug.Log("Error joining room " + evt.Params["error"]);
      
   }   
   public void OnConnection(BaseEvent evt){
      
      Debug.Log("Connected to " + ipAdress);
      
   }
   public void OnConnectionLost(BaseEvent evt){
      
      Debug.Log("Disconnected");
      loggedIn = false;
      
   }
   public void OnLogin(BaseEvent evt){
      
      Debug.Log("Logged into " + evt.Params["zone"]);
      loggedIn = true;
      
      smartFox.Send(new JoinRoomRequest("The Lobby"));
      
   }
   public void OnJoinRoom(BaseEvent evt){
      
      Debug.Log("Joined " + evt.Params["room"]);
      
   }
   public void OnCreateRoomError(BaseEvent evt){
      
      Debug.Log("Error creating room: " + (string)evt.Params["error"]);
      
   }
   public void OnRoomAdd(BaseEvent evt){
      string error = (string)evt.Params["error"];
      Debug.Log("Added room: " + error);
      
   }
   
   void OnGUI(){
      
      if(!smartFox.IsConnected){
      
         ipAdress = GUI.TextField(new Rect(btnX, btnY, btnW, txtFldH), ipAdress);
         
         if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH),"Connect")){
            
            smartFox.Connect(ipAdress, 9933);   
            
         }
         if(GUI.Button(new Rect(btnX, btnY * 1.2f + 2 * btnH, btnW, btnH),"Disconnect")){
            
            smartFox.Disconnect();   
            
         }
      }
      else if(!loggedIn){
         
         if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH),"Login")){
            
            smartFox.Send ( new LoginRequest("Birch", "", "test"));
            
         }         
         if(GUI.Button(new Rect(btnX, btnY * 1.2f + 2 * btnH, btnW, btnH),"Disconnect")){
            
            smartFox.Disconnect();   
            
         }         
      }
      else if(!(smartFox.LastJoinedRoom == smartFox.GetRoomByName("The Lobby"))){
         
         if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH),"New Game")){
            
            RoomSettings roomSettings = new RoomSettings("game");
            roomSettings.GroupId = "default";
            roomSettings.MaxUsers = 10;
            roomSettings.IsGame = true;
            roomSettings.MaxSpectators = 0;
            
            smartFox.Send(new CreateRoomRequest(roomSettings,true,smartFox.LastJoinedRoom));
            
         }
         
      }
      else{
         
         if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH), "Send Message")){
            
            ISFSObject data = new SFSObject();
            
            data.PutInt("test", 1);
            
            ExtensionRequest request = new ExtensionRequest("test",data,smartFox.LastJoinedRoom, false);
            
            smartFox.Send(request);
            
         }
         
      }
   }
   
}





Important parts:

Sending the message

Code: Select all

ISFSObject data = new SFSObject();
            
data.PutInt("test", 1);
            
ExtensionRequest request = new ExtensionRequest("test",data,smartFox.LastJoinedRoom, false);
            
smartFox.Send(request);


Receiving the message

Code: Select all

public class testExtension extends SFSExtension {
   
   public void init(){
      
      addRequestHandler("test", testHandler.class);
      
   }

}


Dealing with and returning the message

Code: Select all

public class testHandler extends BaseClientRequestHandler{

   @Override
   public void handleClientRequest(User user, ISFSObject sfsObject) {
      
      int number = sfsObject.getInt("test");
      
      ISFSObject data = new SFSObject();
      
      data.putInt("number", number * 2);
      
      this.send("test", data, this.getParentExtension().getParentRoom().getPlayersList(),false);
      
   }

}


Receiving the message in Unity

Code: Select all

smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);

.
.
.

public void OnExtensionResponse(BaseEvent evt){
         
   if(evt.Params["cmd"] == "test"){
         
      Debug.Log("Succes!" + evt.Params["number"]);
         
   }
      
   Debug.Log("Hmmm...");
         
}
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Extension not working

Postby rjgtav » 16 Jun 2012, 17:00

Hello.
Is your extension attached to the Zone or to the Room?
If it is attached to the Zone, you must not specify any room object when sending the extension request, so you will have to use a code like:

ExtensionRequest request = new ExtensionRequest("test",data,null, false);
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Re: Extension not working

Postby ThomasLund » 19 Jun 2012, 09:13

Yeah - nothing otherwise obviously wrong from just looking at the code.

Otherwise try to grab the FPS example and work from there - removing a lot of things start adding your code. But otherwise as said - looks sane enough.

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
Birch
Posts: 15
Joined: 06 Jun 2012, 07:42
Location: Sweden

Re: Extension not working

Postby Birch » 21 Jun 2012, 17:31

I've attached it to the room. Thanks for all the replies. I guess i'll have to try and figure this one out on my own! :)
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Extension not working

Postby rjgtav » 22 Jun 2012, 09:06

Does the server console show any error/exception?
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Birch
Posts: 15
Joined: 06 Jun 2012, 07:42
Location: Sweden

Re: Extension not working

Postby Birch » 22 Jun 2012, 10:08

Nope, i only get this when i login:
[com.smartfoxserver.v2.controllers.SystemController-1] api.SFSApi - Login in, { Zone: test }, ( User Name: Birch, Id: 2, Priv: 0, Sess: 192.168.1.3 )

I've also confirmed that i am in the correct room and zone with the zone monitoring in the Admin Tools.

And here is the entire console:

Code: Select all

11:54:38,832 INFO  [main] v2.SmartFoxServer     - Boot sequence starts...
11:54:38,876 INFO  [main] v2.SmartFoxServer     -
 _____ _____ _____    ___ __ __    _____ _____ _____ _____
|   __|   __|   __|  |_  |  |  |  | __  |     |     |_   _|
|__   |   __|__   |  |  _|-   -|  | __ -|  |  |  |  | | | 
|_____|__|  |_____|  |___|__|__|  |_____|_____|_____| |_|

11:54:39,846 INFO  [main] v2.SmartFoxServer     - License code not found, starting Community Edition license
11:54:39,846 INFO  [main] core.SFSEventManager     - AnonymousService-1 initalized
11:54:39,867 INFO  [main] impl.DefaultFileReplicator     - Using "/var/folders/lH/lHA6-HzAHPaTIdOcxMvG1U+++TI/-Tmp-/vfs_cache" as temporary files store.
11:54:40,010 INFO  [main] v2.SmartFoxServer     - License loaded:

===================================
LICENSE DETAILS
-----------------------------------
Type            : Community Edition
Max users       : 100
===================================

11:54:40,011 INFO  [main] managers.SFSBannedUserStorage     - BanUserStorage initialized
11:54:40,046 INFO  [main] managers.SFSBannedUserManager     - BanUser data loaded: 0 records.
11:54:40,068 INFO  [main] config.SFSConfigurator     - Loading: zones/BasicExamples.zone.xml
11:54:40,106 INFO  [main] config.SFSConfigurator     - Loading: zones/test.zone.xml
11:54:40,144 INFO  [main] managers.SFSZoneManager     -

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 >> Zone: BasicExamples
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

11:54:40,167 INFO  [main] managers.SFSZoneManager     - Creating room: (default) The Lobby
11:54:40,195 INFO  [main] api.SFSApi     - Room created: [ Room: The Lobby, Id: 0, Group: default, isGame: false ]
11:54:40,196 INFO  [main] managers.SFSZoneManager     -

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 >> Zone: test
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

11:54:40,197 INFO  [main] managers.SFSZoneManager     - Creating room: (default) The Lobby
11:54:40,197 INFO  [main] api.SFSApi     - Room created: [ Room: The Lobby, Id: 1, Group: default, isGame: false ]
11:54:40,226 INFO  [main] api.SFSApi     - Room created: [ Room: AdminRoom, Id: 2, Group: default, isGame: false ]
11:54:40,236 INFO  [main] core.AdminToolService     - AdminTool Service started
11:54:41,519 INFO  [main] http.SFSHttpServer     - Http Server started.
11:54:41,557 WARN  [main] bootLogger     - Was not able to bind socket: { 192.168.1.4:9933, (Tcp) }
11:54:41,557 WARN  [main] bootLogger     - Was not able to bind socket: { 192.168.1.4:9933, (Udp) }
11:54:41,563 INFO  [main] v2.SmartFoxServer     - Listening Sockets: { 127.0.0.1:9933, (Tcp) } { 127.0.0.1:9933, (Udp) } { 127.0.0.1:8787, (Tcp) } { 127.0.0.1:8787, (Udp) }
11:54:41,563 INFO  [main] v2.SmartFoxServer     - 
 _____ _____ _____    ___ __ __
|   __|   __|   __|  |_  |  |  |
|__   |   __|__   |  |  _|-   -|
|_____|__|  |_____|  |___|__|__|                                         
 _____ _____ _____ ____  __ __
| __  |   __|  _  |    \|  |  |
|    -|   __|     |  |  |_   _|
|__|__|_____|__|__|____/  |_| 
[ 2.0.1 ]

11:54:41,564 INFO  [main] v2.SmartFoxServer     - SmartFoxServer 2X (2.0.1) READY!
11:55:25,293 INFO  [com.smartfoxserver.v2.controllers.SystemController-1] api.SFSApi     - Login in, { Zone: test }, ( User Name: Birch, Id: 0, Priv: 0, Sess: 192.168.1.3 )
11:56:15,643 INFO  [SocketReader] core.SocketAcceptor     - Session created: { Id: 2, Type: DEFAULT, Logged: No, IP: 127.0.0.1:51737 } on Server port: 9933 <---> 51737
11:56:15,726 INFO  [SocketReader] sessions.DefaultSessionManager     - Session removed: { Id: 2, Type: DEFAULT, Logged: No, IP: 127.0.0.1:51737 }
11:56:15,777 INFO  [SocketReader] core.SocketAcceptor     - Session created: { Id: 3, Type: DEFAULT, Logged: No, IP: 127.0.0.1:51738 } on Server port: 9933 <---> 51738
11:56:15,944 INFO  [pool-1-thread-1] api.SFSApi     - Login in, { Zone: --=={{{ AdminZone }}}==-- }, ( User Name: sfsadmin, Id: 1, Priv: 0, Sess: 127.0.0.1:51738 )
11:57:36,185 INFO  [com.smartfoxserver.v2.controllers.SystemController-1] entities.SFSZone     - User already logged in. Disconnecting previous instance : ( User Name: Birch, Id: 0, Priv: 0, Sess: 192.168.1.3 )
11:57:36,185 INFO  [com.smartfoxserver.v2.controllers.SystemController-1] entities.SFSZone     - User: Birch was disconnected.
11:57:36,186 INFO  [com.smartfoxserver.v2.controllers.SystemController-1] api.SFSApi     - User disconnected: ( User Name: Birch, Id: 0, Priv: 0, Sess: 192.168.1.3 )
11:57:36,186 INFO  [com.smartfoxserver.v2.controllers.SystemController-1] api.SFSApi     - Login in, { Zone: test }, ( User Name: Birch, Id: 2, Priv: 0, Sess: 192.168.1.3 )
11:57:55,767 INFO  [pool-2-thread-3] sessions.DefaultSessionManager     - Session removed: { Id: 1, Type: BLUEBOX, Logged: Yes, IP: 192.168.1.3 }
11:59:51,710 INFO  [Scheduler1-thread-1] sessions.DefaultSessionManager     - Session removed: { Id: 4, Type: BLUEBOX, Logged: Yes, IP: 192.168.1.3 }
11:59:51,710 INFO  [Scheduler1-thread-1] entities.SFSZone     - User: Birch was disconnected.
11:59:51,710 INFO  [Scheduler1-thread-1] api.SFSApi     - User disconnected: ( User Name: Birch, Id: 2, Priv: 0, Sess: 192.168.1.3 )
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Extension not working

Postby rjgtav » 22 Jun 2012, 11:13

Hmm... How are you attaching the Extension?
And can you try adding a trace to the testHandler.class to confirm if that class is being called? Please also check if your extension is initing correctly by ading a trace in the init() method.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Birch
Posts: 15
Joined: 06 Jun 2012, 07:42
Location: Sweden

Re: Extension not working

Postby Birch » 22 Jun 2012, 11:50

I am attaching the extension to the room in the zone configurator -> room extension, to the room "The Lobby". And after adding a Trace both in the init() function and testHandler class it really seems as it is working. Every time i send a message to the server the trace in my testHandler gets called and shows up in the Server console. The init() trace is also there. Seems like the problem is on the unity side :?
Birch
Posts: 15
Joined: 06 Jun 2012, 07:42
Location: Sweden

Re: Extension not working

Postby Birch » 22 Jun 2012, 12:43

I added this trace:

trace(this.getParentExtension().getParentRoom().getPlayersList());

And it gives me this in the console:

14:30:35,349 INFO [com.smartfoxserver.v2.controllers.ExtensionController-1] Extensions - {testExtension}: []

I also added these traces to be sure that it is searching the right room:

trace(this.getParentExtension().getParentRoom().getName());
trace(this.getParentExtension().getName());

And it gives me:

14:30:35,349 INFO [com.smartfoxserver.v2.controllers.ExtensionController-1] Extensions - {testExtension}: The Lobby
14:30:35,349 INFO [com.smartfoxserver.v2.controllers.ExtensionController-1] Extensions - {testExtension}: testExtension

And i've double checked in the zone monitoring that i am in that room. But it seems as it can't find me :?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Extension not working

Postby rjgtav » 22 Jun 2012, 14:31

Are you sure that the client is inside that room? You can check that inside the AdminTool. And how are you joining the user in?
Can you try getUserList() instead of the getPlayersList()?
Are you running the latest SFS2X patch (2.1.0) alongside with the latest client-side Unity API?
And as ThomasLund suggested, have you tried starting from the FPS Example or the Tris example (which is much simpler)?
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Birch
Posts: 15
Joined: 06 Jun 2012, 07:42
Location: Sweden

Re: Extension not working

Postby Birch » 23 Jun 2012, 08:22

Thanks for all your help guys! Finally got it working. The problem was that i used getPlayersList instead of getUserList. I guess it's related to the fact that my room wasn't a game room? But anyways. It's working now. Thanks for your support! :D

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 2 guests