Send response to all users in a room (AS)

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

Moderators: Lapo, Bax

Post Reply
Soleil
Posts: 19
Joined: 06 Mar 2010, 06:24

Send response to all users in a room (AS)

Post 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?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
Nieles
Posts: 12
Joined: 25 Mar 2010, 15:07

Post 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.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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")
Lapo
--
gotoAndPlay()
...addicted to flash games
Nieles
Posts: 12
Joined: 25 Mar 2010, 15:07

Post 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]
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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.
Lapo
--
gotoAndPlay()
...addicted to flash games
Nieles
Posts: 12
Joined: 25 Mar 2010, 15:07

Post 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")
	}
	
	
}
Nieles
Posts: 12
Joined: 25 Mar 2010, 15:07

Post by Nieles »

I indeed get a server error:

Image
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
Nieles
Posts: 12
Joined: 25 Mar 2010, 15:07

Post 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?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply