Avatar Chat Errors

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Avatar Chat Errors

Postby Guy71173cp » 14 Sep 2011, 19:29

Hello.

I have a login system in my game. Recently, I've been expierencing errors. When I login, I get this error:

****************************************************************
Internal error:
You haven't joined any rooms!
In order to interact with the server you should join at least one room.
Please consult the documentation for more infos.
****************************************************************

I tried changing my game coding to the avatar chat example coding. I am joining a room. I still expierence the errors. How would I fix this? Thanks!


~Guy71173
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 16 Sep 2011, 00:34

You probably forgot to get the room list. Please consult the examples provided in the online documentation for the proper workflow which is:

1. Connect
2. Login (needs to be successful)
3. Get Room list
4. After getting the room list, join room
Smartfox's forum is my daily newspaper.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 18 Sep 2011, 16:59

Hello BigFIsh,
Thank you for the quick answer. I changed my code to this:
smartfox.onExtensionResponse = function(resObj:Object) {
if (resObj._cmd == "logOK") {
_global.myName = resObj.name;
smartfox.getRoomList();
} else if (resObj._cmd == "logKO") {
serverstatus.text = "Error:" +resObj.error;

And I still get the error. It doesn't show any errors, but there is no avatar and whenever I type something in the chat box and press send, the error message appears in the output panel.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 18 Sep 2011, 20:44

It is fixed, but I now get this error:

Variables SET
---------------------------------------------
myID : null
User Obj: undefined
Vars: undefined
px: undefined
py: undefined
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 19 Sep 2011, 00:08

That's not really an error :)

It is probably because you're not fetching the user's variable properly.

1. You need to join a room first in order to access user variables.
2. You can't access user variables in a Limbo room. Limbo room is designed to handle many many clients, so many events are supressed.

Furthermore, you could figure the problem by analyzing the incoming/outcoming messages. You should be able to see the variables.

I would also recommend looking at some of the tutorials for the proper workflow of fetching a user varaible.
Smartfox's forum is my daily newspaper.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 19 Sep 2011, 20:26

Hello.

I went to the end of the chat coding, and added smartfox.setUserVariables() , but nothing happens. What should I do?
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 23 Sep 2011, 19:33

Anybody have an answer?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 23 Sep 2011, 23:15

I went to the end of the chat coding, and added smartfox.setUserVariables() , but nothing happens. What should I do?


Please refer to http://www.smartfoxserver.com/docs/docPages/as1/methods/setUserVariables.htm

Use the .debug flag for your SmartFoxClient instance to see what's going on. If there's nothing going out, then it is a problem on your client side. Probably because that method never got called. Please use breakpoints in flash and review the variables.
Smartfox's forum is my daily newspaper.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 25 Sep 2011, 16:06

BigFIsh,

I did what that said. I decided to use the default code for the game play (from avatar chat), and my own custom login. Codes:

Connect and login code:

[code]import it.gotoandplay.smartfoxserver.*;
#include "lib/[flashAPI].as"
#include "lib/easingEquations.as"
#include "lib/easing.as"

stop();
var ip:String = "127.0.0.1";
var port:Number = 9339;
var zone:String = "simpleChat";
var connected:Boolean = false;
userName._visible = false;
passWord._visible = false;
login_btn._visible = false;
var smartfox:SmartFoxClient = new SmartFoxClient();
smartfox.debug = true;
connect();
function connect() {
status.text = "Connecting";
smartfox.connect(ip,port);
}
smartfox.onConnection = function(success) {
if (success) {
status.text = "Successfully connected!";
connected = true;
userName._visible = true;
passWord._visible = true;
login_btn._visible = true;
login_btn.onRelease = sendLogin;
Selection.setFocus(userName);
} else {
status.text = "Can't connect!";
}
};
function sendLogin() {
if (connected) {
if (userName.text != "") {
error.text = "";
status.text = "Logging in...";
smartfox.login(zone,userName.text,passWord.text);
}
}else{
connect()
}
}
smartfox.onExtensionResponse = function(resObj:Object) {
if (resObj._cmd == "logOK") {
_global.myName = resObj.name;
smartfox.getRoomList();
} else if (resObj._cmd == "logKO") {
status.text = "Error at login: "+resObj.error;

}
};
smartfox.onRoomListUpdate = function(roomList:Object) {
gotoAndStop(16);
display.text = "Logged in as "+_global.myName;
};
smartfox.onConnectionLost = function() {
gotoAndStop(1);
status.text = "Disconnected";
connected = false;
userName._visible = false;
passWord._visible = false;
};




/*
smartfox.onExtensionResponse = function(resObj:Object) {

if (resObj._cmd == "logOK") {
status_txt.text = "Login Successful. Logging in..."
// Login Success Example Code
_global.myName = resObj.name
gotoAndStop(16)
} else if (resObj._cmd == "logKO") {
// Login failed example code
status_txt.text = resObj.err;
}
}

/*
function sendLogin()
{
if (!_global.isBusy)
smartfox.login(zone, login_txt.text)
}


/*function sendLogin() {
if (connected) {
if (login_txt.text != "") {
login_txt.text = "";
status_txt.text = "Logging in...";
butt_login.onRelease = sendLogin
smartfox.login(zone,login_txt.text,pass_txt.text);
}
}else{
connect()
}
}
smartfox.onExtensionResponse = function(resObj:Object) {
if (resObj._cmd == "logOK") {
_global.myName = resObj.name;
smartfox.getRoomList();
} else if (resObj._cmd == "logKO") {
status_txt.text = "Error at login: "+resObj.error;
status_txt.text = "Connected";
}
};

Handle login response from server


smartfox.onLogin = function(resObj:Object)
{
if (resObj.success)
{
// Login Succesfull
_global.myName = resObj.name
}
else
{
// Login Failed
_gloabl.isBusy = true

// Show an error window
var win:MovieClip = showWindow("errorWindow")
win.errorMsg.text = resObj.error
}
}


function loginUser(login_txt:String, password_txt:String){
if(login_txt.length != 0){

if(password_txt.length != 0){
_root.sfs.login("simpleChat", login_txt, password_txt);
} else {
trace("Your password cannot be 0 characters long");
}
}
}
*/



//----------------------------------------------------------
// Handle the onRoomListUpdate here and keep it in the
// queue. We'll handle it in the next frame.
//----------------------------------------------------------
smartfox.onRoomListUpdate = function(o:Object)
{
evtQueue.push(o)
gotoAndStop(16)
}


//----------------------------------------------------------
// Handle unexpected server disconnection
//----------------------------------------------------------
smartfox.onConnectionLost = function()
{
gotoAndStop(1)
}



//----------------------------------------------------------
// Show / Hides the login input field and submit button
//----------------------------------------------------------
function showLogin(bool:Boolean)
{
butt_login._visible = bool
login_txt._visible = bool
loginBox._visible = bool

if (bool)
Selection.setFocus("login_txt")
}



//----------------------------------------------------------
// 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
}


stop();
function deadClick () {
}

var myMenu:ContextMenu = new ContextMenu();
myMenu.hideBuiltInItems();

var copyrightNotice:ContextMenuItem = new ContextMenuItem("Donut Island Inc.", deadClick);
var mySiteLink:ContextMenuItem = new ContextMenuItem("Donut Island!", gotoMySite);
myMenu.customItems.push(mySiteLink, copyrightNotice);

_root.menu = myMenu;

function gotoMySite () {
getURL("http://www.donutisland.tk", "_top");
}
copyrightNotice.separatorBefore = true;

Game play:

import it.gotoandplay.smartfoxserver.*
stop()

//----------------------------------------------------------
// Setup components callback functions
//----------------------------------------------------------
roomList_lb.setChangeHandler("changeRoom")



//----------------------------------------------------------
// Setup textfields
//----------------------------------------------------------
userName_txt.text = "Logged as: " + _global.myName
input_txt.text = ""



//----------------------------------------------------------
// Setup global vars
//----------------------------------------------------------
var areaW:Number = 600 // width of the are where avatars can move
var areaH:Number = 335 // hieght of the are where avatars can move
var avatarW:Number = 40 // width of the avatar
var avatarH:Number = 40 // height of the avatar

var inited:Boolean = false // flag to see if the application has been initialized

var myAvatar:MovieClip // my Avatar symbol


//----------------------------------------------------------
// Setup Mouse Listener
//----------------------------------------------------------
var myMouse:Object = {}
myMouse.onMouseDown = function()
{
if (inited)
{
if (!_global.isBusy)
{
var px:Number = int(avatarMC._xmouse)
var py:Number = int(avatarMC._ymouse)



if ((px > avatarW/2) && (px < areaW - avatarW/2) && (py > avatarH/2) && (py < areaH - avatarH/2))
{
// save new variables
// Please note that init is set to false:
// this means that we're only moving somewhere and we don't need to init tha avatar
smartfox.setUserVariables({px:px, py:py, init:false})

// method derived from the [flashAPI].as
// moves the mc using the "Quint" equation, with "InOut" easying
// to the new px,py position in 100 milliseconds.
myAvatar.easingTo("Quint", "InOut", px, py, 100)
}
}
}
}



//----------------------------------------------------------
// Handles the RoomList response from server
//----------------------------------------------------------
// roomList is an array of room objects
// Each room objects has these methods:
//
// getId() = get room id
// getName() = get room name
// isPrivate() = is the room private ? (0 = false, 1 = true)
// isTemp() = is the room temporary ? (0 = false, 1 = true)
// isGame() = is the room holding a game ? (0 = false, 1 = true)
// getUserCount() = get # of users in the room
// getMaxUsers() = get capacity of the room
// getUser(id) = get the user Object from a known user id
// getUserList() = get the userList Object
// variables = a property Object containing all room variables
//----------------------------------------------------------
smartfox.onRoomListUpdate = function(roomList:Object)
{
roomList_lb.removeAll()

for (var i:String in roomList)
{
var room = roomList[i]
roomList_lb.addItem(room.getName() + " (" + room.getUserCount() + ")", room.getId())
}
roomList_lb.sortItemsBy("label", "ASC")

// Join the default room
this.autoJoin()
}

// Handle roomListUpdate events that occurred before the playhead was moved to this frame
if (evtQueue.length > 0)
{
smartfox.onRoomListUpdate(evtQueue[0])
delete evtQueue
}


//----------------------------------------------------------
// Handles the JoinRoom response
// Upon joining a room the client receives the userList of
// that room. By calling the getUserList() method on the
// room Object you get the userList Object
//
// You can use these methods on every user Object:
//
// getId() = get user unique id
// getName() = get user nickname
// variables = a property Object containing all user vars
//----------------------------------------------------------
smartfox.onJoinRoom = function(roomObj:Object)
{
cleanAvatars()

var roomId:Number = roomObj.getId()
var userList:Object = roomObj.getUserList()

resetRoomSelected(roomId)

_global.currentRoom = roomObj

// Update Room Name in the avatar area
currentRoom.htmlText = "Current room: <b>" + roomObj.getName() + "</b>"

// Clear current list
userList_lb.removeAll()

for (var i:String in userList)
{
var user:User = userList[i]
var uName:String = user.getName()
var uId:Number = user.getId()

userList_lb.addItem(uName, uId)

if (uName != _global.myName)
{
var uVars:Object = user.getVariables()

var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + uId, Number(uId))
mc._x = uVars.px
mc._y = uVars.py
mc.disc.gotoAndStop(uVars.col)
mc.name.text = uName
}
}

// Sort names
userList_lb.sortItemsBy("label", "ASC")

setupMyAvatar()
}



//----------------------------------------------------------
// Handles Join Room Errors
//----------------------------------------------------------
smartfox.onJoinRoomError = function(errorMsg)
{
var win:MovieClip = showWindow("errorWindow")
win.errorMsg.text = errorMsg

// Put the selected room in the combo box back to its old value
resetRoomSelected(smartfox.activeRoomId)
}



//----------------------------------------------------------
// Handles a new user entering the room
//----------------------------------------------------------
smartfox.onUserEnterRoom = function(fromRoom, user)
{
var userId:Number = user.getId()
var userName:String = user.getName()

// Add user to the userList listbox
userList_lb.addItem(userName, userId)

// Sort names
userList_lb.sortItemsBy("label", "ASC")

updateRoomStatus(fromRoom)

// Show the user avatar
var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + userId, userId)

mc._x = user.variables["px"]
mc._y = user.variables["py"]
mc.name.text = userName

mc.disc.gotoAndStop(user.variables["col"])
}



//----------------------------------------------------------
// Handles a new user leaving the room
//----------------------------------------------------------
smartfox.onUserLeaveRoom = function(fromRoom:Number, usrId:Number)
{
for (var i:Number = 0; i < userList_lb.getLength(); i++)
{
var item:Object = userList_lb.getItemAt(i)

if (item.data == usrId)
{
var usrName:String = item.label
userList_lb.removeItemAt(i)
break
}
}

// Destroy avatar from screen
avatarMC["avatar_" + usrId].removeMovieClip()

// Sort names
userList_lb.sortItemsBy("label", "ASC")
updateRoomStatus(fromRoom)

}



//----------------------------------------------------------
// Handles a change of variables in User
//----------------------------------------------------------
smartfox.onUserVariablesUpdate = function(user:User)
{
var currRoom:Number = this.getActiveRoom()
var id:Number = user.getId()
var uVars:Object = user.getVariables()
var mc:MovieClip

if (uVars.init)
{
mc = avatarMC.attachMovie("avatar", "avatar_" + id, Number(id))

mc._x = uVars.px
mc._y = uVars.py
mc.disc.gotoAndStop(uVars.col)
mc.name.text = user.getName()
}
else
{
mc = avatarMC["avatar_" + id]
mc.easingTo("Quint", "InOut", uVars.px, uVars.py, 100)
}

}



//----------------------------------------------------------
// Handles a user count change in the room
//----------------------------------------------------------
smartfox.onUserCountChange = function(roomObj:Room)
{
updateRoomStatus(roomObj.getId())
}



//----------------------------------------------------------
// Handles a public message
//----------------------------------------------------------
smartfox.onPublicMessage = function(msg:String, user:User)
{
var mc:MovieClip = avatarMC["avatar_" + user.getId()]

chat_txt.htmlText += "<b>[ " + user.getName() + " ]:</b> " + msg
main_sb.setScrollPosition(chat_txt.maxscroll)

if (msg.length > 50)
msg = msg.substring(0,48) + "..."

mc.bubble._visible = true
mc.bubble.message.text = msg
mc.deactivate()
}



//----------------------------------------------------------
// Handles an admin message
//----------------------------------------------------------
smartfox.onAdminMessage = function(msg:String, user:User)
{
var mc:MovieClip = showWindow("adminWindow")
mc.errorMsg.text += msg + "\n"
}


//----------------------------------------------------------
// Handles a new room added to the zone
//----------------------------------------------------------
smartfox.onRoomAdded = function(roomObj:Object)
{
roomList_lb.addItem(roomObj.getName() + " (" + roomObj.getUserCount() + ")", roomObj.getId())
roomList_lb.sortItemsBy("label", "ASC")
}



//----------------------------------------------------------
// Handles a new room deleted in the zone
//----------------------------------------------------------
smartfox.onRoomDeleted = function(roomObj:Object)
{
for (var i = 0; i < roomList_lb.getLength(); i++)
{
if (roomObj.getId() == roomList_lb.getItemAt(i).data)
{
roomList_lb.removeItemAt(i)
break;
}
}
}



//----------------------------------------------------------
// Sends a new public chat message to the other users
//----------------------------------------------------------
function sendChatMsg()
{
if (input_txt.text.length > 0)
{
smartfox.sendPublicMessage(input_txt.text)
input_txt.text = ""
}
}



//----------------------------------------------------------
// Handle changes in the roomList listbox
//----------------------------------------------------------
function changeRoom()
{
if(!_global.isBusy)
{
var item:Object = roomList_lb.getSelectedItem()

// new Room id
var newRoom:String = item.data

if (newRoom != smartfox.activeRoomId)
{
// Check if new room is password protected
var priv:Boolean = smartfox.getRoom(newRoom).isPrivate()

if (priv)
{
// Save newroom as _global for later use
_global.newRoom = newRoom

showWindow("passwordWindow")
}
else
{
// Pass the room id
smartfox.joinRoom(item.data)
}
}
}
}



