External DB and Custom Login

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

Moderators: Lapo, Bax

Prefix
Posts: 4
Joined: 21 Oct 2011, 02:10

External DB and Custom Login

Postby Prefix » 21 Oct 2011, 02:38

Hello all, and thank you for reading and responding in advance. I've used the default login that SFS2x had in place for the tutorial FPS. Ive tweaked it a little and it worked well.

I recently expanded on this to create an external Database and so on. I've read the tutorials, how to's and utilized this Tutorial....

Recipe #1 — Custom login with database:

Since then, there's been some hardships and bugs and tweaking to get it workable. Ive finally gotten down to... pressing play on Unity, getting to a login screen. Typing in the name & password, IP and port. Click on Login, and then i get the following messages. I've looked through script after script and have no clue as to what im looking for, or where exactly the error is occuring.

So in short, it "hangs" on ... Sending login Request, approx 20-30 sec... Then finishes with ... OnConnectionLost.

[img][img]http://img840.imageshack.us/img840/6482/customloginhelp.png[/img]

Uploaded with ImageShack.us[/img]
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 21 Oct 2011, 22:19

Can you post the part of the script where you make the connection and all the callbacks please.
Are you processing the events that come back from SFS ?
Prefix
Posts: 4
Joined: 21 Oct 2011, 02:10

Postby Prefix » 22 Oct 2011, 06:12

appels wrote:Can you post the part of the script where you make the connection and all the callbacks please.
Are you processing the events that come back from SFS ?


My apologies, im just getting a grasp on the concept of this stuff. If you need more info, just lemme know in a little more detail what you need. Im still a begginer with this stuff :)

Thanks....

This is the LoginEventHandler

Code: Select all


package sfs2x.extension.test.dblogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class LoginEventHandler extends BaseServerEventHandler
{
   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException
   {
      // Grab parameters from client request
      String userName = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
      String cryptedPass = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
      ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
      
      // Get password from DB
      IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
      Connection connection;
      
        try
        {
           // Grab a connection from the DBManager connection pool
           connection = dbManager.getConnection();
          
           // changed muppets to my database test name.
           PreparedStatement stmt = connection.prepareStatement("SELECT pword,id FROM test WHERE name=?");
           stmt.setString(1, userName);
          
           // Execute query
         ResultSet res = stmt.executeQuery();
         
         // Verify that one record was found
         if (!res.first())
         {
            // This is the part that goes to the client
            SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
            errData.addParameter(userName);
            
            // This is logged on the server side
            throw new SFSLoginException("Bad user name: " + userName, errData);
         }
         
         String dbPword = res.getString("pword");
         int dbId = res.getInt("id");
         
         // Verify the secure password
         if (!getApi().checkSecurePassword(session, dbPword, cryptedPass))
         {
            SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
            data.addParameter(userName);
            
            throw new SFSLoginException("Login failed for user: "  + userName, data);
         }
         
         // Store the client dbId in the session
         session.setProperty(DBLogin.DATABASE_ID, dbId);
         
         // Return connection to the DBManager connection pool
         connection.close();
        }
       
        // User name was not found
        catch (SQLException e)
        {
           SFSErrorData errData = new SFSErrorData(SFSErrorCode.GENERIC_ERROR);
           errData.addParameter("SQL Error: " + e.getMessage());
           
           throw new SFSLoginException("A SQL Error occurred: " + e.getMessage(), errData);
        }
   }
}



Client Side Login

