Page 1 of 1
Send response to all users in a room (AS)
Posted: 15 Mar 2010, 06:11
by Soleil
Seems like a common thing to do...
In my extension code I have this:
Code: Select all
var users = _server.getRoom(fromRoom).getUserList();
_server.sendResponse(res, -1, user, users, "str");
Error in extension [ simpleExt.as ]: TypeError: Cannot find function getRoom. (simpleExt.as#1676) Internal: 101 -- Line number:
Fair enough. So I tried this:
Code: Select all
var curZone = _server.getCurrentZone();
var curRoom = curZone.getRoom(fromRoom);
var users = curRoom.getUserList();
_server.sendResponse(res, -1, user, users, "str");
Error in extension [ simpleExt.as ]: TypeError: Cannot find function getChannel. (simpleExt.as#526) Internal: -1049 -- Line number: (MainLib line: 526)
Have no idea what that means, since I don't ever try to call "getChannel". Where can I find documentation on server-side API? The client side AS3 API is documented, but I can't find the server side code. What is the best way to send a response to all users in a room?
Posted: 16 Mar 2010, 07:53
by Lapo
The documentation is here:
http://www.smartfoxserver.com/docs/docP ... /index.htm
If you scroll down the left side menu you'll notice that there are other Server classes doc: Zone, Room etc...
I would use this:
Code: Select all
var users = _server.getRoom(fromRoom).getAllUsers();
_server.sendResponse(res, -1, user, users, "str");
Additionally you can consult the Javadoc for a full view of the server side objects:
http://www.smartfoxserver.com/docs/docP ... index.html
Posted: 25 Mar 2010, 15:12
by Nieles
I'm trying to do the same and I have the same problem!
Also this code doesn't work:
Code: Select all
var users = _server.getRoom(fromRoom).getAllUsers();
_server.sendResponse(res, -1, user, users, "str");
The actionscript server class doesn not have a function getRoom.
Is there some other way to acces a room with AS?
ooh yes the extension is running on a zone level so _server.getCurrentRoom() also doesn't work.
Posted: 25 Mar 2010, 15:22
by Lapo
Sorry my fault

The
getRoom was intended on the
Zone object.
The should read like this:
Code: Select all
var users = _server.getCurrentZone().getRoom(fromRoom).getAllUsers();
_server.sendResponse(res, -1, user, users, "str");
Or to make it a bit more readable:
Code: Select all
var zone = _server.getCurrentZone()
var room = zone.getRoom(fromRoom)
_server.sendResponse(res, -1, user, room.getAllUsers(), "str")
Posted: 25 Mar 2010, 15:39
by Nieles
Yes the code seems to be oke now, but I'm still not receiving anything.
This is the code on my client side:
Code: Select all
function onExtensionResponseHandler(evt:SFSEvent):void {
trace("gotMessage");
}
Very simpel
This is what I do, I join a room and click somewhere in the screen.
Then It send a message to smartfox:
Code: Select all
MovieClip(root).sfs.sendXtMessage("PositionParser", "setP", o, "str");
and then smartfox should send a message back to every in the room:
Code: Select all
function handleRequest(cmd, params, user, fromRoom)
{
if (cmd == "setP"){
var zone = _server.getCurrentZone()
var room = zone.getRoom(fromRoom)
var users = room.getAllUsers()
_server.sendResponse(res, -1, null, user, users, "str")
}
}
The server side code is fired because when I put a trace there it does trace it when I click.
Or does this code not send the object to sender self?[/quote]
Posted: 25 Mar 2010, 15:45
by Lapo
Where is res defined??
My example shows how to obtain the user list but you will need to create real data to be sent to those users.
The code should probably raise a server side error, because res is not defined anywhere.
Posted: 25 Mar 2010, 15:48
by Nieles
woops sorry something went wrong there, this is the full function:
Code: Select all
function handleRequest(cmd, params, user, fromRoom)
{
if (cmd == "setP"){
var res = []
res[0] = "sp"
res[1] = params[0]
res[2] = params[1]
var zone = _server.getCurrentZone()
var room = zone.getRoom(fromRoom)
var users = room.getAllUsers()
_server.sendResponse(res, -1, null, user, users, "str")
}
}
Posted: 25 Mar 2010, 16:02
by Nieles
I indeed get a server error:

Posted: 25 Mar 2010, 19:05
by Lapo
You have too many parameters in there:
_server.sendResponse(res, -1, null, user, users, "str")
The method takes 5 params, not 6
Either
null or
user should go away
Posted: 26 Mar 2010, 09:20
by Nieles
stupid of me.
Well it works now I receive messages, but I have 1 more question:
I send the following array res:
Code: Select all
var res = []
res[0] = "sp"
res.push(user.getName())
res.push(params[0])
res.push(params[1])
var zone = _server.getCurrentZone()
var room = zone.getRoom(fromRoom)
var users = room.getAllUsers()
_server.sendResponse(res, -1, null, users, "str")
Now when I trace the object in flash I get:
sp,-1,Niels,408,404
It also pushed in the from room variable -1
I don't need that variable thats why its -1, but it still sends it and that takes more bandwitdh that I don't need.
Is this a bug?
Posted: 26 Mar 2010, 14:26
by Lapo
No. It's not
Consult the docs to see how terminator based messages work. There are a couple of things to keep in mind.
Specifically I refer to this tutorial:
http://www.smartfoxserver.com/docs/docP ... /index.htm
There's also an AS3 version of this example:
http://forums.smartfoxserver.com/viewtopic.php?t=6669