Register system causing bugs everywhere!

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Ninjaoninja2
Posts: 204
Joined: 22 Sep 2013, 23:33

Register system causing bugs everywhere!

Postby Ninjaoninja2 » 26 Mar 2014, 04:13

Hello,
I am using the following extension code for a register system:

Code: Select all

var dbase

function init(){
   dbase = _server.getDatabaseManager()
}

function destroy(){
   delete dbase
}

function handleInternalEvent(evt){ //not used }

function handleRequest(cmd, params, user, fromRoom){
   if (cmd == "register") {
      trace("starting registration proccess")
      var userName = params.uName;
      var passWord = params.pass;
      var email = params.email;
      
      var response = {_cmd:cmd };
      var error = "";
      var success = false;
      
      var sql = "SELECT COUNT (NAME) FROM USERS WHERE NAME='"+_server.escapeQuotes(userName)+"'";
      var queryRes = dbase.executeQuery(sql)
      
      if (queryRes != null){
         var Row = queryRes.get(0)
         var count = Row.getItem("COUNT(NAME)");
            
         if(count == 0){
            trace("USERNAME NOT REGISTERED, REGISTERING");
            sql = "INSERT INTO USERS (NAME, PASS, EMAIL) VALUES ('" + userName + "', '" + passWord + "', '" + email + "')";
            queryRes = dbase.executeCommand(sql);
            success = true;
         }else{
            trace("USERNAME ALREADY REGISTERED!")
            error = "The username entered is already registered.";
         }
      }else{
         error = "Error connecting to the database";
      }
      
      response.error = error;
      response.success = success;
      _server.sendResponse(response, -1, null, [user])
   }
   }
}

And then for the login:

Code: Select all

var dbase
var userName;
var passWord;
var SocketChannel;
 
function init(){
 dbase = _server.getDatabaseManager()
}
 
function destroy(){
 delete dbase
 delete userName;
 delete passWord;
 delete SocketChannel;
}
 
function handleInternalEvent(evt)
{
 if(evt.name == "loginRequest"){ 
  var error = "";
   
  userName = evt["nick"];
        passWord = evt["pass"];
        SocketChannel = evt["chan"];
   
  var sql = "SELECT COUNT (NAME) FROM USERS WHERE NAME='"+userName+"' AND PASS='"+passWord+"'";
  var queryRes = dbase.executeQuery(sql)
   
  var response = {}
   
  if (queryRes != null){
   var Row = queryRes.get(0)
   var count = Row.getItem("COUNT(NAME)");
     
   if(count == 1){
    trace("SUCCESSFULL LOGIN")
    var obj = _server.loginUser(userName, passWord, SocketChannel)
     
    if(obj.success){
     response._cmd = "logOK";
     response.name = userName;
    }else{
     error = obj.error;
     response._cmd = "logKO";
    }
   }else if(count == 0){
    trace("FAILED LOGIN")
    response._cmd = "logKO";
    error = "Wrong username or password";
   }
  }else{
   response._cmd = "logKO";
   error = "Error connecting to the database"
  }
   
  response.error = error;
  _server.sendResponse(response, -1, null, SocketChannel)
 }
}

But it seems the register broke the login because now I can't login and the register is also broken because it just keeps saying "Connecting" and never imports any of the users into my H2 DB here is my connect code from one of the frames might be helpful:

Code: Select all

import it.gotoandplay.smartfoxserver.*;

#include "lib/[flashAPI].as"
#include "lib/easingEquations.as"
#include "lib/easing.as"

/*
* ----------------------------------------------------------------------------
*[ Avatar Chat ] version 1.1.0 -- Actionscript 2.0
*a SmartFoxServer sample application
*
*www.smartfoxserver.com
*(c) 2004 gotoAndPlay()
* ----------------------------------------------------------------------------
*/
stop();
Stage.showMenu = false;


//----------------------------------------------------------
// Setup global variables
//----------------------------------------------------------
var stageW:Number = 780;
var stageH:Number = 490;

// hide semi-transparent panel
// used when a dialog box is shown
disabler._visible = false;

// isBusy is true when the application
// is requiring input through a dialog box
// When true all other controls are temporarily disabled
_global.isBusy = false;

