onExtensionResponse no longer called after leaving game room

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

Moderators: Lapo, Bax

Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

onExtensionResponse no longer called after leaving game room

Postby Rashomon » 19 Jul 2011, 16:00

I am working with SmartFoxServer Pro and Flash/Actionscript CS5.

I am working on a dynamic avatar screen that is hooked to a database. You change the avatarIndex and then update the database. When you go into a game room, your avatar will reflect your change. (For game design reasons, I cannot do this within the game room.)

This works great in the avatar screen. You can make multiple changes, and they all get updated in the database. However, if you go into a game room, leave it, and then back to the avatar selection screen, the onExtensionResponse no longer receives the response from my extension.

I have traces in my extension that clearly show the updates are taking effect. I can even veryify the changes through MySQL Workbench. However, the extension response NEVER gets called.

My best guess is that I am not handling something properly when I leave the game room and go back to my lobby (often the chat room, in the examples), but I can't figure out what that would be.


Here's my code:

Click event in the avatar screen:

Code: Select all

function onUpdateData( evt:MouseEvent ):void
{
   updateRecord();
}
function updateRecord()
{
   // prepare the request object
   var req:Object = {};
   req.user_id = 1;   // Just set it manually, for now
   req.avatar_name = txtAvatarName.text;
   req.avatar_index = (int(mc_standingAvatar.currentFrame)-1);

   smartFox.sendXtMessage("myDatabaseExtension", "updateData", req, "xml");
}



The request handler in myDatabaseExtension.as:

Code: Select all

function handleRequest(cmd, params, user, fromRoom)
{
   trace(">>> handleRequest");
   if (cmd == "updateData")
   {
      trace(">>> updateData");
      var sql = "UPDATE user_table SET "
      sql += " avatar_name='" + _server.escapeQuotes(params.avatar_name) + "',"
      sql += " avatar_index='" + _server.escapeQuotes(params.avatar_index) + "' "
      sql += " WHERE user_id='" +  _server.escapeQuotes(params.user_id) + "'"
      
      var success = dbase.executeCommand(sql)
      
      if (success)
      {
         trace(">>> successful update");
         var response       = {}
         response._cmd      = "updateData"
         
         response.user_id    = params.user_id
         response.user_name    = params.user_name
         response.avatar_name    = params.avatar_name
         response.avatar_index   = params.avatar_index
         
         trace(">>> sending server response");
         _server.sendResponse(response, -1, null, [user])
      }
   }
}



My onExtensionResponse handler:

Code: Select all

function onExtensionResponse( evt:SFSEvent ):void
{
   trace("onExtensionResponse");
   var resObj:Object = evt.params.dataObj
   var type:String = evt.params.type
   var db_entry:Object = new Object();
   messageQueue.push({ resObj: resObj, type: type })
   
   trace("cmd:  " + resObj._cmd)
   trace("type: " + type)
   
   // We expect the response to be xml formatted
   if (type == "xml")
   {
      // Let's check if it's a "getData" message
      if (resObj._cmd == "updateData")
      {
         delete resObj._cmd
         
         trace("--- Update response is: ---");
         trace("resObj.user_id   = " + resObj.user_id);
         trace("resObj.user_name = " + resObj.user_name);
         trace("resObj.avatar_name  = " + resObj.avatar_name);
         trace("resObj.avatar_index = " + resObj.avatar_index);
         
         smartFox.setUserVariables({ uAvatarName: resObj.avatar_name, uAvatarIndex: resObj.avatar_index});
         Globals.vars.myAvatarIndex = resObj.avatar_index;
         trace("Globals.vars.myAvatarIndex = " + Globals.vars.myAvatarIndex);
         trace("-------------------------------");
      }
      
}



My leaveGameRoom handler: (I've tried different combinations of these three lines.)

Code: Select all

function leaveGameRoom():void
{
   smartFox.joinRoom( "Lobby", "" );
   gotoAndStop( "lobby" );   
   smartFox.getRoomList();
}



My roomListUpdate handler:

Code: Select all

function onRoomListUpdate( evt:SFSEvent ):void
{
   removeEventListener(Event.ENTER_FRAME, updateFilter);
   if (LOGGING_IN) {
      gotoAndStop( "check" );
   } else {
      gotoAndStop("lobby");
   }
}


The first time the user enters the game, my code takes him to the "check" frame. This frame checks to see if there is an existing avatar in the database. If there is, "check" then takes him to the "lobby". If no avatar exists, then it takes the user to the avatar selection screen. We don't have to do this check after he's already logged in, which is what the LOGGING_IN flag controls.

If any of you know what's going on, I would be extremely grateful.

Thanks in advance.
Sarevok
Posts: 75
Joined: 12 Apr 2011, 22:12

Postby Sarevok » 19 Jul 2011, 20:09

What is the last trace you see on server? That is first thing that could help in detecting where things go wrong. In other words, do you see "">>> successful update" and ">>> sending server response" on server console when problem happens?
Also, did you check debug output on client side, to see if extension response is sent by server or not?

Also, I have one question, because I don't know actionscript. You use following code to send response:

Code: Select all

 _server.sendResponse(response, -1, null, [user])


What "[user]" means in actionscript? In java, when you are sending response you have to specify user socket channel (not user object). I guess AS API is a bit different, but I am just curious :)
Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

Postby Rashomon » 20 Jul 2011, 20:11

The last two lines on my server trace are:

>>> successful update
>>> sending server response

The last lines in my client-side trace are:

executeMessage:
type = xml
cmd = checkForAvatar

After taking a closer look at my client-side traces, I realized that two different response handlers were getting called.

I built my overall game based on the "Realtime Maze" example (AS3). Someone correct me if I'm wrong, but this is my understanding:

"Realtime Maze" uses an onExtensionResponse and an onExtensionResponses (plural) handler. The first one handles responses that need to be done immediately on that frame. It also adds the responses to a queue, so they can still be processed even if the user goes to a different room, such as one of the game rooms.

The onExtensionResponses (plural) handler is in the game room. One of the first things that happens when the user enters the room is that it calls a function to process all responses in the queue.

When I followed this example, my event listeners were out of synch. I had both listeners in my connect room, before entering the lobby/chat room. I added the onExtensionResponse (singluar) to my lobby/chat room.

I had onExtensionResponse (singular) in my game room. I changed that to onExtensionResponses (plural).

I think my responses were either going to the room I just left, or they were going to limbo. After making these changes, though, I am now correctly processing responses, both before and after entering/leaving my game rooms.

Thanks for the help, Sarevok!

(As for the [user] in AS3, I'm not 100% positive. I think that whole line tells SFS that it does not need the user name or other parms. Someone else can give you a better answer.)
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 21 Jul 2011, 04:36

(As for the [user] in AS3, I'm not 100% positive. I think that whole line tells SFS that it does not need the user name or other parms. Someone else can give you a better answer.)


That parameter takes an array of users. [user] is this: An array with a sfs user object in its first element. It's same as this: var list:Array = new Array(); list.push(user);

You can even do this: [user, user1, user2, user3]
Smartfox's forum is my daily newspaper.

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 7 guests