//----------------------------------------------------------
// Create my Avatar
// Randomly generate the x,y position and a color
//
// Then initiliaze my user variables saving:
// px = mc._x position
// py = mc._y position
// col = avatar color
// init = a flag, that tells that the avatar is initializing
//----------------------------------------------------------
function setupMyAvatar()
{
if (!inited)
{
var col:Number = int(Math.random() * 8) + 1

myAvatar = avatarMC.attachMovie("avatar", "avatar_" + smartfox.myUserId, 99999)

myAvatar.disc.gotoAndStop(col)
myAvatar.name.text = _global.myName

var px:Number = int(Math.random() * (areaW - myAvatar._width / 2))
var py:Number = int(Math.random() * (areaH - myAvatar._height / 2))

myAvatar._x = px
myAvatar._y = py

// Store the avatar position on the server
smartfox.setUserVariables({px:px, py:py, col:col, init:true})

// Testing ONLY!!!
trace("Variables SET")
trace("---------------------------------------------")
trace("myID : " + smartfox.myUserId)
var self = smartfox.getRoom(smartfox.activeRoomId).getUser(smartfox.myUserId)
trace("User Obj: " + self)
trace("Vars: " + self.variables)
trace("px: " + self.variables["px"])
trace("py: " + self.variables["py"])

inited = true
Mouse.addListener(myMouse)
}
}



//----------------------------------------------------------
// Clean all Avatars from screen, except mine
//----------------------------------------------------------
function cleanAvatars()
{
for (var mc:String in avatarMC)
{
if (avatarMC[mc] != myAvatar)
avatarMC[mc].removeMovieClip()
}
}



//----------------------------------------------------------
// Enter a password protected room
// This function is being called by the pwdWindow movieclip
//----------------------------------------------------------
function loginProtectedRoom(pwd:String)
{
hideWindow("passwordWindow")
smartfox.joinRoom(_global.newRoom, pwd)
}



//----------------------------------------------------------
// Reset the selected item in the RoomList listbox
// Used when a password protected login has failed
//----------------------------------------------------------
function resetRoomSelected(id:Number)
{
var status:Boolean = roomList_lb.getEnabled()

roomList_lb.setEnabled(true)

for (var i:Number = 0; i < roomList_lb.getLength(); i++)
{
var item:Object = roomList_lb.getItemAt(i)

if (item.data == id)
{
roomList_lb.setSelectedIndex(i)
break
}
}

roomList_lb.setEnabled(status)
}



//----------------------------------------------------------
// Update the label of a Room in the listbox
// Used when updating the # of users in the room
//----------------------------------------------------------
function updateRoomStatus(roomId:Number)
{
var room:Room = smartfox.roomList[roomId]

var newLabel:String = room.getName() + " (" + room.getUserCount() + ")"

for (var i:Number = 0; i < roomList_lb.getLength(); i++)
{
var item:Object = roomList_lb.getItemAt(i)

if (roomId == item.data)
{
roomList_lb.replaceItemAt(i, newLabel, item.data)
break;
}
}
}



//----------------------------------------------------------
// Log out, and shut down connection
//----------------------------------------------------------
function closeConnection()
{
smartfox.disconnect()
gotoAndStop("connect")
}
[/code]

Do you see anything wrong?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 25 Sep 2011, 19:11

If you're using custom login, you will need to manually set sfs.myUserId and sfs.myUserName upon successful login in order for setUserVariables to work.

Please refer to http://www.smartfoxserver.com/docs/docPages/tutorials_pro/09_secureLogin/index.htm as it will have more information about this.
Smartfox's forum is my daily newspaper.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 26 Sep 2011, 19:29

I read that, but how would I achieve this? Would it be something like this?

Code: Select all

smartfox.myUserId
smartfox.myUserName
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 27 Sep 2011, 22:40

Those info need to be passed from the extension upon successful login in which you will then need to manually set them.
Smartfox's forum is my daily newspaper.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 29 Sep 2011, 19:31

So what your saying is, I need to pass these via extension? I thought that wouldn't work? Sorry, I'm SO confused. :?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 30 Sep 2011, 17:05

Yes, you need to pass the userId and userName via your zone extension upon successful login. In other words, you send them with your 'logOK' command.
Smartfox's forum is my daily newspaper.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Postby Guy71173cp » 01 Oct 2011, 14:12

I have this in my extension:

Code: Select all

if (count == 1) {
            trace("SUCCESSFULL LOGIN");
            var obj = _server.loginUser(userName, passWord, SocketChannel);
            if (obj.success) {
               response._cmd = "logOK";
               response.name = userName;
               smartfox.myUserId
                    smartfox.myUserName



Everything is fine, but when I login, the variables are still not assigned. I also looked at the server, and it said Smartfox is not defined line 44

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 16 guests