Code: Select all

   void Start()
   {
      bool debug = false;
      if (SmartFoxConnection.IsInitialized)
      {
         smartFox = SmartFoxConnection.Connection;
      }
      else
      {
         smartFox = new SmartFox(debug);
      }
         
      smartFox.AddLogListener(LogLevel.INFO, OnDebugMessage);
   }
   
   private void AddEventListeners() {
      
      smartFox.RemoveAllEventListeners();
      
      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.LOGOUT, OnLogout);
      smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
      smartFox.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
      
      smartFox.AddEventListener(SFSEvent.ROOM_CREATION_ERROR, OnCreateRoomError);
      smartFox.AddEventListener(SFSEvent.USER_ENTER_ROOM, OnUserEnterRoom);
      smartFox.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserLeaveRoom);
      smartFox.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdded);
      smartFox.AddEventListener(SFSEvent.ROOM_REMOVE, OnRoomDeleted);
      smartFox.AddEventListener(SFSEvent.USER_COUNT_CHANGE, OnUserCountChange);
      smartFox.AddEventListener(SFSEvent.UDP_INIT, OnUdpInit);
   }
   
   void FixedUpdate() {
      smartFox.ProcessEvents();
   }
   
   private void UnregisterSFSSceneCallbacks() {
      // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
      smartFox.RemoveAllEventListeners();
   }
   
   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("Sending login request");
         smartFox.Send(new LoginRequest(username, "", zone));

      }
   }

   public void OnConnectionLost(BaseEvent evt) {
      Debug.Log("OnConnectionLost");
      isLoggedIn = false;
      isJoining = false;
      currentActiveRoom = null;
      UnregisterSFSSceneCallbacks();
   }

   // Various SFS callbacks
   public void OnLogin(BaseEvent evt) {
      try {
         bool success = true;
         if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"]) {
            loginErrorMessage = (string)evt.Params["errorMessage"];
            Debug.Log("Login error: "+loginErrorMessage);
         }
         else {
            Debug.Log("Logged in successfully");

            // Startup up UDP
            smartFox.InitUDP(serverName, serverPort);
         }
      }
      catch (Exception ex) {
         Debug.Log("Exception handling login request: "+ex.Message+" "+ex.StackTrace);
      }
   }

   public void OnLoginError(BaseEvent evt) {
      Debug.Log("Login error: "+(string)evt.Params["errorMessage"]);
   }
   
   public void OnUdpInit(BaseEvent evt) {
      if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"]) {
         loginErrorMessage = (string)evt.Params["errorMessage"];
         Debug.Log("UDP error: "+loginErrorMessage);
      } else {
         Debug.Log("UDP ok");
         PrepareLobby();   
      }
   }
   
   void OnLogout(BaseEvent evt) {
      Debug.Log("OnLogout");
      isLoggedIn = false;
      isJoining = false;
      currentActiveRoom = null;
      smartFox.Disconnect();
   }
[/code]
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 22 Oct 2011, 14:50

Your not making the connection at all in the client.
In your start function your should call a connect and the addlisteners function.
Prefix
Posts: 4
Joined: 21 Oct 2011, 02:10

Postby Prefix » 22 Oct 2011, 16:26

Ok. Could you please help me out with how this is supposed to be structured...

The client side handles the GUI and the entry login info. That info is then passed to the smartfoxserver (LoginEventHandler script)... LoginEventHandler script should be listening for the info that I have sent via client side GUI. Once Smartfox server has obtained (listened) for the information, it then sends that info to the "database" to do a "crosscheck" of username and password. Once it clears the login and password, the LoginEventHandler should now do a hand-off to the ZoneEventHandler?

Im confused as to what is supposed to be happening.

Thanks
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 24 Oct 2011, 11:11

client connects, zone extension sends to the logineventhandler, once allowed the connection becomes a client and gets sent to the joinzonehandler.
In the joinzone you can create rooms and send the connection to the room extension.
Prefix
Posts: 4
Joined: 21 Oct 2011, 02:10

Postby Prefix » 24 Oct 2011, 18:48

appels wrote:client connects, zone extension sends to the logineventhandler, once allowed the connection becomes a client and gets sent to the joinzonehandler.
In the joinzone you can create rooms and send the connection to the room extension.


Ok.. understood, thank you!
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 30 Oct 2011, 21:51

Checking the picture from the server log, there are no bound sockets!

I bet you put the wrong IP in the server config so the server isnt listening on any ports

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 17 guests