// An event queue
var evtQueue:Array = [];


//----------------------------------------------------------
// Setup global variables
//----------------------------------------------------------
showLogin(false);
status_txt.text = "Connecting...";



//----------------------------------------------------------
// Server configuration
//
// ip = IP address of the server, if the server is running
//   locally use 127.0.0.1
// port= default value is 9339
// zone = the Server "zone" name for this application
//----------------------------------------------------------

var ip:String = "192.168.1.8";
var port:Number = 9339;
var loginZone:String = "simpleChat";
var registerZone:String = "simpleChat2";
var connected:Boolean = false;
var task = "";

var smartfox:SmartFoxClient = new SmartFoxClient();
smartfox.onConnection = handleConnection;
if (task == "login")
{
   smartfox.login(loginZone,userName.text,passWord.text);
}
else if (task == "register")
{
   smartfox.login(registerZone,"","");
}
else if (task == "")
{
}
smartfox.debug = true;


// Connect to the server
smartfox.connect(ip,port);



//----------------------------------------------------------
// Handle connection response from server
//----------------------------------------------------------
function handleConnection(success:Boolean)
{
   if (success)
   {
      status_txt.text = "Connected, please login:";
      showLogin(true);
      butt_login.onRelease = sendLogin;
   }
   else
   {
      status_txt.text = "Can't connect!";
   }
}



//----------------------------------------------------------
// Send login params to the server
// server.login(zone, nickName, password)
//----------------------------------------------------------
function sendLogin() {
 if (connected) {
  if (userName.text != "") {
   error.text = "";
   status.text = "Logging in...";
   smartfox.login(zone,userName.text,passWord.text);
  }
 }else{
  connect()
 }
}
//----------------------------------------------------------
// Handle login response from server
//----------------------------------------------------------
smartfox.onExtensionResponse = function(resObj:Object)
{
        if (resObj._cmd == "logOK")
        {
                // Login Successful
                _global.myName = resObj.name
                gotoAndStop("chat")
        }
        else if (resObj._cmd == "logKO")
        {
                // Login Failed
                _gloabl.isBusy = true
               
                // Show an error window
                var win = showWindow("errorWindow")
                win.errorMsg.text = resObj.err
        }
}
//----------------------------------------------------------
// Handle the onRoomListUpdate here and keep it in the
// queue. We'll handle it in the next frame.
//----------------------------------------------------------
smartfox.onRoomListUpdate = function(roomList:Object) {
   if(task == "login"){
      display.text = "Logged in as "+_global.myName;
   }else if(task == "register"){
      smartfox.autoJoin();
   }
};

//----------------------------------------------------------
// Handle unexpected server disconnection
//----------------------------------------------------------
smartfox.onConnectionLost = function() {
 gotoAndStop("connect");
 status.text = "Disconnected";
 connected = false;
 userName._visible = false;
 passWord._visible = false;
};

//----------------------------------------------------------
// Show / Hides the login input field and submit button
//----------------------------------------------------------
function showLogin(bool:Boolean)
{
   butt_login._visible = bool;
   userName._visible = bool;
   loginBox._visible = bool;
   if (bool)
   {
      Selection.setFocus("userName");
   }
}



//----------------------------------------------------------
// Shows a popup window and disables all other controls
//----------------------------------------------------------
function showWindow(linkageName:String):MovieClip
{
   _global.isBusy = true;
   disabler._visible = true;
   roomList_lb.setEnabled(false);
   userList_lb.setEnabled(false);
   var win = _root.attachMovie(linkageName, linkageName, 9999);
   win._x = (stageW / 2) - (win._width / 2);
   win._y = (stageH / 2) - (win._height / 2);
   return win;
}



//----------------------------------------------------------
// Hides a popup window and re-enable the controls
//----------------------------------------------------------
function hideWindow(wName:String)
{
   this[wName].removeMovieClip();
   roomList_lb.setEnabled(true);
   userList_lb.setEnabled(true);
   disabler._visible = false;
   _global.isBusy = false;
}

If you see ANY errors please do not hesitate to help me correct them!

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 55 guests